Skip to content

Instantly share code, notes, and snippets.

@kanonji
Created May 10, 2016 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanonji/dccdc546ee6637ef68e434d16a99dfb4 to your computer and use it in GitHub Desktop.
Save kanonji/dccdc546ee6637ef68e434d16a99dfb4 to your computer and use it in GitHub Desktop.
Get an event when tags edited on Tag Manager.
using UnityEditor;
using UnityEditorInternal;
namespace Kanonji.TagManagerEvent
{
public delegate void OnTagsChangedHandler(string[] tags);
public class Publisher
{
public static event OnTagsChangedHandler OnTagsChanged;
[InitializeOnLoadMethod]
public static void InitEditorUpdateCallback()
{
int interval = 6;
int nextTiming = (int) EditorApplication.timeSinceStartup;
string tokenBefore = MakeToken(InternalEditorUtility.tags);
EditorApplication.CallbackFunction function = () =>
{
if (EditorApplication.timeSinceStartup < nextTiming) return;
nextTiming += interval;
string token = MakeToken(InternalEditorUtility.tags);
if (false == tokenBefore.Equals(token))
{
tokenBefore = token;
OnTagsChanged(InternalEditorUtility.tags);
// Debug.Log("InternalEditorUtility.tags changed");
}
};
EditorApplication.update += function;
}
protected static string MakeToken(string[] tags)
{
return string.Join(",", tags);
}
}
}
using UnityEngine;
using UnityEditor;
namespace Kanonji.TagManagerEvent
{
public class Subscriber
{
[InitializeOnLoadMethod]
public static void Subscribe()
{
Publisher.OnTagsChanged += Foo;
}
protected static void Foo(string[] tags)
{
Debug.Log(string.Join(",", tags));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment