Unity uses custom assets (.asset) to configure data

Unity uses custom assets (.asset) to configure data

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

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

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

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

 

 

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);
    }
}

as follows
Write picture description here

Click  Asset to generate a custom resource in the specified path

View the generated resources as follows

Write picture description here

In order to save trouble, add a method directly in the editor class to read custom files

amend as below

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);
        }
    }
}

The print result is as follows
Write picture description here

If you want to customize the Inspector panel of the file, use Bullet.cs the Inspector panel rewritten by the editor class  , the code is as follows

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();
    }

}

Look at the effect again, custom

Write picture description here

At this point, the serialization class is successfully generated and the custom file (.asset) is successfully read, and the Inspector panel is overwritten.

Guess you like

Origin blog.csdn.net/u011105442/article/details/110495214