由于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/二进制 来存储技能树信息,并且开发相应的技能功能。