Skip to content

Instantly share code, notes, and snippets.

@hiroki-o
Created February 15, 2017 08:00
Show Gist options
  • Save hiroki-o/c726f6f31c40073f8a7baa74d75238bc to your computer and use it in GitHub Desktop.
Save hiroki-o/c726f6f31c40073f8a7baa74d75238bc to your computer and use it in GitHub Desktop.
[AssetBundleGraphTool] Custom node sample code to set SpriteTag by Group
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine.AssetBundles.GraphTool;
using Model=UnityEngine.AssetBundles.GraphTool.DataModel.Version2;
/**
ImportSetting is the class for apply specific setting to already imported files.
*/
[CustomNode("Set Sprite Tag", 100)]
public class SetSpriteTag : Node {
[SerializeField] private SerializableMultiTargetString m_spriteTagNameTemplate;
public override string ActiveStyle {
get {
return "flow node 2 on";
}
}
public override string InactiveStyle {
get {
return "flow node 2";
}
}
public override void Initialize(Model.NodeData data) {
m_spriteTagNameTemplate = new SerializableMultiTargetString("SpriteTag_*");
data.AddDefaultInputPoint();
data.AddDefaultOutputPoint();
}
public override Node Clone() {
var newNode = new SetSpriteTag();
return newNode;
}
public override void OnInspectorGUI(NodeGUI node, AssetReferenceStreamManager streamManager, NodeGUIEditor editor, Action onValueChanged) {
EditorGUILayout.HelpBox("Set Sprite Tag: Configure sprite tag name on asset's importer.", MessageType.Info);
editor.UpdateNodeName(node);
GUILayout.Space(10f);
//Show target configuration tab
editor.DrawPlatformSelector(node);
using (new EditorGUILayout.VerticalScope(GUI.skin.box)) {
// Draw Platform selector tab.
var disabledScope = editor.DrawOverrideTargetToggle(node, m_spriteTagNameTemplate.ContainsValueOf(editor.CurrentEditingGroup), (bool b) => {
using(new RecordUndoScope("Remove Target Platform Settings", node, true)) {
if(b) {
m_spriteTagNameTemplate[editor.CurrentEditingGroup] = m_spriteTagNameTemplate.DefaultValue;
} else {
m_spriteTagNameTemplate.Remove(editor.CurrentEditingGroup);
}
onValueChanged();
}
});
// Draw tab contents
using (disabledScope) {
var val = m_spriteTagNameTemplate[editor.CurrentEditingGroup];
var newValue = EditorGUILayout.TextField("Tag Config:", val);
if (newValue != val) {
using(new RecordUndoScope("Tag Name", node, true)){
m_spriteTagNameTemplate[editor.CurrentEditingGroup] = newValue;
onValueChanged();
}
}
EditorGUILayout.HelpBox(
"You can configure tag name with \"*\" to include group name in your sprite tag.",
MessageType.Info);
}
}
}
/**
* Prepare is called whenever graph needs update.
*/
public override void Prepare (BuildTarget target,
Model.NodeData node,
IEnumerable<PerformGraph.AssetGroups> incoming,
IEnumerable<Model.ConnectionData> connectionsToOutput,
PerformGraph.Output Output)
{
// Pass incoming assets straight to Output
var destination = (connectionsToOutput == null || !connectionsToOutput.Any())?
null : connectionsToOutput.First();
if(incoming != null) {
ApplySpriteTag(target, incoming);
if(Output != null) {
foreach(var ag in incoming) {
Output(destination, ag.assetGroups);
}
}
} else {
if(Output != null) {
// Overwrite output with empty Dictionary when no there is incoming asset
Output(destination, new Dictionary<string, List<AssetReference>>());
}
}
}
private string GetTagName(BuildTarget target, string groupName) {
return m_spriteTagNameTemplate[target].Replace("*", groupName);
}
private void ApplySpriteTag(BuildTarget target, IEnumerable<PerformGraph.AssetGroups> incoming) {
foreach(var ag in incoming) {
foreach(var groupKey in ag.assetGroups.Keys) {
var assets = ag.assetGroups[groupKey];
foreach(var asset in assets) {
if(asset.filterType == typeof(UnityEditor.TextureImporter) ) {
var importer = AssetImporter.GetAtPath(asset.importFrom) as TextureImporter;
importer.spritePackingTag = GetTagName(target, groupKey);
importer.SaveAndReimport();
asset.TouchImportAsset();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment