MonoBehavior lifecycle

awake 只调用一次, awake在所有obj都初始化之后被调用.
      用途:
        初始化游戏状态
          设置脚本间的引用
          

### ExecuteInEditMode 编辑模式下
```
这个模式下,脚本编译,会自动reload

[ExecuteInEditMode]
public class LifecycleTest : MonoBehaviour {}

When new component (this script) is added: Awake(), OnEnable(), Reset(), Start()
When scene is saved (CTRL+S): nothing happens but here you may see those irritating info messages
When scene is unloaded: OnDisable(), OnDestroy()
When scene is loaded: Awake(), OnEnable(), Start()
When script is modified: OnDisable(), OnEnable()
```

Awake - OnDestory
OnEnable - OnDisable

Awake只执行一次, 
OnEnable OnDisable会执行多次(reload script的时候), awake里初始化的实例将会丢失(如果没有重新加载场景的话), 报错NullReferenceExceptions

### 游戏模式
1 编辑状态,什么都不执行
2 点play之后, Awake, OnEnble, Start
3 stop后, OnDisable, Destory



### 泄露
动态创建一个材质(material)后,不添加到任何一个渲染对象身上, 被称为leaked

使用hideFlags
 material.hideFlags = HideFlags.DontSave;
 一定要这样删除
 DestroyImmediate(HideFlags)
 

posted @ 2015-01-20 15:38  summernight  阅读(391)  评论(0编辑  收藏  举报