分享

Unity3D自定义资源配置文件

 牧牧彡 2017-06-13

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

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

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

该配置文件的优点:

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

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

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

【管理类】

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

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

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4. using UnityEditor;  
  5.   
  6. public class CustomConfigManager<T> where T : ScriptableObject//对泛型参数进行约束,使其只能是ScriptableObject的派生类  
  7. {  
  8.   
  9.     private static CustomConfigManager<T> _instance;  
  10.   
  11.     public static CustomConfigManager<T> GetInstance  
  12.     {  
  13.         get  
  14.         {  
  15.             if(_instance==null)  
  16.             {  
  17.                 _instance=new CustomConfigManager<T>();  
  18.             }  
  19.   
  20.             return _instance;  
  21.         }  
  22.     }  
  23.   
  24.     private CustomConfigManager()  
  25.     {   
  26.     }  
  27.   
  28.     /// <summary>  
  29.     /// 将类实例化为配置文件  
  30.     /// </summary>  
  31.     /// <typeparam name="T"></typeparam>  
  32.     public static void CreateAsset()  
  33.     {  
  34.         //将对象实例化  
  35.         ScriptableObject so = ScriptableObject.CreateInstance(typeof(T));  
  36.   
  37.         if (so == null)  
  38.         {  
  39.             Debug.Log("该对象无效,无法将对象实例化");  
  40.             return;  
  41.         }  
  42.   
  43.         //自定义配置资源路径  
  44.         string path = Application.dataPath + "/CustomConfigFile/MyFile";  
  45.   
  46.         //判断该路径是否存在,不存在的话创建一个  
  47.         if (Directory.Exists(path)==false)  
  48.         {  
  49.             Directory.CreateDirectory(path);  
  50.         }  
  51.   
  52.   
  53.         //配置文件以.asset结尾  
  54.         //将对象名配置成与类名相同  
  55.         path = string.Format("Assets/CustomConfigFile/MyFile/{0}.asset",typeof(T).ToString());  
  56.   
  57.         //按指定路径生成配置文件  
  58.         AssetDatabase.CreateAsset(so,path);  
  59.     }  
  60.   
  61.   
  62.     /// <summary>  
  63.     /// 将配置文件转化为对象  
  64.     /// </summary>  
  65.     /// <typeparam name="T"></typeparam>  
  66.     public static void GetAsset()  
  67.     {  
  68.         //配置文件名与对象名一致  
  69.         string name = typeof(T).ToString();  
  70.         string path = string.Format("Assets/CustomConfigFile/MyFile/{0}.asset",name);  
  71.   
  72.         // 将配置文件转化为对象  
  73.         T obj = AssetDatabase.LoadAssetAtPath<T>(path);  
  74.     }  
  75. }  


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

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System;  
  4.   
  5. public enum MyEnum  
  6. {  
  7.     START,  
  8.     PAUSE  
  9. }  
  10.   
  11. /// <summary>  
  12. /// 该类可以序列化  
  13. /// </summary>  
  14. [Serializable]  
  15. public class CustomConfig : ScriptableObject//继承ScriptableObject  
  16. {  
  17.     //需要实例化的变量将其设置为public或者设置其属性为[Serializable]  
  18.     public MyEnum me = MyEnum.START;  
  19.   
  20.     public Texture interge;  
  21.   
  22.     [Range(0,10)]  
  23.     public float f;  
  24.   
  25.     public GameObject obj;  
  26. }  


【测试】

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

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

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEditor;  
  4.   
  5. public class MySelfText : MonoBehaviour   
  6. {  
  7.     [MenuItem("AssetCreateOrGet/Create")]  
  8.     public static void Create()  
  9.     {  
  10.         CustomConfigManager<CustomConfig>.CreateAsset();  
  11.     }  
  12.   
  13.     [MenuItem("AssetCreateOrGet/Get")]  
  14.     public static void Get()  
  15.     {  
  16.         CustomConfigManager<CustomConfig>.GetAsset();  
  17.     }  
  18. }  


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

Project视图:

Inspector视图:



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

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

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

该配置文件的优点:

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

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

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

【管理类】

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

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

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.IO;  
  4. using UnityEditor;  
  5.   
  6. public class CustomConfigManager<T> where T : ScriptableObject//对泛型参数进行约束,使其只能是ScriptableObject的派生类  
  7. {  
  8.   
  9.     private static CustomConfigManager<T> _instance;  
  10.   
  11.     public static CustomConfigManager<T> GetInstance  
  12.     {  
  13.         get  
  14.         {  
  15.             if(_instance==null)  
  16.             {  
  17.                 _instance=new CustomConfigManager<T>();  
  18.             }  
  19.   
  20.             return _instance;  
  21.         }  
  22.     }  
  23.   
  24.     private CustomConfigManager()  
  25.     {   
  26.     }  
  27.   
  28.     /// <summary>  
  29.     /// 将类实例化为配置文件  
  30.     /// </summary>  
  31.     /// <typeparam name="T"></typeparam>  
  32.     public static void CreateAsset()  
  33.     {  
  34.         //将对象实例化  
  35.         ScriptableObject so = ScriptableObject.CreateInstance(typeof(T));  
  36.   
  37.         if (so == null)  
  38.         {  
  39.             Debug.Log("该对象无效,无法将对象实例化");  
  40.             return;  
  41.         }  
  42.   
  43.         //自定义配置资源路径  
  44.         string path = Application.dataPath + "/CustomConfigFile/MyFile";  
  45.   
  46.         //判断该路径是否存在,不存在的话创建一个  
  47.         if (Directory.Exists(path)==false)  
  48.         {  
  49.             Directory.CreateDirectory(path);  
  50.         }  
  51.   
  52.   
  53.         //配置文件以.asset结尾  
  54.         //将对象名配置成与类名相同  
  55.         path = string.Format("Assets/CustomConfigFile/MyFile/{0}.asset",typeof(T).ToString());  
  56.   
  57.         //按指定路径生成配置文件  
  58.         AssetDatabase.CreateAsset(so,path);  
  59.     }  
  60.   
  61.   
  62.     /// <summary>  
  63.     /// 将配置文件转化为对象  
  64.     /// </summary>  
  65.     /// <typeparam name="T"></typeparam>  
  66.     public static void GetAsset()  
  67.     {  
  68.         //配置文件名与对象名一致  
  69.         string name = typeof(T).ToString();  
  70.         string path = string.Format("Assets/CustomConfigFile/MyFile/{0}.asset",name);  
  71.   
  72.         // 将配置文件转化为对象  
  73.         T obj = AssetDatabase.LoadAssetAtPath<T>(path);  
  74.     }  
  75. }  


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

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System;  
  4.   
  5. public enum MyEnum  
  6. {  
  7.     START,  
  8.     PAUSE  
  9. }  
  10.   
  11. /// <summary>  
  12. /// 该类可以序列化  
  13. /// </summary>  
  14. [Serializable]  
  15. public class CustomConfig : ScriptableObject//继承ScriptableObject  
  16. {  
  17.     //需要实例化的变量将其设置为public或者设置其属性为[Serializable]  
  18.     public MyEnum me = MyEnum.START;  
  19.   
  20.     public Texture interge;  
  21.   
  22.     [Range(0,10)]  
  23.     public float f;  
  24.   
  25.     public GameObject obj;  
  26. }  


【测试】

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

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

[csharp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEditor;  
  4.   
  5. public class MySelfText : MonoBehaviour   
  6. {  
  7.     [MenuItem("AssetCreateOrGet/Create")]  
  8.     public static void Create()  
  9.     {  
  10.         CustomConfigManager<CustomConfig>.CreateAsset();  
  11.     }  
  12.   
  13.     [MenuItem("AssetCreateOrGet/Get")]  
  14.     public static void Get()  
  15.     {  
  16.         CustomConfigManager<CustomConfig>.GetAsset();  
  17.     }  
  18. }  


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

Project视图:

Inspector视图:



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多