/// /// SURGE FRAMEWORK /// Author: Bob Berkebile /// Email: bobb@pixelplacement.com /// /// This looks over all pieces of the framework to ensure they are properly set up - this is normally needed if a script that was already added to a GameObject suddenly extends something with a RequireComponent which is a decent bug in my opinion but this approach seems to work as a decent safety net. /// /// using UnityEngine; using UnityEditor; using System; namespace Pixelplacement { [InitializeOnLoad] public class InitializationRequirements { static InitializationRequirements () { //state machines: StateMachine[] stateMachines = Resources.FindObjectsOfTypeAll (); foreach (StateMachine item in stateMachines) { if (item.GetComponent () == null) item.gameObject.AddComponent (); } //display object: DisplayObject[] displayObjects = Resources.FindObjectsOfTypeAll (); foreach (DisplayObject item in displayObjects) { if (item.GetComponent () == null) item.gameObject.AddComponent (); } //singleton (generics require some hackery to find what we need): foreach (GameObject item in Resources.FindObjectsOfTypeAll ()) { foreach (Component subItem in item.GetComponents ()) { //bypass this component if its currently unavailable due to a broken or missing script: if (subItem == null) continue; string baseType; #if NETFX_CORE baseType = subItem.GetType ().GetTypeInfo ().BaseType.ToString (); #else baseType = subItem.GetType ().BaseType.ToString (); #endif if (baseType.Contains ("Singleton") && baseType.Contains ("Pixelplacement")) { if (item.GetComponent () == null) item.gameObject.AddComponent (); continue; } } } } } }