Currently I am developing my own Unity project. While my project become bigger, I started to realize how horrible it would be to allow each component having their own life cycle.
For example, I don't know if a component needs to fetch for another component that only initial in Start method, whereas this component raises up this call in awake method. It would be a nightmare.
So I created an interface called IController, and having all the controlling components inherent from it with overriding parent method. Then I manage them in a centrol controller. It looks like this:
Public class CentrolController{
Public NPCController npcController;
Public MusicController musicController;
Public CharacterController characterController;
Public List<IController> controllers;
Public void Awake(){
if (npcController != null)
controllers.Add(npcController);
if (musicController !=null)
controllers.Add(musicController);
if (characterController != null)
controllers.Add(characterController);
}
Public void Update(){
foreach (IController controller in controllers)
{
controller.Update();
}
}
}
I add a reference for each controller because I want to assign them through the inspector. However it seems a little stupid because I need to change the awake method every time I add a new controller to the system, at the same time violating open close principal.
What should I do to make the script scalable?
Aucun commentaire:
Enregistrer un commentaire