比如管理器类型(整个场景中只有一个实例)的东西,自身的启动我都放在awake()里面,要访问其他管理器的启动部分就放在start()里面。
比如
public class Controller
{
public static Controller instance;
private void Awake()
{
instance = this;
this.currentChapterIndex = 1;
}
public int currentChapterIndex;
}
public class Cube
{
private Start()
{
if(Controller.instance.currentChapterIndex == 1)
Debug.log("Yes");
else
Debug.log("No");
}
}
这样当场景载入的时候,哪怕这两个东西是一起生成的,都绝对能保证对Controller.instance不会报null的错。
instance的作用主要是避免去写某些静态的方法。因为静态方法中不能包括invoke这种东西,很受伤的。所以只要有了instance这个东西,这个管理器类所有的方法都可以不写成静态的,但在其他任何地方都能够通过instance来调用,很方便。
转载于:https://www.cnblogs.com/cbbcpp/p/3532226.html