Unity 使用自定义资源(.asset)配置数据

    xiaoxiao2021-04-02  34

    在游戏开发中,经常会用到一些配置文件保存一些数据,然后项目运行中读取这些配置文件中的数据在游戏中使用。 如:配置血条:根据角色类型(人物、动物、怪物等)配置不同的血条,包括血条大小,血条名或血条预设,血条颜色等一些简单数据。 如:配置子弹:子弹类型(真子弹、假子弹、追踪子弹等),子弹速度,伤害数值,子弹关联的特效等。 诸如此类的配置很多种,可创建一个可序列化的类存储数据,或者创建 XML 、JSON 文件保存数据,创建 Excel 文件,创建 TXT 文件,皆可完成需求,灵活使用这些方法保存配置数据。 在此介绍一下使用可序列化类保存配置,并且将可序列化类保存成Unity的自定义文件(.asset),然后配置自定义文件(.asset)。 优点: 可以保存数据类型多样(int、string、Vector3、GameObject、Transform、Texture等)如关联预设,关联图片等资源数据,而XML、TXT等只能保存(int、string、Vector3 等基本数据类型)。 缺点: 如果配置数据中保存了(GameObject、Texture)等资源数据,当关联的资源被删除时,配置数据将丢失,需要重新将新的资源再次关联到配置数据上。 下面做个简单的子弹配置数据 // 创建一个可序列化的子弹类 Bullet.CS using UnityEngine; using System.Collections; using System; // 子弹类型枚举 public enum BulletType { DirectAttack = 0, // 直接攻击 Phony, // 假子弹 Real, // 真子弹 Track, // 追踪子弹 } /// <summary> /// 可序列化 /// </summary> [Serializable] public class Bullet : ScriptableObject { // Bullet 类直接继承自 ScriptableObject // 子弹类型 public BulletType bulletType = BulletType.DirectAttack; // 子弹速度 public int speed = 10; // 伤害数值 public int damage = 5; // 子弹关联的特效 public GameObject effectObj; } 123456789101112131415161718192021222324252627282930313233343536 123456789101112131415161718192021222324252627282930313233343536 创建一个编辑器类,在 菜单栏添加功能按钮,生成自定义资源 using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class CreateAsset : Editor { // 在菜单栏创建功能项 [MenuItem("CreateAsset/Asset")] static void Create() { // 实例化类 Bullet ScriptableObject bullet = ScriptableObject.CreateInstance<Bullet>(); // 如果实例化 Bullet 类为空,返回 if (!bullet) { Debug.LogWarning("Bullet not found"); return; } // 自定义资源保存路径 string path = Application.dataPath + "/BulletAeeet"; // 如果项目总不包含该路径,创建一个 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //将类名 Bullet 转换为字符串 //拼接保存自定义资源(.asset) 路径 path = string.Format("Assets/BulletAeeet/{0}.asset", (typeof(Bullet).ToString())); // 生成自定义资源到指定路径 AssetDatabase.CreateAsset(bullet, path); } } 123456789101112131415161718192021222324252627282930313233343536373839 123456789101112131415161718192021222324252627282930313233343536373839

    如下 

    点击 Asset 在指定路径生成自定义资源

    查看生成资源如下

    为了省事,下面直接在编辑器类中加一个方法,读取自定义文件

    修改如下

    using UnityEngine; using System.Collections; using UnityEditor; using System.IO; using System; public class CreateAsset : Editor { [MenuItem("CreateAsset/Asset")] static void Create() { // 实例化类 Bullet ScriptableObject bullet = ScriptableObject.CreateInstance<Bullet>(); // 如果实例化 Bullet 类为空,返回 if (!bullet) { Debug.LogWarning("Bullet not found"); return; } // 自定义资源保存路径 string path = Application.dataPath + "/BulletAeeet"; // 如果项目总不包含该路径,创建一个 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //将类名 Bullet 转换为字符串 //拼接保存自定义资源(.asset) 路径 path = string.Format("Assets/BulletAeeet/{0}.asset", (typeof(Bullet).ToString())); // 生成自定义资源到指定路径 AssetDatabase.CreateAsset(bullet, path); } [MenuItem("CreateAsset/GetAsset")] static void GetAsset() { //读取 .asset 文件, 直接转换为 类 Bullet Bullet bullet = AssetDatabase.LoadAssetAtPath<Bullet>("Assets/BulletAeeet/Bullet.asset"); // 打印保存的数据 Debug.Log("BulletType :" + Enum.GetName(typeof(BulletType), bullet.bulletType)); Debug.Log("Speed :" + bullet.speed); Debug.Log("damage :" + bullet.damage); if (bullet.effectObj) { Debug.Log("EffectObj :" + bullet.effectObj.name); } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657

    打印结果如下 

    如果想自定义 文件的 Inspector面板,使用编辑器类重写 Bullet.cs 的Inspector面板, 代码如下

    using UnityEngine; using System.Collections; using UnityEditor; [CustomEditor(typeof(Bullet))] public class BulletInspector : Editor { // 子弹类型 public SerializedProperty bulletType; // 子弹速度 public SerializedProperty speed; // 伤害数值 public SerializedProperty damage; // 子弹关联的特效 public SerializedProperty effectObj; private void OnEnable() { bulletType = serializedObject.FindProperty("bulletType"); speed = serializedObject.FindProperty("speed"); damage = serializedObject.FindProperty("damage"); effectObj = serializedObject.FindProperty("effectObj"); } public override void OnInspectorGUI() { serializedObject.Update(); EditorGUI.indentLevel = 1; EditorGUILayout.PropertyField(bulletType, new GUIContent("子弹类型")); GUILayout.Space(5); EditorGUILayout.PropertyField(speed, new GUIContent("子弹速度")); GUILayout.Space(5); EditorGUILayout.PropertyField(damage, new GUIContent("伤害数值")); GUILayout.Space(5); EditorGUILayout.PropertyField(effectObj, new GUIContent("特效对象")); GUILayout.Space(10); // 打印数据 if (GUILayout.Button("Debug")) { Debug.Log("bulletType :" + (BulletType)bulletType.enumValueIndex); Debug.Log("speed :" + speed.intValue); Debug.Log("damage :" + damage.intValue); if (effectObj.objectReferenceValue) { Debug.Log("effectObj :" + effectObj.objectReferenceValue); } } if (GUI.changed) { EditorUtility.SetDirty(target); } serializedObject.ApplyModifiedProperties(); } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869

    再看效果,自定义的

    到此将序列化类生成自定义文件 (.asset)成功,读取成功,重写Inspector面板结束。

    顶 1 踩
    转载请注明原文地址: https://ju.6miu.com/read-665849.html

    最新回复(0)