Unity运行模型
class UnityEnginexxx
{
public static void Main()
{
Init(); //初始化
LoadFirstScene(); //加载首个场景
List<MonoBehaviour> list = new List<MonoBehaviour>();
foreach (GameObject gameobject in scene)
{
var s = gameobject.GetComponents<MonoBehaviour>(); //加载场景中的对象(无序)的组件列表(无序)
if (s != null)
{
list.AddRange(s);
}
}
//awake:初始化内容
foreach (var monoBehaviour in list)
{
monoBehaviour.Awake();
}
//start:引用信息
foreach (var monoBehaviour in list)
{
monoBehaviour.Start();
}
//update:主循环中的每一轮,可以称为一般意义上的“游戏中的一帧"。
while (true)
{
//读取用户输入,进行处理
//动画系统
//开发者逻辑:Engine.logic();
foreach (var monoBehaviour in list)
{
monoBehaviour.Update(); //各组件内部逻辑
}
//渲染
Engine.render();
if(exit) break;
}
exit();
}
}
对于开发者来说,有了游戏引擎,所需要做的事情就是实现游戏引擎暴露出来的各种接口/回调。
logic()和render()可以在两个独立的线程中并发执行,渲染当前帧的同时,计算下一帧.
脚本组件逻辑
public class NewBehaviourScript : MonoBehaviour
{
private int _aa;
[SerializeField] //令私有变量可在unity editor中被调节
[Tooltip("怪物")] //在unity editor中显示的注释
private GameObject _monster;
public NewBehaviourScript xxx; //可在Unity Editor的Inspector窗口中进行拖动赋值
[Tooltip("怪物aa1")]
public GameObject Monster1;
public GameObject Monster2;
void Awake()
{
Transform t = transform.Find("Cell (1)/Cell (5)"); //获取子项的子项的transform组件,层级结构,通过路径定位
bb = t.GetComponent<BoxCollider>(); //实际为t.GameObject.GetComponent<BoxCollider>(); 因为t和t.GameObject的对应关系具有唯一性,所以省略了GameObject
}
public BoxCollider bb;
// Start is called before the first frame update
void Start()
{
Test22(10);
_aa = 10;
int I = 0;
int j = I + 10;
_monster = GameObject.Find("Monster");
}
private void Test22(int i)
{
}
// Update is called once per frame
void Update()
{
//watch
//make decision
//act
transform.position = _monster.transform.position + d;
Monster1.gameObject.transform
}
}