Scriptableobject mainly implements the preservation of object serialization, because it is Unity's own serialization, so it is much more convenient than Xml,json serialization, but the relative controllability is also relatively poor
1.Editor Write and read test:
usingUnityeditor;usingUnityengine;usingSystem.Collections; Public classscriptabletestwindow:editorwindow{[MenuItem ("scriptabletest/mytest")] Public Static voidCallfunc () {Editorwindow.getwindow<ScriptableTestWindow>(); } voidOngui () {if(Guilayout.button ("Save scriptable Data")) { varMyData = scriptableobject.createinstance<mydata>(); Mydata.myname="Dark"; Mydata.mylevel= -; Assetdatabase.createasset (MyData,"Assets/scriptabletest.asset"); Assetdatabase.saveassets (); } if(Guilayout.button ("Load scriptable Data")) { varMyData = assetdatabase.loadassetatpath<mydata> ("Assets/scriptabletest.asset"); Debug.Log ("MyName:"+mydata.myname); Debug.Log ("mylevel:"+mydata.mylevel); } }}View Code
Read under Editor
2. Run-time read:
usingUnityengine;usingSystem.Collections; Public classruntimetest:monobehaviour{voidStart () {varMyData = resources.load<mydata> ("scriptabletest"); Debug.Log ("name"+mydata.myname); Debug.Log (" Level"+mydata.mylevel); }}
Note that in the Unity editor, the data read by resource can be modified again and saved to prevent accidental operation.
Use of Unity Scriptableobject