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

  • 最终效果
    在这里插入图片描述
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值