/// <summary> /// Should be called before you use this Singleton GameObject. /// </summary> public static void Initialize() { if (IsInitialized) { return; } // highly unlikely this would need a lock since all unity calls need to happen on the main thread, but hey, being safe lock (instanceLock) { if (IsInitialized) { return; } T[] objects = GameObject.FindObjectsOfType <T>(); string className = typeof(T).Name; if (objects != null && objects.Length != 0) { Debug.LogErrorFormat(objects[0], "An object of type {0} already exists and wasn't created with Initialize function.", className); } else { // constructing the singleton object GameObject singleton = new GameObject(className, typeof(T)); singleton.transform.parent = SingletonUtil.GetSingletonContainer(); singleton.transform.Reset(); instance = singleton.GetComponent <T>(); } } }
// Initializes asset bundle namager and starts download of manifest asset bundle. // Returns the manifest asset bundle downolad operation object. public static AssetBundleLoadManifestOperation Initialize(string manifestAssetBundleName) { #if UNITY_EDITOR Log(LogType.Info, "Simulation Mode: " + (SimulateAssetBundleInEditor ? "Enabled" : "Disabled")); #endif var go = new GameObject("AssetBundleManager", typeof(AssetBundleManager)); GameObject.DontDestroyOnLoad(go); // puting this gameobject under the singleton container go.transform.SetParent(SingletonUtil.GetSingletonContainer()); #if UNITY_EDITOR // If we're in Editor simulation mode, we don't need the manifest assetBundle. if (SimulateAssetBundleInEditor) { return(null); } #endif LoadAssetBundle(manifestAssetBundleName, true); var operation = new AssetBundleLoadManifestOperation(manifestAssetBundleName, "AssetBundleManifest", typeof(AssetBundleManifest)); inProgressOperations.Add(operation); return(operation); }
/// <summary> /// Should be called before you use this Singleton GameObject. /// </summary> public static void Initialize() { if (Platform.IsApplicationQuitting) { Debug.LogErrorFormat("Tried initializing a SingletonResource {0} while application was quitting.", typeof(T).Name); return; } if (IsInitialized) { return; } // highly unlikely this would need a lock since all unity calls need to happen on the main thread, but hey, being safe lock (instanceLock) { if (IsInitialized) { return; } T[] objects = GameObject.FindObjectsOfType <T>(); string className = typeof(T).Name; if (objects != null && objects.Length != 0) { Debug.LogErrorFormat(objects[0], "An object of type {0} already exists and wasn't created with Initialize function.", className); } else { // constructing the singleton object instance = GameObject.Instantiate <T>(Resources.Load <T>(className)); if (instance == null) { Debug.LogErrorFormat("Couldn't load Dialog {0}. Is there a resource named \"{0}\" with a component of type {0} in the project?", className); return; } instance.name = className; instance.transform.SetParent(SingletonUtil.GetSingletonContainer()); instance.transform.Reset(); } } }