Unity3D脚本间执行顺序
Unity3D中一个场景有时候需要多个脚本,可以挂在同一物体上执行,也可以挂在不同物体上执行
那么执行顺序是怎样的?我们来测试下
在上个项目基础上,再建一个Test2脚本如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test2 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log('2');
}
// Update is called once per frame
void Update()
{
}
}
把Test2脚本也拖拽到Test物体上。可以看到脚本的挂载顺序是Test、Test2

?然后我们运行一下,脚本是按挂载顺序逆序执行的。然后测试挂载不同物体上也是一样的结果

总结:脚本执行顺序默认是按挂载顺序逆序执行
自定义脚本执行顺序
那我想要自定义脚本执行顺序该怎么办呢?
随便打开一个脚本->Execution Order点击添加test、test2,点击Apply(数值越小越先执行)


?

英文版本的路径:Inspector->Execution Order->MonoManager 设置执行顺序(数值越小越先执行)->Apply