Unity Editor Scripting

1 Scriptable Object
通常不單獨使用
通常在編輯器開發中用到(使用IMUI方式)
是數據的容器
和monoBehaviour(用於GameObject)平行存在,但是不能用於GameObject
實例化的scriptableObject可以和assets一樣保存在project裏
代替3方的數據格式,適應於大量數據,靈活裝載
2個用途:editor session 存數據、runtime 存數據

使用scriptableobject:
1 constructor(注意,在deserialized之前)
2 deserialized(這裏會覆蓋之前的數據)
3 onenable(這裏可以初始化數據)

可以使用attribute([CreateAssetMenu(fileName=”xxx”,menuName=”ooo”,order=111)])
或ScriptableObject.createinstance()來實例化
Mysoc myso = ScriptableObject.createinstance();
AssetDatabase…….

2 serialization
Unity需要在c++ core和script side來傳數據(使用serialized data)
關於serialization,存儲編碼後數據,使用unity自己的方式。每次進入和退出play mode都要把serializable的數據通過c++存一遍再重新裝載一遍,包括Dlls,最後再呈現在script side。這也是爲什麼play mode沒有存儲改變。

2個attribute來指明serialize:
serializable
標識class或struct
serializeField
標識private字段

2個陷阱:
shared references
會被序列化成“值”。所以存多次。可能丟失shared references。
這裏使用scriptable object可以正確的存儲一個reference。

shutting down the editor
lose data。
這裏使用scriptable object,可以把數據像asset一樣存下了。

3 inventory
1 class item,名字呀,texture2d呀什麼的,這個要serializable的attributes
2 class itemlist, 這個要繼承scriptableobject
3 class createitemlist,建立這個list的asset實例,提供create()
4 class itemeditor,繼承editorwindow,這裏調用create()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章