Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
UI.cs
using UnityEngine;
public class UI : MonoBehaviour
{
[SerializeField] private GameObject characterUI;
[SerializeField] private GameObject skillTreeUI;
[SerializeField] private GameObject craftUI;
[SerializeField] private GameObject optionsUI;
public UI_itemTooltip itemToolTip;
public UI_statToolTip statToopTip;
public Ui_SkillToolTip skillToolTip;
public UI_CraftWindow craftWindow;
public void Start()
{
SwitchTo(null);
itemToolTip.gameObject.SetActive(false);
statToopTip.gameObject.SetActive(false);
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.C))
{
SwitchWithKeyTo(characterUI);
}
if(Input.GetKeyDown(KeyCode.B))
{
SwitchWithKeyTo(craftUI);
}
if(Input.GetKeyDown(KeyCode.K))
{
SwitchWithKeyTo(skillTreeUI);
}
if(Input.GetKeyDown(KeyCode.O))
{
SwitchWithKeyTo(optionsUI);
}
}
public void SwitchTo(GameObject _menu)//切换窗口函数
{
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(false);
}
if (_menu != null)
{
_menu.SetActive(true);
}
}
public void SwitchWithKeyTo(GameObject _menu)//键盘切换窗口函数
{
if (_menu != null && _menu.activeSelf)//通过判断是否传入mune和mune是否激活来决定使设置为可视或不可使
{
_menu.SetActive(false);
return;
}
SwitchTo(_menu);
}
}
Ui_SkillToolTip.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class Ui_SkillToolTip : MonoBehaviour//制作一个可以检查每个技能的描述的窗口
{
[SerializeField] private TextMeshProUGUI skillDescription;
[SerializeField] private TextMeshProUGUI skillName;
public void ShowToolTip(string _skillDescription,string _skillName)
{
skillDescription.text = _skillDescription;
skillName.text = _skillName;
gameObject.SetActive(true);
}
public void HideToolTip() => gameObject.SetActive(false);
}
UI_SkillTreeSlot.cs
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.WSA;
public class UI_SkillTreeSlot : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
[SerializeField] private string skillName;
[TextArea]
[SerializeField] private string skillDescription;
[SerializeField] private Color lockedSkillColor;
private UI ui;
public bool unlocked;//一个表示是否解锁的bool值
private Image skillImage;
[SerializeField] private UI_SkillTreeSlot[] shouldBeUnlocked;
[SerializeField] private UI_SkillTreeSlot[] shouldBeLocked;//应该解锁和不该解锁的Slot组和Image
private void OnValidate()
{
gameObject.name = "SkillTreeSlot_UI - " + skillName;
}
private void Start()
{
skillImage = GetComponent<Image>();
skillImage.color = lockedSkillColor;
ui = GetComponentInParent<UI>();
GetComponent<Button>().onClick.AddListener(() => UnlockSkillSlot());
}
public void UnlockSkillSlot()//一个判断此Skill是否可以解锁的函数
{
for(int i = 0;i < shouldBeUnlocked.Length;i++)
{
if (shouldBeUnlocked[i].unlocked == false)
{
Debug.Log("Cannot unlock skill");
return;
}
}
for(int i = 0; i < shouldBeLocked.Length; i++)
{
if (shouldBeLocked[i].unlocked == true)
{
Debug.Log("Cannot unlock skill");
return;
}
}
unlocked = true;
skillImage.color = Color.white;
}
public void OnPointerEnter(PointerEventData eventData)
{
ui.skillToolTip.ShowToolTip(skillDescription, skillName);
Vector2 mousePosition = Input.mousePosition;
float xOffset = 0;
float yOffset = 0;
if(mousePosition.x > 600)
{
xOffset = -150;
}
else
{
xOffset = 150;
}
if(mousePosition.y > 200)
{
yOffset = -100;
}
else
{
yOffset = 100;
}
ui.skillToolTip.transform.position = new Vector2(mousePosition.x + xOffset, mousePosition.y + yOffset);
}
public void OnPointerExit(PointerEventData eventData)
{
ui.skillToolTip.HideToolTip();
}
}