游戏开发日志15(技能树上)

为了开发技能树系统,我使用了ScriptableObject来保持数据,创建脚本SkillData如下

[CreateAssetMenu(menuName ="New Skill",fileName ="Skill")]
public class SkillData : ScriptableObject
{
   public int SkillID;
   public Sprite SkillSprite;

   public string SkillName;
   public int SkillLevel;
   [TextArea(1,8)]
   public string SkillInfo;

   public bool isUnlocked;
   public SkillData[] preSkills;
}

如图每张图片都是个button,并添加上SkillButton脚本,给每个SkillButtonn脚本赋上SkillData类型的值。

游戏开发日志15(技能树上)_第1张图片

public class SkillButton : MonoBehaviour, IPointerClickHandler
{
    public SkillData skillData;
    public void OnPointerClick(PointerEventData eventData)
    {
        SkillManage.instance.activeSkill=skillData;
        SkillManage.instance.DisPlaySkillInfo();
    }
}

 其中SkillButton用到了接口IPointerClickHandler,它可以检测鼠标指针在UI的button上的点击事件。

之后是点击让技能信息显示,创建SkillManage脚本,将其挂载在一个空物体上。

using UnityEngine.UI;

public class SkillManage : MonoBehaviour
{
    public static SkillManage instance; 
    public SkillData activeSkill;

    [Header("UI")]
    public Image SkillImage;
    public Text SkillName,SkillLv,SkillInfo;

    [Header("Skill Point")]
    [SerializeField] private int skillPoint;
    public Text pointText;

    private void Awake() {
        if(instance==null)
        {
            instance=this;
        }
        else{
            if(instance!=this)
            {
                Destroy(gameObject);
            }
        }
        DontDestroyOnLoad(gameObject);
    }

    private void Start() {
        updatePointUI();
    }

    private void updatePointUI()
    {
        pointText.text=skillPoint.ToString();
    }

    public void DisPlaySkillInfo()
    {
        SkillImage.sprite=activeSkill.SkillSprite;
        SkillName.text=activeSkill.SkillName;
        SkillLv.text="Skill Level: "+activeSkill.SkillLevel;
        SkillInfo.text="Description:\n"+activeSkill.SkillInfo;
    }
}

这样,当鼠标点击button时,SkillManage.instance.activeSkill=skillData;可以由activeSkill接收到所点击的图片的SkillData类型的数据,再用DisPlaySkillInfo函数把activeSkill的信息提取到介绍的UI上。

下期将完善技能解锁条件,以及技能点数的增减。

你可能感兴趣的:(游戏开发日志,unity数据储存,unity)