【Unity】ScriptableObject 在游戏中的使用实例

ScriptableObject 使用指南

Unity 存储游戏数据的几种方法

Unity ScriptableObject实例

创建一个物品管理的ScriptableObject

  1. ItemScriptableObject
    目的:在 project 面板中添加 Item Scriptable Object 的实例文件。
    请添加图片描述
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 模型数据、数据模型
/// </summary>

[CreateAssetMenu]
public class ItemScriptObject : ScriptableObject
{
    
    
    public int id;
    public new string name;
    public ItemType itemType;
    public string descirption;
    
    public List<Property> propertyList;
    public Sprite icon;
    public GameObject prefab;


}

public enum ItemType
{
    
    
    Weapon,
    Consumable
}


[Serializable]//标识一个类可以序列化(序列化就是可以做数据存储)
public class Property
{
    
    
    public PropertyType proPertyType;//对象
    public int value;//具体值的大小
    
    public Property()
    {
    
    

    }

    public Property(PropertyType proPertyType, int value)
    {
    
    
        this.proPertyType = proPertyType;//标签
        this.value = value;//值
    }   
}

public enum PropertyType
{
    
    
    HPVValue,
    EnergyValue,
    MentalValue,
    SpeedValue,
    AttackValue

}
  • 最终效果:
    在这里插入图片描述

创建一个管理所有 ScriptableObject 的数据库(ItemDBSO)

请添加图片描述

  • ItemDBSO脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 管理所有SO的数据,相当于数据库
/// </summary>


[CreateAssetMenu()]
public class ItemDBSO : ScriptableObject
{
    
    
    public List<ItemScriptObject> itemList;
}

  • 最终效果
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_61561568/article/details/137776208