Last active
December 17, 2015 01:01
-
-
Save kalineh/5fd753bf4f5103d29fa6 to your computer and use it in GitHub Desktop.
Place in Unity project to call OnScriptReloaded() on any object when scripts reload.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Reflection; | |
public class ScriptReloadBroadcast | |
{ | |
#if UNITY_EDITOR | |
[UnityEditor.Callbacks.DidReloadScripts] | |
static void OnScriptsReloaded() | |
{ | |
var flags = | |
BindingFlags.Public | | |
BindingFlags.Instance | | |
BindingFlags.FlattenHierarchy; | |
var func = Application.isPlaying ? "OnScriptReload" : "OnScriptReloadEditor"; | |
var objects = GameObject.FindObjectsOfType<GameObject>(); | |
foreach (var obj in objects) | |
{ | |
var components = obj.GetComponents<Component>(); | |
foreach (var com in components) | |
{ | |
var type = com.GetType(); | |
var method = type.GetMethod(func, flags); | |
if (method != null) | |
{ | |
method.Invoke(com, null); | |
} | |
} | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment