A brief discussion on Unity ScriptableObject

There are many ways to store static configuration data in Unity, such as csv, json, xml, bytes, txt, etc. Unity itself also provides a way to serialize data, ScriptableObject.

In the previous project, csv file reading was always configured, but since unity2020, csv file reading is not very friendly.

Recurring scenario: The csv file contains a decimal point, and the default language of the Android phone is changed to Portuguese. The amazing thing is that it ignores the decimal point and treats it as an integer! ! After switching to Chinese, it could recognize the decimal point, so I thought about changing the data storage method, and ScriptableObject came into being.

code show as below:

[CreateAssetMenu( fileName = "SongConfig", menuName = "Config/SongConfig", order = 3 )]
public class SongConfigScriptableObject : ScriptableObject
{
    private static SongConfigScriptableObject instance = null;

    public static SongConfigScriptableObject Instance
    {
        get
        {
            if( null == instance )
            {
                instance = Resources.Load<SongConfigScriptableObject>( "Config/SongConfig" );
            }

            return instance;
        }
    }

    [SerializeField]
    private SongCSVConfig[] songConfigArr;
    
    private Dictionary<int, SongConfig[]> songConfigDic;   
}

Declare a class that inherits from ScriptableObject, so that you can create the SongConfig.asset file yourself. Next, you need to fill the data in the csv file into the array of the class you created.

Under the editor folder, add a data conversion method:

 [MenuItem( "Tools/ConvertCSV", false, 500 )]
    private static void ConvertCSV()
    {
        string songConfigPath = "Assets/Resources/Config/SongConfig.asset";

        AssetDatabase.DeleteAsset( songConfigPath );

        AssetDatabase.Refresh();

        SongConfigScriptableObject songConfigScriptableObject = CreateInstance<SongConfigScriptableObject>();               

        string[] fileArr = System.IO.Directory.GetFiles( Application.dataPath + "/SongConfig/", "*.csv", System.IO.SearchOption.AllDirectories );

        songConfigScriptableObject.InitConfigList( fileArr.Length );

        for( int i = 0, Imax = fileArr.Length; i < Imax; i++ )
        {
            GenerateSongFile( i, fileArr[i], songConfigScriptableObject );
        }

        AssetDatabase.CreateAsset( songConfigScriptableObject, songConfigPath );

        AssetDatabase.SaveAssets();

        Debug.LogError( "Generate OK" );

        AssetDatabase.Refresh();
    }

There is a point to note here:

1. Be sure to delete the .asset file you created before. If you don’t delete it and fill it directly, when you restart Unity, you will find that your data is not saved in the .asset file.

2. After instantiating the ScriptableObject object, be sure to save it after filling in the data, otherwise you will encounter the same problem as 1.

At this point, the file data reading is completed. ScriptableObject does not support dynamic storage and only supports the reading of static data.

Guess you like

Origin blog.csdn.net/u013748096/article/details/127772525