首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Unity 使用自定义资源配置数据

在游戏开发中,经常会用到一些配置文件保存一些数据,然后项目运行中读取这些配置文件中的数据在游戏中使用。

如:配置血条:根据角色类型(人物、动物、怪物等)配置不同的血条,包括血条大小,血条名或血条预设,血条颜色等一些简单数据。

如:配置子弹:子弹类型(真子弹、假子弹、追踪子弹等),子弹速度,伤害数值,子弹关联的特效等。

诸如此类的配置很多种,可创建一个可序列化的类存储数据,或者创建 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, // 追踪子弹

}

///

/// 可序列化

///

[Serializable]

public class Bullet : ScriptableObject {

// Bullet 类直接继承自 ScriptableObject

// 子弹类型

public BulletType bulletType = BulletType.DirectAttack;

// 子弹速度

public int speed = 10;

// 伤害数值

public int damage = 5;

// 子弹关联的特效

public GameObject effectObj;

}

创建一个编辑器类,在 菜单栏添加功能按钮,生成自定义资源

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 类为空,返回

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/.asset", (typeof(Bullet).ToString()));

// 生成自定义资源到指定路径

AssetDatabase.CreateAsset(bullet, path);

}

}

如下

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

查看生成资源如下

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

修改如下

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 类为空,返回

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/.asset", (typeof(Bullet).ToString()));

// 生成自定义资源到指定路径

AssetDatabase.CreateAsset(bullet, path);

}

[MenuItem("CreateAsset/GetAsset")]

static void GetAsset()

{

//读取 .asset 文件, 直接转换为 类 Bullet

Bullet bullet = AssetDatabase.LoadAssetAtPath("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);

}

}

}

打印结果如下

如果想自定义 文件的 Inspector面板,使用编辑器类重写的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();

}

}

再看效果,自定义的

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

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190820A008CV00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券