Unity3d's Scriptableobject study notes

Source: Internet
Author: User

Unlike the serializable serialization feature provided by C #, Scriptableobject is a data storage class provided by Unity3d, and we'll take a look at the functionality of this class next.

Official documents

Http://docs.unity3d.com/Manual/class-ScriptableObject.html

Http://docs.unity3d.com/ScriptReference/ScriptableObject.html

Usage Scenarios

In Unity3d, we record the game's configuration data can use the file (XML, JSON and other formats), can also use binary files (custom format), both methods need to parse themselves, and unity is more intimate, providing us with another format of data logging, Is Scriptableobject.

A simple example

Let's look at a simple example.

First we need to create a class of record configuration, as follows:

Shopconfig, this class is a class that will be packaged into Assetbundle as configuration data, so you must inherit from Scriptableobject, and be aware that the file name must match the class name:

1 usingSystem.Collections.Generic;2 usingUnityengine;3 4 /// <summary>5 ///product configuration table.6 /// </summary>7  Public classShopconfig:scriptableobject8 {9     /// <summary>Ten     ///commodity tab enumeration. One     /// </summary> A      Public enumShoptag -     { - Hot , the Item, - Weapon -     } -  +     /// <summary> -     ///Product list. +     /// </summary> A      PublicList<shoplistinfo>shoplist; at}

Shoplistinfo, this class is shopconfig referenced and packaged into Assetbundle, but it is not packaged as a data type so you do not have to inherit scriptableobject, but you must add the [ System.serializable] Attribute:

1 usingSystem.Collections.Generic;2 3 /// <summary>4 ///Specifies the list of items for the page to sign.5 /// </summary>6 [system.serializable]7  Public classShoplistinfo8 {9     /// <summary>Ten     ///The page sign. One     /// </summary> A      Publicshopconfig.shoptag tag; -  -     /// <summary> the     ///Product list. -     /// </summary> -      PublicList<shopiteminfo>list; -}

Shopiteminfo, ibid.:

1 /// <summary>2 ///Commodities.3 /// </summary>4 [system.serializable]5  Public classShopiteminfo6 {7     /// <summary>8     ///name.9     /// </summary>Ten      Public stringname; One  A     /// <summary> -     ///Price. -     /// </summary> the      Public intPrice ; -}

Below we will create a script for packaging:

1 usingSystem.Collections.Generic;2 usingUnityeditor;3 usingUnityengine;4 5  Public classCreateconfig6 {7[MenuItem ("Tools/createconfig")]8     Private Static voidCreate ()9     {Ten createshopconfig (); One     } A  -     Private Static voidCreateshopconfig () -     { theShopconfig Shopconfig = scriptableobject.createinstance<shopconfig>(); -  -         //populate the data by reading all the data in a common code from externally curated configuration tables such as CSV, XML, JSON, or even binary files -         //This is just a test, just handwritten. (⊙﹏⊙) b +  -Shopconfig.shoplist =NewList<shoplistinfo>(); +  AShoplistinfo list =Newshoplistinfo (); atList.tag =ShopConfig.ShopTag.hot; -List.list =NewList<shopiteminfo>(); -LIST.LIST.ADD (NewShopiteminfo {name ="Gifted your brother underwear", Price =10000 }); -LIST.LIST.ADD (NewShopiteminfo {name ="buckle up dead panties", Price = the }); -LIST.LIST.ADD (NewShopiteminfo {name ="Panties", Price = - }); - shopConfig.ShopList.Add (list); in  -List =Newshoplistinfo (); toList.tag =ShopConfig.ShopTag.item; +List.list =NewList<shopiteminfo>(); -LIST.LIST.ADD (NewShopiteminfo {name ="severings Medicine", Price = - }); theLIST.LIST.ADD (NewShopiteminfo {name ="and co-dispersion", Price = - }); * shopConfig.ShopList.Add (list); $ Panax NotoginsengList =Newshoplistinfo (); -List.tag =ShopConfig.ShopTag.weapon; theList.list =NewList<shopiteminfo>(); +LIST.LIST.ADD (NewShopiteminfo {name ="Xuan Jian", Price =1 }); ALIST.LIST.ADD (NewShopiteminfo {name ="Peach Wood Sword", Price =5 }); theLIST.LIST.ADD (NewShopiteminfo {name ="small Li Fei knife", Price =213 }); +LIST.LIST.ADD (NewShopiteminfo {name ="Big Li Fei knife", Price =313 }); - shopConfig.ShopList.Add (list); $  $         //Once you've filled the data, you're ready to pack in Assetbundle. -         //The first step must be to create a Asset file that holds the configuration data, and the suffix must be Asset -Assetdatabase.createasset (Shopconfig,"Assets/shopconfig.asset"); the  -         //The second step is to use Buildpipeline to packageWuyiBuildpipeline.buildassetbundle (NULL,New[] the             { -Assetdatabase.loadassetatpath ("Assets/shopconfig.asset",typeof(shopconfig)) Wu             }, -Application.streamingassetspath +"/config.assetbundle", Aboutbuildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets |Buildassetbundleoptions.uncompressedassetbundle, $ buildtarget.standalonewindows64); -     } -}

We run it, we can pack out the Assetbundle, here are two points to note:

    1. Classes that inherit from Scriptableobject cannot be created using new, and are created using the scriptableobject.createinstance<t> () method;
    2. The corresponding asset file must be created before it can be packaged, and the suffix of the asset file must be asset, otherwise unity cannot be recognized;

Pack it up, let's get a script to load it up and look at the following:

1 usingUnityengine;2 usingSystem.Collections;3 4  Public classTestscript:monobehaviour5 {6     voidStart ()7     {8Assetbundle Assetbundle = assetbundle.createfromfile (Application.streamingassetspath +"/config.assetbundle");9 TenShopconfig shopconfig = Assetbundle.load ("Shopconfig",typeof(Shopconfig)) asShopconfig; One Debug.Log (shopConfig.ShopList.Count); A     } -}

Just hang on to the camera and we'll see the results:

The data is correct, no problem.

Asset file

Wait, we seem to forget something, the creation of the asset file is what use, we click on the file can be directly edited in the Inspector window!

This is absolutely, planning direct even excel what other configuration tools do not need, with unity can be directly edited and configured, but the only drawback is that the data are out of unity can be used, Other languages such as the background to use the first to understand the asset file data structure (the background can not always write with unity).

Summarize the benefits
    1. In addition to supporting common types such as float, int, string, and other complex data types such as list, it is most important to support Unity's VECTOR3 and other data.
    2. The created file can be edited directly in unity;
Disadvantages
    1. Other languages need to understand the detailed format to parse the data, and take the time to write the parsing code;
    2. For a lot of data or Excel with a bit more comfortable;
    3. If I change the structure of the configuration table, will the data in the asset file be scrapped?
How to use

I think there are two ways to use it:

    1. Use Excel What to configure, Save as CSV, package data when reading CSV table data populate asset file;
    2. Create an empty asset file to edit directly in unity;

Unity3d's Scriptableobject study notes

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.