如下
点击 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 踩