Use ScriptableObject to create a card memory

In the previous section, we learned about the usage of ScriptableObject. In this section, let's implement a card memory.
I used the design and material of the game Killing Spire that I really like {{xieyanxiao}}.


design concept

I do n’t know if you have played the Killing Spire. The example cards are as follows.
Insert picture description here
We can see a card including 名称, 消耗, 类型, 描述, 卡牌背景, 卡牌底图and other parts, but also in the game, the player's attributes can affect the strength of the cards, such as the power of the player's cards can upgrade the damage, the player's agility can Increase how much armor the player gets.
I followed the design of the Killing Spire and made the following card editor. It realized that the data was stored visually and the card strength was linked to the player. Using this memory, you can create cards with arbitrary data.

Design thinking

Cards should have both additive and non-addable values. In the design, I will use capital letters to represent additive values, and lowercase letters to represent non-additive values.
The bonus value needs to be extracted from the player Asset.
Regardless of whether it is an additive or non-additive placeholder, it will be replaced with a specific value when it is used.

Effect preview

Insert picture description here
The card memory can be divided into three parts: material, data, and processing from top to bottom.

  • Material: Card surface, type, quality, etc. of the storage card.
  • Data: The name of the storage card, ability consumption, expression, description, basic data, player bonus data, etc.
  • Processing: read data and load player data.

Code

In the project, I tried NaughtyAttributesto do further processing on the view panel data. For detailed NaughtyAttributes, please check github .

PlayerAsset.cs

using System.Collections.Generic;
using System.Linq;
using NaughtyAttributes;
using UnityEngine;

namespace Asset
{
    [CreateAssetMenu(fileName = "NEW PlayerAsset", menuName = "MyAsset/PlayerAsset", order = 0)]
    public class PlayerAsset : ScriptableObject
    {

        public int maxHp;
        public int currentHp;
        public int strength;
        public int agility;
        private static PlayerAsset _instance;
        public static PlayerAsset Instance
        {
            get
            {
                if (!_instance)
                {
                   _instance = Resources.FindObjectsOfTypeAll<PlayerAsset>().FirstOrDefault();;
                }
                if (_instance) return _instance;
                _instance= CreateInstance<PlayerAsset>();
                _instance.hideFlags = HideFlags.DontSave;

                return _instance;
            }
            private set => _instance = value;
        }

    }
}

CardAsset.cs

using System;
using System.Collections.Generic;
using System.Linq;
using NaughtyAttributes;
using UnityEngine;
using UnityEngine.Serialization;

namespace Asset
{
    /// <summary>
    /// 卡牌类型
    /// </summary>
    public enum CardType
    {
        Attack,
        Skill,
    }

    public enum CardQuality
    {
        Write,
        Blue,
        Yellow
    }

    [Serializable]
    public class DictData
    {
        public char key;
        public int data;

        public DictData(char key,int data)
        {
            this.key = key;
            this.data = data;
        }
    }

    [CreateAssetMenu(fileName = "NewCard", menuName = "MyAsset/CardAsset", order = 0)]
    public class CardAsset : ScriptableObject
    {
        [BoxGroup("材质")] public CardType cardType;
        [BoxGroup("材质")] public CardQuality cardQuality;
        [BoxGroup("材质")] [ShowAssetPreview()] public Sprite cardSprite;

        [BoxGroup("数据")] [MinValue(0), MaxValue(5), Tooltip("消耗")]
        public int expend;

        [BoxGroup("数据")] public string title;

        [BoxGroup("数据")] [ResizableTextArea] public string express;
        [BoxGroup("数据")] [ResizableTextArea] public string desc;

        [BoxGroup("数据"),Label("基础数据")] [ReorderableList] public DictData[] dictData;
        [BoxGroup("数据"),Label("玩家加成数据")] [ReorderableList] public List<DictData> dictAddData;
        /// <summary>
        /// 写入配置
        /// </summary>
        [Button("写入配置")]
        public void InitCardAsset()
        {
            desc = string.Empty;
            //字典方式
            foreach (var i in express)
            {
                var currChar = i + "";
                foreach (var t in dictData)
                {
                    if (!t.key.Equals(i)) continue;

                    var addValue = 0;    //加成值
                    //判断该数据是否需要被加成
                    if (t.key>='A'&&t.key<='Z')
                    {
                        addValue = dictAddData.FirstOrDefault(m => m.key == t.key).data;
                    }
                    currChar = t.data+addValue + "";
                    break;
                }

                desc += currChar;
            }
        }
        /// <summary>
        /// 更新卡牌
        /// </summary>
        [Button("写入玩家加成数据")]
        public void UpdateCard()
        {
            dictAddData.Clear();
            dictAddData.Add(new DictData('X',PlayerAsset.Instance.strength));
            dictAddData.Add(new DictData('Y',PlayerAsset.Instance.agility));
            //X-玩家力量 Y-玩家敏捷
 Debug.Log(PlayerAsset.Instance.maxHp+"\t"+PlayerAsset.Instance.currentHp+"\t"+PlayerAsset.Instance.strength);
            //重写
            InitCardAsset();
        }
    }
}
Published 25 original articles · Likes6 · Visits 10,000+

Guess you like

Origin blog.csdn.net/qq_37446649/article/details/105647874