主要用到委托和事件
C#委托官网API:
event:Link
委托:Link
鼠标事件方法,需要继承这两个接口IPointerEnterHandler,IPointerExitHandler
从而生成以下两个方法
OnPointerEnter()
OnPointerExit()
将以下代码挂载到背包的Slot上,并拖拽需要的public 背包和显示信息的面板,这里主要介绍思路,代码具体数据获取部分可自行修改
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System;public class onm ouseEnterShowItemInfo : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler { public event Action<string,string> UpdateItemPanel;//声明委托 [Header("背包本地数据库")] public Bag myBag; [Header("背包需要关联的面板")] public GameObject itemInfoPanel; public bool itemInfoPanelState; private void Awake() { itemInfoPanel = GameObject.Find("人物画布/背包/物品信息栏面板"); UpdateItemPanel += UpdateItemPanelSetting;//订阅委托 } public void OnPointerEnter(PointerEventData eventData) { //获取当前鼠标进入对象的名称做比对 if(eventData.pointerEnter.gameObject.transform.parent.name == "item" ){ itemInfoPanel.SetActive(itemInfoPanelState = true);//显示信息面板 //找到当前鼠标下标ID var slotID = eventData.pointerEnter.gameObject.transform.parent.parent.GetComponent<Slot>().slotID; UpdateItemPanel?.Invoke(myBag.itemList[slotID].itemName,myBag.itemList[slotID].itemInfo);//发部委托 } } public void OnPointerExit(PointerEventData eventData) { itemInfoPanel.SetActive(itemInfoPanelState = false);//隐藏物品详情面板 } //订阅者方法(获取到委托传递过来的参数) public void UpdateItemPanelSetting(string itemName,string itemInfo){ //找到需要修改的数据 GameObject.Find("人物画布/背包/物品信息栏面板/名称/Text").GetComponent<Text>().text = itemName; GameObject.Find("人物画布/背包/物品信息栏面板/info/物品详情信息").GetComponent<Text>().text = itemInfo; } }
效果
标签:背包,委托,void,unity,鼠标悬停,物品,面板,public From: https://www.cnblogs.com/alanshreck/p/16921040.html