此方法只是用於學習和實驗所以細節不必要求
一、Ui設置。
畫布配置如下:

布局:

說明:
畫布里面首先建立一個panel命名為weapon1,在其內部再建立4個panel用於裝備的卡槽,裝備以image來顯示,並且添加自定義hp腳本(hp腳本主要配置物品的名稱、稀有度、說明)。message主要用來顯示武器信息。image配置如下:

weapon1配置此處使用兩種方法來檢測鼠標事件(1.直接添加event trigger組建綁定函數 2.在dragManager代碼中監聽):

二、代碼。
dragManager
//繼承接口實現鼠標進入和移除事件 public class dragManager : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler { //定義偏移量,用來記錄鼠標和裝備圖片的偏移 public Vector3 offet; //提示武器信息即message public GameObject g; //是否拖放成功 private bool massger; public Action a; public Action b; //拖拽武器 public GameObject game; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (a != null) a(); // print(game.activeSelf); } //開始拖拽確定物體 public void beginDrag() { for (int i = 0; i < transform.childCount; i++) { RectTransform rect =(RectTransform) transform.GetChild(i); if (rect.rect.Contains(Input.mousePosition - rect.position) && rect.GetChild(0).gameObject.activeSelf) { offet = Input.mousePosition - rect.position; g = rect.GetChild(0).gameObject; return; } } } //釋放 public void endDrag() { massger = false; for (int i = 0; i < transform.childCount; i++) { RectTransform rect = (RectTransform)transform.GetChild(i); if (rect.rect.Contains(Input.mousePosition - rect.position) && !rect.GetChild(0).gameObject.activeSelf && g!=null) { rect.GetChild(0).GetComponent<Image>().sprite=g.GetComponent<Image>().sprite; g.GetComponent<UnityEngine.UI.Image>().sprite = null; g.gameObject.SetActive(false); rect.GetChild(0).gameObject.SetActive(true); massger = true; g.GetComponent<RectTransform>().localPosition = Vector3.zero; g = null;offet = Vector3.zero; return; } } if (!massger && g!=null) { g.GetComponent<RectTransform>().localPosition = Vector3.zero; g = null; offet = Vector3.zero; } } //拖拽中 public void draging() { if (g != null) { g.GetComponent<RectTransform>().position = Input.mousePosition - offet; } } //鼠標進入 public void OnPointerEnter(PointerEventData eventData) { a += NewMethod; } private void NewMethod() { for (int i = 0; i < transform.childCount; i++) { RectTransform rect = (RectTransform)transform.GetChild(i); if (rect.rect.Contains(Input.mousePosition - rect.position) && rect.GetChild(0).gameObject.activeSelf) { Text t = game.transform.GetChild(0).GetComponent<Text>(); hp h= rect.GetChild(0).GetComponent<hp>(); string s= "<color=" + h.color + ">"+ h.name +"</color>\n"+h.description; t.text =s.ToString(); break; } } game.transform.position = new Vector3(Input.mousePosition.x + game.GetComponent<RectTransform>().rect.width / 2, Input.mousePosition.y - game.GetComponent<RectTransform>().rect.height / 2, 0); if(!game.activeSelf)game.SetActive(true); } //鼠標移出 public void OnPointerExit(PointerEventData eventData) { b += NewMethod2; b(); } private void NewMethod2() { game.SetActive(false); b = null; a = null; } }
三、運行結果有點丑,但只是實驗用。


四、解決個別問題。屏幕放大圖片信息一直閃爍。

將text的Raycast Target取消勾選。
