# Egret in Unity Like ## Main Script #### cBigBrother 控管所有 cComponent 的 Start, Update, FixedUpdate 的呼叫時機 --- #### cComponent 類似 Unity MonoBehaviour,依附於 GameObject 主要特性為被 cBigBrother, cGameObject 自動呼叫的 functions <font color = #FF0000> 如:Awake, OnEnable, Start, Update, FixedUpdate, OnDisable, OnDestroy </font> --- #### cGameObject 類似 Unity GameObject,可掛載多個 cComponent 的實體對象,有父子節點的概念 當父物件被移除,其所有子物件將被一併移除 --- ## Functions in cComponent #### Awake 當物件第一次 active 時呼叫 注意:系統自動觸發此方法後,會將此方法設為 undefined --- #### Start 當物件第一次 active 後的下一個 frame 時呼叫 <font color = #FF0000> 注意:即比 Awake 晚一個 frame </font> --- #### OnEnable: 當物件每次從 inactive 轉變成 active 時呼叫 <font color = #FF0000> 注意:與 Awake 同一個 frame,但比 Awake 晚 </font> --- #### OnDisable: 當物件每次從 active 轉變成 inactive 時呼叫 <font color = #FF0000> 注意:destroy cGameObject 時並不會觸發 cComponent 的 OnDisable </font> ## Relationship with Egret DisplayObject 1. 可為 cGameObject 設定 egret.DisplayObject,藉此透過 cGameObject 與其依附的 cComponent 與 DisplayObject 互動,如:cGameObject 的 SetActive 除影響 cComponent 是否運行之外,也會直接影響 DisplayObject 是否可見 2. cGameObject 的父子關係,也代表著其 DisplayObject 的深度關係,子 < 父 (z-depth),而彼此的 DisplayObject 在 eui 體系下,也是明確的父子關係 ## Prefab ```typescript= // 準備 prefab let com = new egret.DisplayObjectContainer(); let prefab = new cGameObject('prefabName'); prefab.SetDisplayObject(com); prefab.SetActive(false); // ex. (UI.cUIButton, new UI.cUIButton(this.m_GroupName)) prefab.AddComponent_AlreadyNew(ComponentType, Component實體); // 使用 prefab let obj = Pool.Spawn(prefab, 'objName'); ``` ## cBE_Switch 看似類似 Unity Button 的 spriteState 功能,可以根據按鈕狀態切換圖片,有以下狀態可以設定圖片 * Down * Disable * Enter * Out * Enable cBE_Switch 必須在 UI.cUIButton 之前事先 AddComponent 到 cGameObject 上,因為 cUIButton 會在 Awake 尋找底下全部的 ButtonEffect,並事先存放起來,而 ButtonEffect 就是 cBE_Switch 的 function ## Comparison | | Egret View | Unity Like | | -------- | -------- | -------- | | 物件綁定 | 直接由id綁定到同名變數 | 需要寫Script透過GetChild設定 | | Trace | 正常 | 無法(因為透過eval呼叫) | | -------- | -------- | -------- | ###### tags: `教學`