通过定制资源配置文件来管理数据

通过定制资源配置文件来管理数据

即:把所有可序列化的数据容器类放入自定义资源文件中,该资源文件可以在Inspector视图中进行配置,Unity会自动进行序列化、反序列化来读取、保存这些数据在该资源文件中。

使用定制资源配置文件与使用XML、JSON或者其他一些外部文件格式相比有很多好处:

  • 通常,文件大小会更小
  • 不需要自己去序列化,Unity将会掌控所有的序列化和反序列化。
    唯一不好的地方:不小心减小了集合的Size属性后,会丢失数据(不过还好,这里也支持Undo操作,即可以Ctrl+z来撤销,数据可以还原)

步骤1:先定义可序列化的数据容器类

数据容器类将成为资源配置文件的基本数据项,并可在Inspector面板中显示、修改这些数据(序列化与反序列化)
如GameObjectPoolElement.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[Serializable]
public class Enemy {
    public string Name;
    public int Capacity;
    public GameObject Prefab;
    [NonSerialized]
    private List<GameObject> goList = new List<GameObject>();
}
GameObjectPoolElement的管理类,GameObjectPool.cs
using UnityEngine;
using System.Collections.Generic;

public class GameObjectPool : ScriptableObject
{
    public List<GameObjectPoolElement> GOPoolElement;
}

步骤2:扩展编辑器

用于创建定制资源配置文件的工具方法容器类:CustomAssetUtility.cs
注意:资源配置文件必须以.asset为扩展名,否则编辑器无法识别

using UnityEngine;
using UnityEditor;
using System.IO;
public class CustomAssetUtility
{
    //创建一个指定类型的定制资源配置文件
    public static void CreateAsset<T>() where T : ScriptableObject
    {
        T asset = ScriptableObject.CreateInstance<T>();
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        if (path==string.Empty)
        {
            path = "Assets";
        }
        else if (Path.GetExtension(path)!=string.Empty) //如果扩展名不为空
        {
            path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), string.Empty);
        }
        //存放路径+文件全名,把生成的资源文件放于Resources文件夹是为了打包后进行压缩,使用时便于读取,但也可以放在其它地方
        string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/Resources/ " + typeof(T).ToString() + ".asset");
        AssetDatabase.CreateAsset(asset, assetPathAndName);
        AssetDatabase.SaveAssets();
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = asset;
    }
}

扩展编辑器类:CreatMenu.cs

using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
public class CreatMenu {
    [MenuItem("MyTools/CreateAssets/GameObjectPool")]
    public static void CreateAsset()
    {
        CustomAssetUtility.CreateAsset<GameObjectPool>();
    }
}

步骤3:使用定制资源配置文件

1.点击编辑器主菜单上的MyTools/CreateAssets/GameObjectPool来创建一个定制资源配置文件。
2.在Inspector面板中可以对它进行配置修改,任何改动都会被自动保存到该文件中(Unity会自动执行序列化操作来保存)。
3.定制资源配置文件现在成为了一个资源文件,可以像使用其它资源文件一样使用它。
如:把该文件直接赋值给GameObjectPool类型的变量,如:

using UnityEngine;
using System.Collections.Generic;
public class test : MonoBehaviour {
    //方式1:直接拖拽赋值
    //public GameObjectPool goPool1;
    //方式2:代码读取资源
    private GameObjectPool goPool2;
    void Awake()
    {
        goPool2 = Resources.Load<GameObjectPool>("GameObjectPool");
    }
    void Start()
    {
        List<GameObjectPoolElement> goPoolElementList = goPool2.GOPoolElement;
        Debug.Log(goPoolElementList[0].Name);
        Debug.Log(goPoolElementList[0].Capacity);
        Debug.Log(goPoolElementList[0].Prefab);
    }
}
原文地址:https://www.cnblogs.com/susufufu/p/6912350.html