#silentkrakenblog Manager systems for unity3d, a new approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Managers.Audio.Play(transform.position, clip); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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