本章项目成果展示
我们打开上一篇763D模型动态UI显示的项目,
本章要做的事情是鼠标悬停在道具身上显示对应信息
首先制作一个武器Image信息面板
重命名为WeaponUI01
设为隐藏
修改脚本:RightClickItem.cs
查看挂载脚本:
运行项目 - 当鼠标悬停背包中的装备时
继续制作面板 - 修改增加其他装备
首先增加装备栏里的插槽位置标签
修改脚本:RightClickItem.cs
修改脚本:UGUICanvas.cs
增加装备栏插槽sprite需要加载图片
创建脚本:RightClickShoesBack.cs
using QFramework.Example;
using QFramework;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class RightClickShoesBack : MonoBehaviour, IPointerClickHandler{Image childImage;Sprite ShoesIconNormal;GameManager gm;GameObject currentCanvas;void Start(){gm = GameManager.Instance;ShoesIconNormal = Resources.Load<Sprite>("Prefabs/UGUIIcons/IndigoShieldtIcon_14");currentCanvas = GameObject.Find("CurrentCanvas").gameObject;}public void OnPointerClick(PointerEventData eventData){if (eventData.button == PointerEventData.InputButton.Right){if (transform.GetComponent<Image>().sprite == null)return;Transform Icon = currentCanvas.transform.Find("BagExample/Scroll View/Viewport/SlotItemRoot/UISlot(Clone)/Icon").transform;Icon.gameObject.SetActive(true);childImage = GameObject.FindWithTag("ShoesEquipPos").GetComponent<Image>();GameObject[] uiSlots = GameObject.FindGameObjectsWithTag("UISlot");foreach (GameObject uiSlot in uiSlots){Image imageComponent = uiSlot.GetComponent<Image>();if (imageComponent != null && imageComponent.sprite == null){ItemKit.AddItem(ConfigManager.Default.Diamond.GetKey);bool isSpriteNull = true;uiSlot.SetActive(isSpriteNull);uiSlot.gameObject.GetComponent<Image>().sprite = childImage.sprite;uiSlot.gameObject.GetComponent<Image>().color = Color.white;childImage.sprite = ShoesIconNormal;childImage.GetComponent<RectTransform>().sizeDelta = new Vector2(90, 90);gm.infoSys.defineValue -= 180;gm.infoSys.CombatValue -= (int)(180 * 3);currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().color = Color.red;currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().text = "战斗力下降" + (int)(180 * 3);currentCanvas.transform.Find("EquipInfo").gameObject.SetActive(true);StartCoroutine(WaitForThreeEquipText());FindObjectOfType<BagExample>().Refresh();FindObjectOfType<UGUICanvas>().Refresh();}}}}IEnumerator WaitForThreeEquipText(){yield return new WaitForSeconds(2);currentCanvas.transform.Find("EquipInfo").gameObject.SetActive(false);}
}
绑定脚本
运行项目
修改脚本:RightClickItem.cs
此时当鼠标悬停在装备栏时就会有设置隐藏的装备面板显示,鼠标离开后消失
RightClickItem.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using QFramework.Example;
using QFramework;
public class RightClickItem : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler{Image childImage;GameManager gm;GameObject currentCanvas;GameObject PlayerNormal;GameObject PlayerUI;void Start(){gm = GameManager.Instance;childImage = transform.Find("Icon").GetComponent<Image>();currentCanvas = GameObject.Find("CurrentCanvas").gameObject;PlayerNormal = GameObject.FindWithTag("Player").gameObject;PlayerUI = GameObject.Find("PlayerUI").gameObject;}public void OnPointerClick(PointerEventData eventData){if (eventData.button == PointerEventData.InputButton.Right){#region 装备武器if (transform.Find("Icon").GetComponent<Image>().sprite != null && transform.Find("Icon").GetComponent<Image>().sprite.name == "equip02"){currentCanvas.transform.Find("EquipUIs/WeaponUI01").gameObject.SetActive(false);Transform weaponEquipPos = GameObject.FindGameObjectWithTag("WeaponEquipPos").transform;weaponEquipPos.gameObject.GetComponent<Image>().sprite = transform.Find("Icon").GetComponent<Image>().sprite;weaponEquipPos.gameObject.GetComponent<Image>().color = Color.white;weaponEquipPos.gameObject.transform.Find("Image").gameObject.SetActive(false);var slot = ItemKit.BagSlots.Find(s => s.Item != null && s.Item.GetKey == "Item_ironSword"); if (slot != null){slot.Item = null;slot.Count = 0;}transform.Find("Icon").GetComponent<Image>().sprite = null;transform.Find("Icon").GetComponent<Image>().color = new Color(45f / 255f, 45f / 255f, 45f / 255f);transform.Find("Icon").GetComponent<Image>().GetComponent<RectTransform>().sizeDelta = new Vector2(90, 90);gm.infoSys.attackValue += 500;gm.infoSys.CombatValue += (int)(500 * 1.3f);currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().color = Color.green;currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().text = "战斗力上升" + (int)(500 * 1.3f);currentCanvas.transform.Find("EquipInfo").gameObject.SetActive(true);FindObjectOfType<BagExample>().Refresh();}#endregion#region 装备护肩if (childImage.sprite != null && childImage.sprite.name == "equip01"){currentCanvas.transform.Find("EquipUIs/ShoulderUI01").gameObject.SetActive(false);Transform shoulderEquipPos = GameObject.FindGameObjectWithTag("ShoulderEquipPos").transform;shoulderEquipPos.gameObject.GetComponent<Image>().sprite = childImage.sprite;shoulderEquipPos.gameObject.GetComponent<Image>().color = Color.white;shoulderEquipPos.gameObject.transform.Find("Image").gameObject.SetActive(false);var slot = ItemKit.BagSlots.Find(s => s.Item != null && s.Item.GetKey == "item_iron"); if (slot != null){slot.Item = null;slot.Count = 0;}transform.Find("Icon").GetComponent<Image>().sprite = null;transform.Find("Icon").GetComponent<Image>().color = new Color(45f / 255f, 45f / 255f, 45f / 255f);transform.Find("Icon").GetComponent<Image>().GetComponent<RectTransform>().sizeDelta = new Vector2(90, 90);PlayerNormal.transform.Find("Player/Armors/Plate1/PlateSet1_Shoulders").gameObject.SetActive(true);PlayerUI.transform.Find("Player111/Armors/Plate1/PlateSet1_Shoulders").gameObject.SetActive(true);gm.infoSys.defineValue += 112;gm.infoSys.CombatValue += (int)(112 * 3.3f);currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().color = Color.green;currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().text = "战斗力上升" + (int)(112 * 3.3f);currentCanvas.transform.Find("EquipInfo").gameObject.SetActive(true);FindObjectOfType<BagExample>().Refresh();}#endregion#region 装备铠甲if (childImage.sprite != null && childImage.sprite.name == "equip03"){currentCanvas.transform.Find("EquipUIs/ClothesUI01").gameObject.SetActive(false);Transform clothesEquipPos = GameObject.FindGameObjectWithTag("ClothesEquipPos").transform;clothesEquipPos.gameObject.GetComponent<Image>().sprite = childImage.sprite;clothesEquipPos.gameObject.GetComponent<Image>().color = Color.white;clothesEquipPos.gameObject.transform.Find("Image").gameObject.SetActive(false);var slot = ItemKit.BagSlots.Find(s => s.Item != null && s.Item.GetKey == "item_clothes"); if (slot != null){slot.Item = null;slot.Count = 0;}transform.Find("Icon").GetComponent<Image>().sprite = null;transform.Find("Icon").GetComponent<Image>().color = new Color(45f / 255f, 45f / 255f, 45f / 255f);transform.Find("Icon").GetComponent<Image>().GetComponent<RectTransform>().sizeDelta = new Vector2(90, 90);PlayerNormal.transform.Find("Player/Armors/Plate1/PlateSet1_Chest").gameObject.SetActive(true);PlayerUI.transform.Find("Player111/Armors/Plate1/PlateSet1_Chest").gameObject.SetActive(true);PlayerNormal.transform.Find("Player/Armors/StarterClothes/Starter_Chest").gameObject.SetActive(false);PlayerUI.transform.Find("Player111/Armors/StarterClothes/Starter_Chest").gameObject.SetActive(false);gm.infoSys.defineValue += 224;gm.infoSys.CombatValue += (int)(224 * 3.6f);currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().color = Color.green;currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().text = "战斗力上升" + (int)(224 * 3.6f);currentCanvas.transform.Find("EquipInfo").gameObject.SetActive(true);FindObjectOfType<BagExample>().Refresh();}#endregion#region 装备裤子if (childImage.sprite != null && childImage.sprite.name == "equip04"){currentCanvas.transform.Find("EquipUIs/PantsUI01").gameObject.SetActive(false);Transform pantsEquipPos = GameObject.FindGameObjectWithTag("PantsEquipPos").transform;pantsEquipPos.gameObject.GetComponent<Image>().sprite = childImage.sprite;pantsEquipPos.gameObject.GetComponent<Image>().color = Color.white;pantsEquipPos.gameObject.transform.Find("Image").gameObject.SetActive(false);var slot = ItemKit.BagSlots.Find(s => s.Item != null && s.Item.GetKey == "Item_pants");if (slot != null){slot.Item = null;slot.Count = 0;}transform.Find("Icon").GetComponent<Image>().sprite = null;transform.Find("Icon").GetComponent<Image>().color = new Color(45f / 255f, 45f / 255f, 45f / 255f);transform.Find("Icon").GetComponent<Image>().GetComponent<RectTransform>().sizeDelta = new Vector2(90, 90);PlayerNormal.transform.Find("Player/Armors/Plate1/PlateSet1_Pants").gameObject.SetActive(true);PlayerUI.transform.Find("Player111/Armors/Plate1/PlateSet1_Pants").gameObject.SetActive(true);PlayerNormal.transform.Find("Player/Armors/StarterClothes/Starter_Pants").gameObject.SetActive(false);PlayerUI.transform.Find("Player111/Armors/StarterClothes/Starter_Pants").gameObject.SetActive(false);gm.infoSys.defineValue += 80;gm.infoSys.CombatValue += (int)(80 * 5);currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().color = Color.green;currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().text = "战斗力上升" + (int)(80 * 5);currentCanvas.transform.Find("EquipInfo").gameObject.SetActive(true);FindObjectOfType<BagExample>().Refresh();}#endregion#region 装备鞋子if (childImage.sprite != null && childImage.sprite.name == "equip05"){currentCanvas.transform.Find("EquipUIs/ShoesUI01").gameObject.SetActive(false);Transform shoesEquipPos = GameObject.FindGameObjectWithTag("ShoesEquipPos").transform;shoesEquipPos.gameObject.GetComponent<Image>().sprite = childImage.sprite;shoesEquipPos.gameObject.GetComponent<Image>().color = Color.white;shoesEquipPos.gameObject.transform.Find("Image").gameObject.SetActive(false);var slot = ItemKit.BagSlots.Find(s => s.Item != null && s.Item.GetKey == "item_shoes");if (slot != null){slot.Item = null;slot.Count = 0;}transform.Find("Icon").GetComponent<Image>().sprite = null;transform.Find("Icon").GetComponent<Image>().color = new Color(45f / 255f, 45f / 255f, 45f / 255f);transform.Find("Icon").GetComponent<Image>().GetComponent<RectTransform>().sizeDelta = new Vector2(90, 90);PlayerNormal.transform.Find("Player/Armors/Plate1/PlateSet1_Boots").gameObject.SetActive(true);PlayerUI.transform.Find("Player111/Armors/Plate1/PlateSet1_Boots").gameObject.SetActive(true);PlayerNormal.transform.Find("Player/Armors/StarterClothes/Starter_Boots").gameObject.SetActive(false);PlayerUI.transform.Find("Player111/Armors/StarterClothes/Starter_Boots").gameObject.SetActive(false);gm.infoSys.defineValue += 180;gm.infoSys.CombatValue += (int)(180 * 3);currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().color = Color.green;currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().text = "战斗力上升" + (int)(180 * 3);currentCanvas.transform.Find("EquipInfo").gameObject.SetActive(true);FindObjectOfType<BagExample>().Refresh();}#endregion#region 装备头盔if (childImage.sprite != null && childImage.sprite.name == "equip06"){currentCanvas.transform.Find("EquipUIs/HatUI01").gameObject.SetActive(false);Transform hatEquipPos = GameObject.FindGameObjectWithTag("HatEquipPos").transform;hatEquipPos.gameObject.GetComponent<Image>().sprite = childImage.sprite;hatEquipPos.gameObject.GetComponent<Image>().color = Color.white;hatEquipPos.gameObject.transform.Find("Image").gameObject.SetActive(false);var slot = ItemKit.BagSlots.Find(s => s.Item != null && s.Item.GetKey == "item_hat");if (slot != null){slot.Item = null;slot.Count = 0;}transform.Find("Icon").GetComponent<Image>().sprite = null;transform.Find("Icon").GetComponent<Image>().color = new Color(45f / 255f, 45f / 255f, 45f / 255f);transform.Find("Icon").GetComponent<Image>().GetComponent<RectTransform>().sizeDelta = new Vector2(90, 90);PlayerNormal.transform.Find("Player/Armors/Plate1/PlateSet1_Helmet").gameObject.SetActive(true);PlayerUI.transform.Find("Player111/Armors/Plate1/PlateSet1_Helmet").gameObject.SetActive(true);PlayerNormal.transform.Find("Player/Armors/Plate1/PlateSet1_Gloves").gameObject.SetActive(true);PlayerUI.transform.Find("Player111/Armors/Plate1/PlateSet1_Gloves").gameObject.SetActive(true);gm.infoSys.defineValue += 200;gm.infoSys.CombatValue += (int)(200 * 3);currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().color = Color.green;currentCanvas.transform.Find("EquipInfo").GetComponent<Text>().text = "战斗力上升" + (int)(200 * 3);currentCanvas.transform.Find("EquipInfo").gameObject.SetActive(true);FindObjectOfType<BagExample>().Refresh();}#endregion}}public void OnPointerEnter(PointerEventData eventData){#region 装备武器if (childImage.sprite != null && childImage.sprite.name == "equip02"){currentCanvas.transform.Find("EquipUIs/WeaponUI01").gameObject.SetActive(true);}#endregion#region 装备护肩if (childImage.sprite != null && childImage.sprite.name == "equip01"){currentCanvas.transform.Find("EquipUIs/ShoulderUI01").gameObject.SetActive(true);}#endregion#region 装备铠甲if (childImage.sprite != null && childImage.sprite.name == "equip03"){currentCanvas.transform.Find("EquipUIs/ClothesUI01").gameObject.SetActive(true);}#endregion#region 装备裤子if (childImage.sprite != null && childImage.sprite.name == "equip04"){currentCanvas.transform.Find("EquipUIs/PantsUI01").gameObject.SetActive(true);}#endregion#region 装备鞋子if (childImage.sprite != null && childImage.sprite.name == "equip05"){currentCanvas.transform.Find("EquipUIs/ShoesUI01").gameObject.SetActive(true);}#endregion#region 装备头盔if (childImage.sprite != null && childImage.sprite.name == "equip06"){currentCanvas.transform.Find("EquipUIs/HatUI01").gameObject.SetActive(true);}#endregion}public void OnPointerExit(PointerEventData eventData){#region 装备武器if (childImage.sprite != null && childImage.sprite.name == "equip02"){currentCanvas.transform.Find("EquipUIs/WeaponUI01").gameObject.SetActive(false);}#endregion#region 装备护肩if (childImage.sprite != null && childImage.sprite.name == "equip01"){currentCanvas.transform.Find("EquipUIs/ShoulderUI01").gameObject.SetActive(false);}#endregion#region 装备铠甲if (childImage.sprite != null && childImage.sprite.name == "equip03"){currentCanvas.transform.Find("EquipUIs/ClothesUI01").gameObject.SetActive(false);}#endregion#region 装备裤子if (childImage.sprite != null && childImage.sprite.name == "equip04"){currentCanvas.transform.Find("EquipUIs/PantsUI01").gameObject.SetActive(false);}#endregion#region 装备鞋子if (childImage.sprite != null && childImage.sprite.name == "equip05"){currentCanvas.transform.Find("EquipUIs/ShoesUI01").gameObject.SetActive(false);}#endregion#region 装备头盔if (childImage.sprite != null && childImage.sprite.name == "equip06"){currentCanvas.transform.Find("EquipUIs/HatUI01").gameObject.SetActive(false);}#endregion}
}
运行项目
接下来的文章内容:
1.六件装备穿戴齐全后的炫光效果
2.窗口可拖拽脚本
3.点击名称寻找地点功能
4.隐藏怪物的生成
5.怪物I攻击范围内的主动攻击
6.掉落坐骑蛋的获取
7.异步传送转换场景
以及开放回合制、坐骑系统、宠物系统、背包系统、神炼系统、商城系统、Boss的目标跟随任务导航系统以及UI播放3D动画效果等等。
具体项目运行效果请关注water1024的b站视频项目演示《破碎纪元》
【Unity回合2.5D】破碎纪元_单机游戏热门视频 (bilibili.com)https://www.bilibili.com/video/BV1rZY4e9Ebs/?spm_id_from=333.999.0.0&vd_source=547091a95b03acfa8e8a9e46ef499cd6