Unity3D自定义资源配置文件

。_饼干妹妹 提交于 2019-12-03 15:39:05

配置资源文件估计大家了解很多,比如XML、JSON、Protobuf、Excel、TXT等等

在开发过程中,将游戏数据序列化到配置文件中,项目运行时读取配置文件中的数据

本文另外介绍一个Unity的配置文件(.asset)

该配置文件的优点:

当我们需要将游戏资源里的贴图(Texture)、游戏对象(Gameobject)等预设体保存到配置文件时,这时我们就可以使用该配置文件

但是当关联的预设体丢失时,需要重新将预设体关联起来

接下来,我写了一个Demo,介绍在Unity3D创建自定义配置资源文件点击打开链接

【管理类】

创建一个管理类,提供函数来创建和读取配置文件(.asset)

在这里我已经简单的封装好了,大家下载来后直接使用即可

 

using UnityEngine;
using System.Collections;
using System.IO;
using UnityEditor;

public class CustomConfigManager<T> where T : ScriptableObject//对泛型参数进行约束,使其只能是ScriptableObject的派生类
{

    private static CustomConfigManager<T> _instance;

    public static CustomConfigManager<T> GetInstance
    {
        get
        {
            if(_instance==null)
            {
                _instance=new CustomConfigManager<T>();
            }

            return _instance;
        }
    }

    private CustomConfigManager()
    { 
    }

    /// <summary>
    /// 将类实例化为配置文件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static void CreateAsset()
    {
        //将对象实例化
        ScriptableObject so = ScriptableObject.CreateInstance(typeof(T));

        if (so == null)
        {
            Debug.Log("该对象无效,无法将对象实例化");
            return;
        }

        //自定义配置资源路径
        string path = Application.dataPath + "/CustomConfigFile/MyFile";

        //判断该路径是否存在,不存在的话创建一个
        if (Directory.Exists(path)==false)
        {
            Directory.CreateDirectory(path);
        }


        //配置文件以.asset结尾
        //将对象名配置成与类名相同
        path = string.Format("Assets/CustomConfigFile/MyFile/{0}.asset",typeof(T).ToString());

        //按指定路径生成配置文件
        AssetDatabase.CreateAsset(so,path);
    }


    /// <summary>
    /// 将配置文件转化为对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static void GetAsset()
    {
        //配置文件名与对象名一致
        string name = typeof(T).ToString();
        string path = string.Format("Assets/CustomConfigFile/MyFile/{0}.asset",name);

        // 将配置文件转化为对象
        T obj = AssetDatabase.LoadAssetAtPath<T>(path);
    }
}

 

 

 

 

 

【创建可序列化类(演示)】

 

using UnityEngine;
using System.Collections;
using System;

public enum MyEnum
{
    START,
    PAUSE
}

/// <summary>
/// 该类可以序列化
/// </summary>
[Serializable]
public class CustomConfig : ScriptableObject//继承ScriptableObject
{
    //需要实例化的变量将其设置为public或者设置其属性为[Serializable]
    public MyEnum me = MyEnum.START;

    public Texture interge;

    [Range(0,10)]
    public float f;

    public GameObject obj;
}

 

 

 

 

 

【测试】

创建一个Editor文件夹,将脚本添加到其中

创建好的管理类后,直接调用即可

 

using UnityEngine;
using System.Collections;
using UnityEditor;

public class MySelfText : MonoBehaviour 
{
    [MenuItem("AssetCreateOrGet/Create")]
    public static void Create()
    {
        CustomConfigManager<CustomConfig>.CreateAsset();
    }

    [MenuItem("AssetCreateOrGet/Get")]
    public static void Get()
    {
        CustomConfigManager<CustomConfig>.GetAsset();
    }
}

 

 

 

 

 

点击按钮,成功创建配置文件

Project视图:

Inspector视图:

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!