利用ScriptableObject在U3D里优雅地实现自定义配置文件
2017-11-06 11:08
316 查看
因为我本人也是一个初学者,所以对于一些简单的设定相关的游戏内容时,为了将数据持久化,需要自己做轮子觉得很麻烦。
偶然看到ScriptableObject这么一个类,总算看到了一倒救星,原来在U3D里,并不是必须讲脚本挂载以实现实例化,存在着这么一个类,让我们可以在代码里实例化,又可以去调用U3D的一些API。
(http://blog.csdn.net/liqiangeastsun/article/details/50510615这是原文链接,感谢
首先是数据类。
// 创建一个可序列化的子弹类 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;
/// <summary>
/// 子弹速度
/// </summary>
public int speed = 10;
// 伤害数值
public int damage = 5;
// 子弹关联的特效
public GameObject effectObj;
}
为了能够让团队里其他人,可以在程序不在的时候预览到配置文件,再把inpector面板也修改成中文,毕竟这是我们的母语,瞄一眼就能很快理解。
偶然看到ScriptableObject这么一个类,总算看到了一倒救星,原来在U3D里,并不是必须讲脚本挂载以实现实例化,存在着这么一个类,让我们可以在代码里实例化,又可以去调用U3D的一些API。
(http://blog.csdn.net/liqiangeastsun/article/details/50510615这是原文链接,感谢
强哥的私房菜
博主为我这样的小白带来便利)
感觉豁然开朗,大惊以前的自己是那么蠢,百度以后做了一个测试工程。如下。首先是数据类。
// 创建一个可序列化的子弹类 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;
/// <summary>
/// 子弹速度
/// </summary>
public int speed = 10;
// 伤害数值
public int damage = 5;
// 子弹关联的特效
public GameObject effectObj;
}
然后是拓展编辑器。
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) 路径 string path2 = string.Format("Assets/BulletAeeet/{0}.asset", (typeof(Bullet).ToString())); // 生成自定义资源到指定路径 AssetDatabase.CreateAsset(bullet, path2); } [MenuItem("ReadAsset/GetBulletAsset")] 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); } } }
为了能够让团队里其他人,可以在程序不在的时候预览到配置文件,再把inpector面板也修改成中文,毕竟这是我们的母语,瞄一眼就能很快理解。
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 GU 4000 IContent("伤害数值")); 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(); } }
相关文章推荐
- net利用SMTP发邮件,配置文件(web.config)实现邮件发送的相关配置
- 利用配置文件实现SQL-Server与Oralce数据库访问类的转换
- 读取配置文件和自定义配置文件(python实现)
- 读取配置文件和自定义配置文件(python实现)
- C#读取配置文件详解 完全自定义实现动态加载dll
- 利用XML序列化实现程序配置文件
- 一步一步SharePoint 2007之四十三:实现自定义Workflow(2)——设置配置文件
- 利用samba实现Ubuntu和win7文件共享配置过程
- 利用java反射机制 读取配置文件 实现动态类加载以及动态类型转换
- 利用正则表达式进行修改自定义ini配置文件
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
- 重构实践——利用配置文件实现设计的高度抽象
- 通过修改 Apache 的配置文件 htaccess 文件实现自定义404页面
- 在python中实现自定义配置文件解析及应用技巧[yaml and ConfigParser] 推荐
- android 自定义ListView 实现 弹出自定义对话框(带EditText)实现 配置文件实现
- 利用XML序列化和Asp.Net Web缓存实现站点配置文件
- 一步一步SharePoint 2007之四十三:实现自定义Workflow(2)——设置配置文件
- 利用samba实现Ubuntu和win7文件共享配置过程
- 实现自定义Workflow(2):设置配置文件
- 利用配置文件实现可选风格下拉菜单