Use of Unity Scriptableobject

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.