A Unity3D nasty global container for the sake of decoupling.
- Adjust to your own needs.
- Call Glue.Rebind() on Scene change.
| using UnityEngine; | |
| static class Glue | |
| { | |
| public static AudioManager audio; | |
| public static LevelManager level; | |
| public static PrefabManager prefabs; | |
| public static SocialManager social; | |
| public static UIManager ui; | |
| public static UserManager user; | |
| public static System.Random random; | |
| static Glue () | |
| { | |
| random = new System.Random (); | |
| Rebind (); | |
| } | |
| public static void Rebind () | |
| { | |
| audio = TryFind<AudioManager> ("AudioManager"); | |
| level = TryFind<LevelManager> ("LevelManager"); | |
| prefabs = TryFind<PrefabManager> ("PrefabManager"); | |
| social = TryFind<SocialManager> ("SocialManager"); | |
| ui = TryFind<UIManager> ("UIManager"); | |
| user = TryFind<UserManager> ("UserManager"); | |
| } | |
| private static T TryFind<T> (string name) where T: Component | |
| { | |
| GameObject go = GameObject.Find (name); | |
| if (go == null) { | |
| Debug.LogError ("GameObject " + name + " not found in this scene."); | |
| return default(T); | |
| } | |
| T c = go.GetComponent<T> (); | |
| if (c == null) { | |
| Debug.LogError ("GameObject " + name + " doesn't have a " + name + " component."); | |
| return default(T); | |
| } | |
| return c; | |
| } | |
| } |