游戏开发日志16(技能树下)

由于SkillData中的preSkills[]是用来存放解锁条件的SkillData类型值,其中有个isUnlocked,于是可以写个函数UpGrade,在UpGrade中遍历preSkills来判断是否有isUnlocked为true的,从而进行updateSkill。

updateSkill:

private void updateSkill()
    {
        skillButtons[activeSkill.SkillID].GetComponent<Image>().color=Color.white;
        skillButtons[activeSkill.SkillID].transform.GetChild(1).gameObject.SetActive(true);
        activeSkill.SkillLevel++;
        skillButtons[activeSkill.SkillID].transform.GetChild(1).GetComponent<Text>().text=
        activeSkill.SkillLevel.ToString();
        DisPlaySkillInfo();
        skillPoint--;
        updatePointUI();
        activeSkill.isUnlocked=true;
    }

UpGrade:

public void UpGrade()
    {
        if(activeSkill==null)return;

        if(skillPoint>0&&activeSkill.preSkills.Length==0)
        {
           updateSkill();
        }

        if(skillPoint>0)
        {
            for(int i=0;i<activeSkill.preSkills.Length;i++)
            {
                if(activeSkill.preSkills[i].isUnlocked==true)
                {
                    updateSkill();
                    break;
                }
            }
        }
    }

实现这个技能树的所有源码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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;

    public SkillButton[] skillButtons;

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

    public void UpGrade()
    {
        if(activeSkill==null)return;

        if(skillPoint>0&&activeSkill.preSkills.Length==0)
        {
           updateSkill();
        }

        if(skillPoint>0)
        {
            for(int i=0;i<activeSkill.preSkills.Length;i++)
            {
                if(activeSkill.preSkills[i].isUnlocked==true)
                {
                    updateSkill();
                    break;
                }
            }
        }
    }

    private void updateSkill()
    {
        skillButtons[activeSkill.SkillID].GetComponent<Image>().color=Color.white;
        skillButtons[activeSkill.SkillID].transform.GetChild(1).gameObject.SetActive(true);
        activeSkill.SkillLevel++;
        skillButtons[activeSkill.SkillID].transform.GetChild(1).GetComponent<Text>().text=
        activeSkill.SkillLevel.ToString();
        DisPlaySkillInfo();
        skillPoint--;
        updatePointUI();
        activeSkill.isUnlocked=true;
    }

    public GameObject skillTree;
    public GameObject skillInfo;
    bool isOpen=true;
    public void close()
    {
        skillTree.SetActive(isOpen);
        isOpen=!isOpen;
        if(isOpen==true)
        {
            skillInfo.SetActive(false);
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[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;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

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

但是,ScriptableObject并不能保存数据,接下来我将利用PlayerPrefs/二进制 来存储技能树信息,并且开发相应的技能功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值