基于“RPG项目01_技能释放”,将UI包导入Unity场景中,
将图片放置
拖拽
取消勾选(隐藏攻击切片)
对技能添加蒙版
调节父子物体大小一致
将子类蒙版复制
执行5次
运行即可看到技能使用完的冷却条
在Scripts下创建UI文件夹
写代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class UIBase : MonoBehaviour
{
public Transform btnParent;
public GameObject prefab;
public UnityAction
public UITween tween;
protected List
protected Button btnBack;
protected Text text;
protected void Init()
{
tween = GetComponent
btnBack = GameManager.FindType
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerPanel : UIBase
{
[Header("==============子类变量================")]
public UIBase panelMsn;
public UIBase panelBag;
Button btnMsn;
Button btnBag;
static Image hpImage;
static Image mpImage;
void Start()
{
hpImage = GameManager.FindType
mpImage = GameManager.FindType
btnMsn = GameManager.FindType
//刷新血量
public static void UpdateHpUI(float hpValue, float mpValue)
{
hpImage.fillAmount = hpValue;
mpImage.fillAmount = mpValue;
}
}
将PalyerPanel挂载在MainPanel上
新增MaPlayer代码:
protected override void UpdateUI()
{
PlayerPanel.UpdateHpUI(GetHpRation(), (float)Mp/MpMax);
}
将初始mp/最大mp调少一点:
运行E拔刀后F1释放技能即可看到mp蓝条减少
添加代码:
public void SkillClick(int num)
{
if (!Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight"))
{
return;
}
SkillBase skill = skills[num];
if (!skill.MayRelease())
{
return;
}
Anim.SetTrigger("CSkill" + num);
ReleaseSkill(skill);
}
选中技能1-6添加Button组件
新增PlayerPanel代码:(找到按钮)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++点击技能释放技能缺少button事件+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
加入新按钮,
删除旧按钮,
新增两个到Bag包下
删除两个
delete
挪动Text位置
复制道具框,
在Content上增加尺寸适配器组件(自动计算)通常和Grid Layout Group配合使用
设置自动隐藏(当需要滚动条的时候显示,不需要时隐藏)
此时,两侧面板基本完成
新建代码BagPanel
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BagPanel : UIBase{
Transform canvas;
Transform bagTran;
Transform equipTran;
Button btnTakeOff;
Text textHp;
Text textMp;
Text textLv;
Text textExp;
Text textAtt;
Text textDef;
Text textSpd;
Text textMdf;
private void Start(){
}
public void InitText(){
canvas = MainGame.canvas;
bagTran = transform.Find("Bag");
equipTran = transform.Find("Equip");
text = GameManager.FindType
Transform temp = equipTran.Find("TextParent");
textHp = GameManager.FindType
textMp = GameManager.FindType
textLv = GameManager.FindType
textExp = GameManager.FindType
textAtt = GameManager.FindType
textDef = GameManager.FindType
textSpd = GameManager.FindType
textMdf = GameManager.FindType
}
}
新增MainPanel代码类段代码:
修改Text为TextGold
在Canvas下创建Image
选一张图片
添加组件
添加组件
添加事件类型
再做一下鼠标移动到图标就会消失功能:
鼠标到图标位置,图标就会消失
知识点:如果用Selectable和Event Trigger就可以替代Button按钮做更复杂的UI
新建脚本道具类GameObj
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//药品,装备
public enum ObjType { None, Drug, Equip };
//装备包含:
public enum EquipType { None, Helmet, Armor, Boots, Weapon, Shield };
//装备数据
public class GameObj{
public string oname;
public string msg;
public int idx;
public string type;
public int _hp;
public int _mp;
public int _def;
public int _mdf;
public int _att;
public int _spd;
public bool isTakeOn;//判断是否穿上
public virtual void UseObjValue(){
}
public virtual void TakeOff(){
}
public virtual void TakeOn(){
}
}
修改BagPanel代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class BagPanel : UIBase{
Transform canvas;
Transform bagTran;
Transform equipTran;
Button btnTakeOff;
Text textHp;
Text textMp;
Text textLv;
Text textExp;
Text textAtt;
Text textDef;
Text textSpd;
Text textMdf;
void Start(){
}
public void InitText(){
canvas = MainGame.canvas;
bagTran = transform.Find("Bag");
equipTran = transform.Find("Equip");
text = GameManager.FindType
Transform temp = equipTran.Find("TextParent");
textHp = GameManager.FindType
textMp = GameManager.FindType
textLv = GameManager.FindType
textExp = GameManager.FindType
textAtt = GameManager.FindType
textDef = GameManager.FindType
textSpd = GameManager.FindType
textMdf = GameManager.FindType
}
void AddEvent(GameObj obj, GameObject btn)
{
Image image = btn.GetComponent
EventTrigger et = btn.GetComponent
if (!et)
{
et = btn.AddComponent
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.BeginDrag;//开始拖拽
entry.callback.AddListener(delegate
{
image.raycastTarget = false;
btn.transform.SetParent(canvas);
}
);
et.triggers.Add(entry);
//
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;//拖拽中
entry.callback.AddListener(delegate (BaseEventData arg)
{
PointerEventData ped = (PointerEventData)arg;//将基础数据变成鼠标数据
Vector3 newPos;
RectTransformUtility.ScreenPointToWorldPointInRectangle(
btn.GetComponent
ped.enterEventCamera, out newPos);
btn.transform.position = newPos;
}
);
et.triggers.Add(entry);
/;
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.EndDrag;//结束拖拽
entry.callback.AddListener(delegate (BaseEventData arg)
{
if (!EventSystem.current.IsPointerOverGameObject())
{
btn.transform.parent = btnParent;
image.raycastTarget = true;
return;
}
PointerEventData ped = (PointerEventData)arg;
Transform target = ped.pointerEnter.transform;
if (target)
{
if (target.tag == obj.GetType().ToString())
{
obj.TakeOn();
//交换装备
ChangeGameObj(target.parent, btn);
//刷新数值
UpdatePlayerValue();
}
else if (target.name == obj.GetType().ToString() || target.name == obj.type)
{
//交换药品
ChangeGameObj(target, btn);
}
else
{
obj.isTakeOn = false;
btn.transform.parent = btnParent;
}
}
image.raycastTarget = true;
}
);
et.triggers.Add(entry);
/
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;//拖拽中
entry.callback.AddListener(delegate (BaseEventData arg)
{
if (btn.tag != ObjType.Drug.ToString())
{
return;
}
//MainGame.player.UseDrugObj(btn.transform);
}
);
et.triggers.Add(entry);
}
//刷新玩家数据
public void UpdatePlayerValue()
{
textHp.text = "生命:" + MainGame.player.Hp + "/" + MainGame.player.HpMax;
textMp.text = "魔力:" + MainGame.player.Mp + "/" + MainGame.player.MpMax;
textAtt.text = "攻击:" + MainGame.player.Att;
textDef.text = "防御:" + MainGame.player.Def;
textMdf.text = "魔抗:" + MainGame.player.Mdf;
textSpd.text = "速度:" + MainGame.player.Spd;
textLv.text = "等级:" + MainGame.player.lv;
textExp.text = "经验:" + MainGame.player.Exp;
}
void ChangeGameObj(Transform target, GameObject btn)
{
GameObj temp = new GameObj();
if (target.childCount >= 2)
{
//temp = GameManager.GetGameObj(target.GetChild(0).name);
//SetBtnTakeOff(target.GetChild(0), temp);
}
btn.transform.parent = target.transform;
btn.transform.SetAsFirstSibling();
btn.transform.localPosition = Vector3.zero;
btn.GetComponent
//temp = GameManager.GetGameObj(btn.name);
if (temp != null)
{
temp.isTakeOn = true;
}
}
void SetBtnTakeOff(Transform btn, GameObj obj)
{
if (obj != null)
{
btn.parent = btnParent;
obj.isTakeOn = false;
btn.GetComponent
}
}
}
修改GameManager类:
using System.Collections.Generic;
using System.Xml;
using Unity.VisualScripting;
using UnityEngine;
public enum GameState { Play,Menu };
public class GameManager
{
//当只需要一个的时候使用静态类
public static GameState gameState = GameState.Play;
//物品库
public static Dictionary
public static void Init()
{
}
public static T FindType
{
return t.Find(n).GetComponent
}
public static T ParseEnum
{
return (T)System.Enum.Parse(typeof(T), value, true);
}
#region 添加物品库
//读取XML文件
public static void SetGoods()
{
TextAsset t = LoadManager.LoadXml("XML");
XmlDocument xml = new XmlDocument();
xml.LoadXml(t.ToString().Trim());
XmlElement root = xml.DocumentElement;
XmlElement oInfo = (XmlElement)root.SelectSingleNode("DrugInfo");
//节点列表 获取所有子节点
XmlNodeList nodeList = oInfo.ChildNodes;
foreach (XmlElement item in nodeList)
{
//Drug drug = new Drug();
//drug.oname = item.GetAttribute("Name");
//drug.msg = item.GetAttribute("Msg");
//drug.idx = int.Parse(item.GetAttribute("Idx"));
//drug.type = item.GetAttribute("Type");
//drug._hp = int.Parse(item.GetAttribute("Hp"));
//drug._mp = int.Parse(item.GetAttribute("Mp"));
//AddGoodDict(drug);
}
// oInfo = (XmlElement)root.SelectSingleNode("EquipInfo");
// nodeList = oInfo.ChildNodes;//每一个节点
// foreach (XmlElement item in nodeList)
// {
// Equip eq = new Equip();
// eq.oname = item.GetAttribute("Name");
// eq.msg = item.GetAttribute("Msg");
// eq.idx = int.Parse(item.GetAttribute("Idx"));
// eq.type = item.GetAttribute("Type");
// eq._att = int.Parse(item.GetAttribute("Att"));
// eq._def = int.Parse(item.GetAttribute("Def"));
// eq._mdf = int.Parse(item.GetAttribute("Mdf"));
// eq._spd = int.Parse(item.GetAttribute("Spd"));
// AddGoodDict(eq);
// }
//}
//public static void AddGoodDict(GameObj obj)
//{
// if (obj == null)
// {
// return;
// }
// goods.Add(obj.oname, obj);
//}
//public static GameObj GetGameObj(string name)
//{
// if (goods.ContainsKey(name))
// {
// return goods[name];
// }
// else
// {
// return null;
// }
//}
//public static GameObj GetGameObj(int idx)
//{
// foreach (GameObj item in goods.Values)
// {
// if (item.idx == idx)
// {
// return item;
// }
// }
// return null;
//}
#endregion
}
}
新建脚本药品类Drug
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Drug : GameObj
{
public override void UseObjValue()
{
MainGame.player.AddHp(_hp);
MainGame.player.AddMp(_mp);
}
}
继续修改GameManager代码:
using System.Collections.Generic;
using System.Xml;
using Unity.VisualScripting;
using UnityEngine;
public enum GameState { Play,Menu };
public class GameManager
{
//当只需要一个的时候使用静态类
public static GameState gameState = GameState.Play;
//物品库
public static Dictionary
public static void Init()
{
}
public static T FindType
{
return t.Find(n).GetComponent
}
public static T ParseEnum
{
return (T)System.Enum.Parse(typeof(T), value, true);
}
#region 添加物品库
//读取XML文件
public static void SetGoods()
{
TextAsset t = LoadManager.LoadXml("XML");
XmlDocument xml = new XmlDocument();
xml.LoadXml(t.ToString().Trim());
XmlElement root = xml.DocumentElement;
XmlElement oInfo = (XmlElement)root.SelectSingleNode("DrugInfo");
//节点列表 获取所有子节点
XmlNodeList nodeList = oInfo.ChildNodes;
foreach (XmlElement item in nodeList)
{
Drug drug = new Drug();
drug.oname = item.GetAttribute("Name");
drug.msg = item.GetAttribute("Msg");
drug.idx = int.Parse(item.GetAttribute("Idx"));
drug.type = item.GetAttribute("Type");
drug._hp = int.Parse(item.GetAttribute("Hp"));
drug._mp = int.Parse(item.GetAttribute("Mp"));
AddGoodDict(drug);
}
//oInfo = (XmlElement)root.SelectSingleNode("EquipInfo");
//nodeList = oInfo.ChildNodes;//每一个节点
//foreach (XmlElement item in nodeList)
//{
// Equip eq = new Equip();
// eq.oname = item.GetAttribute("Name");
// eq.msg = item.GetAttribute("Msg");
// eq.idx = int.Parse(item.GetAttribute("Idx"));
// eq.type = item.GetAttribute("Type");
// eq._att = int.Parse(item.GetAttribute("Att"));
// eq._def = int.Parse(item.GetAttribute("Def"));
// eq._mdf = int.Parse(item.GetAttribute("Mdf"));
// eq._spd = int.Parse(item.GetAttribute("Spd"));
// AddGoodDict(eq);
//}
}
public static void AddGoodDict(GameObj obj)
{
if (obj == null)
{
return;
}
goods.Add(obj.oname, obj);
}
//public static GameObj GetGameObj(string name)
//{
// if (goods.ContainsKey(name))
// {
// return goods[name];
// }
// else
// {
// return null;
// }
//}
//public static GameObj GetGameObj(int idx)
//{
// foreach (GameObj item in goods.Values)
// {
// if (item.idx == idx)
// {
// return item;
// }
// }
// return null;
//}
#endregion
}
再创建Equip道具类
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Equip : GameObj
{
public override void TakeOff()
{
//
}
}
继续修改GameManager类:
using System.Collections.Generic;
using System.Xml;
using Unity.VisualScripting;
using UnityEngine;
public enum GameState { Play,Menu };
public class GameManager
{
//当只需要一个的时候使用静态类
public static GameState gameState = GameState.Play;
//物品库
public static Dictionary
public static void Init()
{
SetGoods();
}
public static T FindType
{
return t.Find(n).GetComponent
}
public static T ParseEnum
{
return (T)System.Enum.Parse(typeof(T), value, true);
}
#region 添加物品库
//读取XML文件
public static void SetGoods()
{
TextAsset t = LoadManager.LoadXml("XML");
XmlDocument xml = new XmlDocument();
xml.LoadXml(t.ToString().Trim());
XmlElement root = xml.DocumentElement;
XmlElement oInfo = (XmlElement)root.SelectSingleNode("DrugInfo");
//节点列表 获取所有子节点
XmlNodeList nodeList = oInfo.ChildNodes;
foreach (XmlElement item in nodeList)
{
Drug drug = new Drug();
drug.oname = item.GetAttribute("Name");
drug.msg = item.GetAttribute("Msg");
drug.idx = int.Parse(item.GetAttribute("Idx"));
drug.type = item.GetAttribute("Type");
drug._hp = int.Parse(item.GetAttribute("Hp"));
drug._mp = int.Parse(item.GetAttribute("Mp"));
AddGoodDict(drug);
}
oInfo = (XmlElement)root.SelectSingleNode("EquipInfo");
//节点列表 获取所有子节点
nodeList = oInfo.ChildNodes;
foreach (XmlElement item in nodeList)
{
//alt + 回车 一键全改错误eq
Equip eqq = new Equip();
eqq.oname = item.GetAttribute("Name");
eqq.msg = item.GetAttribute("Msg");
eqq.idx = int.Parse(item.GetAttribute("Idx"));
eqq.type = item.GetAttribute("Type");
eqq._att = int.Parse(item.GetAttribute("Att"));
eqq._def = int.Parse(item.GetAttribute("Def"));
eqq._mdf = int.Parse(item.GetAttribute("Mdf"));
eqq._spd = int.Parse(item.GetAttribute("Spd"));
AddGoodDict(eqq);
}
}
public static void AddGoodDict(GameObj obj)
{
if (obj == null)
{
return;
}
goods.Add(obj.oname, obj);
}
public static GameObj GetGameObj(string name)
{
if (goods.ContainsKey(name))
{
return goods[name];
}
else
{
return null;
}
}
public static GameObj GetGameObj(int idx)
{
foreach (GameObj item in goods.Values)
{
if (item.idx == idx)
{
return item;
}
}
return null;
}
#endregion
}
修改BagPanel类:
修改背包面板BagPanel代码,在Start下加内容:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class BagPanel : UIBase{
Transform canvas;
Transform bagTran;
Transform equipTran;
Button btnTakeOff;
Text textHp;
Text textMp;
Text textLv;
Text textExp;
Text textAtt;
Text textDef;
Text textSpd;
Text textMdf;
void Start(){
InitText();
btnTakeOff = GameManager.FindType
btnTakeOff.onClick.AddListener(delegate
{
foreach (Transform item in equipTran.Find("Eq"))
{
if (item.childCount >= 2)
{
GameObj temp = GameManager.GetGameObj(item.GetChild(0).name);
temp.TakeOff();
UpdatePlayerValue();
SetBtnTakeOff(item.GetChild(0), temp);
}
}
});
tween = bagTran.GetComponent
tween.AddEventStartHandle(UpdateValue);
btnBack = GameManager.FindType
btnBack.onClick.AddListener(delegate
{
tween.UIBack();
equipTran.GetComponent
});
}
public void InitText(){
canvas = MainGame.canvas;
bagTran = transform.Find("Bag");
equipTran = transform.Find("Equip");
text = GameManager.FindType
Transform temp = equipTran.Find("TextParent");
textHp = GameManager.FindType
textMp = GameManager.FindType
textLv = GameManager.FindType
textExp = GameManager.FindType
textAtt = GameManager.FindType
textDef = GameManager.FindType
textSpd = GameManager.FindType
textMdf = GameManager.FindType
}
void AddEvent(GameObj obj, GameObject btn)
{
Image image = btn.GetComponent
EventTrigger et = btn.GetComponent
if (!et)
{
et = btn.AddComponent
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.BeginDrag;//开始拖拽
entry.callback.AddListener(delegate
{
image.raycastTarget = false;
btn.transform.SetParent(canvas);
}
);
et.triggers.Add(entry);
//
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;//拖拽中
entry.callback.AddListener(delegate (BaseEventData arg)
{
PointerEventData ped = (PointerEventData)arg;//将基础数据变成鼠标数据
Vector3 newPos;
RectTransformUtility.ScreenPointToWorldPointInRectangle(
btn.GetComponent
ped.enterEventCamera, out newPos);
btn.transform.position = newPos;
}
);
et.triggers.Add(entry);
/;
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.EndDrag;//结束拖拽
entry.callback.AddListener(delegate (BaseEventData arg)
{
if (!EventSystem.current.IsPointerOverGameObject())
{
btn.transform.parent = btnParent;
image.raycastTarget = true;
return;
}
PointerEventData ped = (PointerEventData)arg;
Transform target = ped.pointerEnter.transform;
if (target)
{
if (target.tag == obj.GetType().ToString())
{
obj.TakeOn();
//交换装备
ChangeGameObj(target.parent, btn);
//刷新数值
UpdatePlayerValue();
}
else if (target.name == obj.GetType().ToString() || target.name == obj.type)
{
//交换药品
ChangeGameObj(target, btn);
}
else
{
obj.isTakeOn = false;
btn.transform.parent = btnParent;
}
}
image.raycastTarget = true;
}
);
et.triggers.Add(entry);
/
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;//拖拽中
entry.callback.AddListener(delegate (BaseEventData arg)
{
if (btn.tag != ObjType.Drug.ToString())
{
return;
}
//MainGame.player.UseDrugObj(btn.transform);
}
);
et.triggers.Add(entry);
}
//刷新玩家数据
public void UpdatePlayerValue()
{
textHp.text = "生命:" + MainGame.player.Hp + "/" + MainGame.player.HpMax;
textMp.text = "魔力:" + MainGame.player.Mp + "/" + MainGame.player.MpMax;
textAtt.text = "攻击:" + MainGame.player.Att;
textDef.text = "防御:" + MainGame.player.Def;
textMdf.text = "魔抗:" + MainGame.player.Mdf;
textSpd.text = "速度:" + MainGame.player.Spd;
textLv.text = "等级:" + MainGame.player.lv;
textExp.text = "经验:" + MainGame.player.Exp;
}
void ChangeGameObj(Transform target, GameObject btn)
{
GameObj temp = new GameObj();
if (target.childCount >= 2)
{
temp = GameManager.GetGameObj(target.GetChild(0).name);
SetBtnTakeOff(target.GetChild(0), temp);
}
btn.transform.parent = target.transform;
btn.transform.SetAsFirstSibling();
btn.transform.localPosition = Vector3.zero;
btn.GetComponent
temp = GameManager.GetGameObj(btn.name);
if (temp != null)
{
temp.isTakeOn = true;
}
}
void SetBtnTakeOff(Transform btn, GameObj obj)
{
if (obj != null)
{
btn.parent = btnParent;
obj.isTakeOn = false;
btn.GetComponent
}
}
}
新增BagPanel代码:
public override void UpdateValue()
{
ClearBtn(btnParent);
//暂时写不了,需要新增MyPlayer代码的函数
}
新增MyPlayer代码:
添加一个方法
修改MyPlayer代码,利用xml文档的函数添加两个道具
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class MyPlayer : People
{
[Header("=================子类变量=================")]
public Transform toolPanel;//道具面板
public Transform skillPanel;//技能面板
public BagPanel bag;//包
CharacterController contro;
Controls action;
float rvalue;
float spdFast = 1;
bool isHold;//握住刀
GameObject sword;
GameObject swordBack;
public Image imageHp;
public Image imageMp;
//bagTools = 背包工具
Dictionary
Dictionary
void Start()
{
//base.Start();
InitValue();
InitSkill();
//获取自身角色控制器
contro = GetComponent
SetInput();
sword = transform.Find("Sword_Hand").gameObject;
swordBack = transform.Find("Sword_Back").gameObject;
bag.InitText();
// AddTool(GameManager.GetGameObj("大还丹"), 5);
//3.5.7.6是xml文档里的道具(数字是编号)
//作用是利用xml文档里添加两个道具
AddTool(GameManager.GetGameObj(3), 5);
AddTool(GameManager.GetGameObj(7), 6);
}
void Update()
{
if (GameManager.gameState != GameState.Play)
{
return;
}
Ctrl();
UpdateSkillTime();
}
void SetInput()
{
action = new Controls();
action.Enable();
action.MyCtrl.Move.started += Move;
action.MyCtrl.Move.performed += Move;
action.MyCtrl.Move.canceled += StopMove;
action.MyCtrl.Jump.started += Jump;
action.MyCtrl.Rotate.started += Rotate;
action.MyCtrl.Rotate.performed += Rotate;
action.MyCtrl.Fast.started += FastSpeed;
action.MyCtrl.Fast.performed += FastSpeed;
action.MyCtrl.Fast.canceled += FastSpeed;
action.MyCtrl.GetTool.started += ClickNpcAndTool;
action.MyCtrl.HoldRotate.performed += Hold;
action.MyCtrl.HoldRotate.canceled += Hold;
action.MyAtt.Att.started += Attack;
action.MyAtt.SwordOut.started += SwordOut;
action.Skill.F1.started += SkillAtt;
action.Skill.F2.started += SkillAtt;
action.Skill.F3.started += SkillAtt;
action.Skill.F4.started += SkillAtt;
action.Skill.F5.started += SkillAtt;
action.Skill.F6.started += SkillAtt;
// action.Tools._1.started += GetkeyClick;
// action.Tools._2.started += GetkeyClick;
// action.Tools._3.started += GetkeyClick;
// action.Tools._4.started += GetkeyClick;
//action.Tools._5.started += GetkeyClick;
//action.Tools._6.started += GetkeyClick;
//action.Tools._7.started += GetkeyClick;
//action.Tools._8.started += GetkeyClick;
}
private void GetkeyClick(InputAction.CallbackContext context)
{
string[] str = context.control.ToString().Split('/');
int num = int.Parse(str[2]) - 1;
// UseObj(num);
}
private void SwordOut(InputAction.CallbackContext context)
{
if (GameManager.gameState != GameState.Play)
{
return;
}
Anim.SetBool("SwordOut", !Anim.GetBool("SwordOut"));
}
#region 攻击
void SetSwordVisible(int n)
{
sword.SetActive(n != 0);
swordBack.SetActive(n == 0);
}
private void Attack(InputAction.CallbackContext context)
{
if (GameManager.gameState != GameState.Play)
{
return;
}
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight"))
{
Anim.SetInteger("Att", 1);
Anim.SetTrigger("AttTrigger");
}
else
{
int num = Anim.GetInteger("Att");
if (num == 6)
{
return;
}
if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Light_Attk_" + num))
{
Anim.SetInteger("Att", num + 1);
}
}
}
public void PlayerAttack(string hurt)
{
Collider[] cs = Physics.OverlapBox(attPoint.position, Vector3.one * 0.5f,
attPoint.rotation, LayerMask.GetMask("Enemy"));
if (cs.Length <= 0)
{
return;
}
int value = (int)(Att * Anim.GetInteger("Att") * 0.5f);
foreach (Collider c in cs)
{
print(value);
}
}
public void PlayerAttackHard(string hurt)
{
Collider[] cs = Physics.OverlapBox(attPoint.position, Vector3.one * 0.5f,
attPoint.rotation, LayerMask.GetMask("Enemy"));
if (cs.Length <= 0)
{
return;
}
int value = (int)(Att * Anim.GetInteger("Att") * 0.5f);
foreach (Collider c in cs)
{
print(value);
print("让敌人播放击倒特效");
}
}
#endregion
#region 人物控制
void Ctrl()
{
if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run") || Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_Inplace") || Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle")
|| Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight"))
{
float f = action.MyCtrl.Move.ReadValue
contro.Move(transform.forward * f * Time.deltaTime * spdFast * Spd);
contro.Move(transform.up * -9.8f * Time.deltaTime);
if (isHold)
{
transform.Rotate(transform.up * rvalue * 0.3f);
}
}
}
private void Hold(InputAction.CallbackContext context)
{
if (GameManager.gameState != GameState.Play)
{
return;
}
if (context.phase == InputActionPhase.Canceled)
{
isHold = false;
}
else
{
isHold = true;
}
}
private void ClickNpcAndTool(InputAction.CallbackContext context)
{
//throw new NotImplementedException();
}
private void FastSpeed(InputAction.CallbackContext context)
{
if (GameManager.gameState != GameState.Play)
{
return;
}
if (Anim.GetCurrentAnimatorStateInfo(0).IsName("Run") || Anim.GetCurrentAnimatorStateInfo(0).IsName("Run_Inplace"))
{
if (context.phase == InputActionPhase.Canceled)
{
spdFast = 1;
}
else
{
spdFast = 2;
}
}
}
private void Rotate(InputAction.CallbackContext context)
{
if (GameManager.gameState != GameState.Play)
{
return;
}
rvalue = context.ReadValue
}
private void Jump(InputAction.CallbackContext context)
{
if (GameManager.gameState != GameState.Play)
{
return;
}
Anim.SetTrigger("Jump");
}
private void StopMove(InputAction.CallbackContext context)
{
Anim.SetBool("IsRun", false);
}
private void Move(InputAction.CallbackContext context)
{
if (GameManager.gameState != GameState.Play)
{
return;
}
Anim.SetBool("IsRun", true);
}
#endregion
#region 技能
protected override void InitSkill()
{
SphereSkill thunderBombCut = new SphereSkill(this, "雷爆斩", SkillType.Magic, 3, Att * 10, -100, 20, 0.3f, 0);
skills.Add(1, thunderBombCut);
SphereSkill windCircleCut = new SphereSkill(this, "旋风斩", SkillType.Physics, 3, Att * 8, -50, 20, 0.2f, 0);
skills.Add(2, windCircleCut);
StraightSkill thunderLightCut = new StraightSkill(this, "雷光斩", SkillType.Physics, 7, 0.5f, Att * 7, -50, 20, 0.2f, 0);
skills.Add(3, thunderLightCut);
SphereSkill oneCut = new SphereSkill(this, "归一斩", SkillType.Physics, 7, Att * 7, -30, 8, 0.2f, 0);
skills.Add(4, oneCut);
StraightSkill crossCut = new StraightSkill(this, "十字斩", SkillType.Physics, 25, 0.5f, Att * 3, -40, 20, 0.2f, 0);
skills.Add(5, crossCut);
SphereSkill thunderLargeCut = new SphereSkill(this, "轰雷斩", SkillType.Magic, 7, Att * 15, -120, 25, 0.35f, 0);
skills.Add(6, thunderLargeCut);
}
private void SkillAtt(InputAction.CallbackContext context)
{
if (!Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight"))
{
return;
}
string[] str = context.control.ToString().Split('/');
int num = int.Parse(str[2][1].ToString());
SkillBase skill = skills[num];
if (!skill.MayRelease())
{
return;
}
Anim.SetTrigger("CSkill" + num);
ReleaseSkill(skill);
}
public void SkillClick(int num)
{
if (!Anim.GetCurrentAnimatorStateInfo(0).IsName("Idle_Fight"))
{
return;
}
SkillBase skill = skills[num];
if (!skill.MayRelease())
{
return;
}
Anim.SetTrigger("CSkill" + num);
ReleaseSkill(skill);
}
void UpdateSkillTime()
{
for (int i = 0; i < skillPanel.childCount; i++)
{
if (skills[i + 1].IsRelease)
{
continue;
}
Image image = skillPanel.GetChild(i).GetChild(0).GetComponent
image.fillAmount = skills[i + 1].GetFillTime();
}
}
#endregion
protected override void UpdateUI()
{
PlayerPanel.UpdateHpUI(GetHpRation(), (float)Mp/MpMax);
}
#region 物品道具
public Dictionary
{
return bagTools;
}
public void AddTool(GameObj gObj, int num)
{
if (gObj == null)
{
return;
}
GameObj temp = GameManager.GetGameObj(gObj.oname);
if (bagTools.ContainsKey(temp))
{
bagTools[temp] += num;
}
else
{
// bagTools[temp] = num;
bagTools.Add(gObj, num);
}
bag.UpdateValue();
}
//public void UseDrugObj(Transform t)
//{
// GameObj obj = GameManager.GetGameObj(t.name);
// obj.UseObjValue();
// bag.UpdatePlayerValue();
// bagTools[obj]--;
// t.GetComponentInChildren
// if (bagTools[obj] == 0)
// {
// bagTools.Remove(obj);
// Destroy(t.gameObject);
// }
//}
//private void UseObj(int num)
//{
// Transform temp = toolPanel.GetChild(num);
// if (temp.childCount < 2)
// {
// return;
// }
// Transform t = temp.GetChild(0);
// UseDrugObj(t);
//}
#endregion
}
再次修改BagPanel代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class BagPanel : UIBase{
Transform canvas;
Transform bagTran;
Transform equipTran;
Button btnTakeOff;
Text textHp;
Text textMp;
Text textLv;
Text textExp;
Text textAtt;
Text textDef;
Text textSpd;
Text textMdf;
void Start(){
InitText();
btnTakeOff = GameManager.FindType
btnTakeOff.onClick.AddListener(delegate
{
foreach (Transform item in equipTran.Find("Eq"))
{
if (item.childCount >= 2)
{
GameObj temp = GameManager.GetGameObj(item.GetChild(0).name);
temp.TakeOff();
UpdatePlayerValue();
SetBtnTakeOff(item.GetChild(0), temp);
}
}
});
tween = bagTran.GetComponent
tween.AddEventStartHandle(UpdateValue);
btnBack = GameManager.FindType
btnBack.onClick.AddListener(delegate
{
tween.UIBack();
equipTran.GetComponent
});
}
public override void UpdateValue()
{
ClearBtn(btnParent);
Dictionary
foreach (GameObj item in keys)
{
if (item.isTakeOn)
{
continue;
}
GameObject btn = Instantiate(prefab, btnParent);
btn.GetComponent
btn.name = item.oname;
btn.tag = item.GetType().ToString();
btn.GetComponentInChildren
AddEvent(item, btn);
}
}
public void InitText(){
canvas = MainGame.canvas;
bagTran = transform.Find("Bag");
equipTran = transform.Find("Equip");
text = GameManager.FindType
Transform temp = equipTran.Find("TextParent");
textHp = GameManager.FindType
textMp = GameManager.FindType
textLv = GameManager.FindType
textExp = GameManager.FindType
textAtt = GameManager.FindType
textDef = GameManager.FindType
textSpd = GameManager.FindType
textMdf = GameManager.FindType
}
void AddEvent(GameObj obj, GameObject btn)
{
Image image = btn.GetComponent
EventTrigger et = btn.GetComponent
if (!et)
{
et = btn.AddComponent
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.BeginDrag;//开始拖拽
entry.callback.AddListener(delegate
{
image.raycastTarget = false;
btn.transform.SetParent(canvas);
}
);
et.triggers.Add(entry);
//
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;//拖拽中
entry.callback.AddListener(delegate (BaseEventData arg)
{
PointerEventData ped = (PointerEventData)arg;//将基础数据变成鼠标数据
Vector3 newPos;
RectTransformUtility.ScreenPointToWorldPointInRectangle(
btn.GetComponent
ped.enterEventCamera, out newPos);
btn.transform.position = newPos;
}
);
et.triggers.Add(entry);
/;
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.EndDrag;//结束拖拽
entry.callback.AddListener(delegate (BaseEventData arg)
{
if (!EventSystem.current.IsPointerOverGameObject())
{
btn.transform.parent = btnParent;
image.raycastTarget = true;
return;
}
PointerEventData ped = (PointerEventData)arg;
Transform target = ped.pointerEnter.transform;
if (target)
{
if (target.tag == obj.GetType().ToString())
{
obj.TakeOn();
//交换装备
ChangeGameObj(target.parent, btn);
//刷新数值
UpdatePlayerValue();
}
else if (target.name == obj.GetType().ToString() || target.name == obj.type)
{
//交换药品
ChangeGameObj(target, btn);
}
else
{
obj.isTakeOn = false;
btn.transform.parent = btnParent;
}
}
image.raycastTarget = true;
}
);
et.triggers.Add(entry);
/
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;//拖拽中
entry.callback.AddListener(delegate (BaseEventData arg)
{
if (btn.tag != ObjType.Drug.ToString())
{
return;
}
//MainGame.player.UseDrugObj(btn.transform);
}
);
et.triggers.Add(entry);
}
//刷新玩家数据
public void UpdatePlayerValue()
{
textHp.text = "生命:" + MainGame.player.Hp + "/" + MainGame.player.HpMax;
textMp.text = "魔力:" + MainGame.player.Mp + "/" + MainGame.player.MpMax;
textAtt.text = "攻击:" + MainGame.player.Att;
textDef.text = "防御:" + MainGame.player.Def;
textMdf.text = "魔抗:" + MainGame.player.Mdf;
textSpd.text = "速度:" + MainGame.player.Spd;
textLv.text = "等级:" + MainGame.player.lv;
textExp.text = "经验:" + MainGame.player.Exp;
}
void ChangeGameObj(Transform target, GameObject btn)
{
GameObj temp = new GameObj();
if (target.childCount >= 2)
{
temp = GameManager.GetGameObj(target.GetChild(0).name);
SetBtnTakeOff(target.GetChild(0), temp);
}
btn.transform.parent = target.transform;
btn.transform.SetAsFirstSibling();
btn.transform.localPosition = Vector3.zero;
btn.GetComponent
temp = GameManager.GetGameObj(btn.name);
if (temp != null)
{
temp.isTakeOn = true;
}
}
void SetBtnTakeOff(Transform btn, GameObj obj)
{
if (obj != null)
{
btn.parent = btnParent;
obj.isTakeOn = false;
btn.GetComponent
}
}
}
挂载脚本
将content拖拽
修改unity场景中TxtParent为TextParent
拖拽Text预制体
添加图片作为一件脱装备,设置正常尺寸
修改Button名为BtnTakeOff
将UITween代码分别挂载在Bag和Equip上
拖拽背包面板BagPanel
添加标签
拖拽
增加两个页面的偏移值
接下来需要做下侧道具栏的层级显示