Skip to content

Instantly share code, notes, and snippets.

@sethillgard
Last active December 13, 2015 22:08
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sethillgard/4982341 to your computer and use it in GitHub Desktop.
#silentkrakenblog Manager systems for unity3d, a new approach
[RequireComponent(typeof(GameManager))]
[RequireComponent(typeof(ScreenManager))]
[RequireComponent(typeof(AudioManager))]
[RequireComponent(typeof(MissionManager))]
public class Managers : MonoBehaviour
{
private static GameManager gameManager;
public static GameManager Game
{
get { return gameManager; }
}
private static ScreenManager screenManager;
public static ScreenManager Screen
{
get { return screenManager; }
}
private static AudioManager audioManager;
public static AudioManager Audio
{
get { return audioManager; }
}
private static MissionManager missionManager;
public static MissionManager Mission
{
get { return missionManager; }
}
// Use this for initialization
void Awake ()
{
//Find the references
gameManager = GetComponent<GameManager>();
screenManager = GetComponent<ScreenManager>();
audioManager = GetComponent<AudioManager>();
missionManager = GetComponent<MissionManager>();
//Make this game object persistant
DontDestroyOnLoad(gameObject);
}
}
Managers.Audio.Play(transform.position, clip);
public class GameManager : MonoBehaviour
{
private GameState currentState;
public GameState State
{
get { return currentState; }
}
//Changes the current game state
public void SetState(System.Type newStateType)
{
if (currentState != null)
{
currentState.OnDeactivate();
}
currentState = GetComponentInChildren(newStateType) as GameState;
if (currentState != null)
{
currentState.OnActivate();
}
}
void Update()
{
if (currentState != null)
{
currentState.OnUpdate();
}
}
void Start()
{
SetState(typeof(GameplayState));
}
}
public abstract class GameState : MonoBehaviour
{
public abstract void OnActivate();
public abstract void OnDeactivate();
public abstract void OnUpdate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment