Skip to content

Instantly share code, notes, and snippets.

@fcingolani
Last active August 29, 2015 14:23
Show Gist options
  • Save fcingolani/4460a59871d5294ba1de to your computer and use it in GitHub Desktop.
Save fcingolani/4460a59871d5294ba1de to your computer and use it in GitHub Desktop.
Glue.cs - A Unity3D nasty global container for the sake of decoupling.

Glue.cs

A Unity3D nasty global container for the sake of decoupling.

  1. Adjust to your own needs.
  2. 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment