209 lines
7.3 KiB
C#
209 lines
7.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
using UnityEngine.UI;
|
||
|
||
public class UIItem : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
|
||
{
|
||
public bool NoBag;
|
||
private RectTransform rectTransform;
|
||
public string ItemName = "";
|
||
|
||
public int Count = 1;
|
||
private bool canDrag;
|
||
|
||
public void OnBeginDrag(PointerEventData eventData)
|
||
{
|
||
if (ItemName != "")
|
||
{
|
||
RemainsKind kind = BagControl.Instance.bag.GetRemainsItem(ItemName).kind;
|
||
if (kind == RemainsKind.YishiRemains)
|
||
{
|
||
canDrag = true;
|
||
rectTransform = BagControl.Instance.ChangeDragUI(ItemName);
|
||
rectTransform.anchoredPosition = eventData.position;
|
||
}
|
||
else if (kind == RemainsKind.ScreeenRemains)
|
||
{
|
||
|
||
if (ItemName == "太阳神之眼")
|
||
{
|
||
GameObject obj = Instantiate(BagControl.Instance.bag.SunRemains);
|
||
obj.name = ItemName;
|
||
}
|
||
else
|
||
{
|
||
GameObject obj = Instantiate(BagControl.Instance.bag.OtherRemains);
|
||
obj.name = ItemName;
|
||
obj.GetComponent<BuildRemains>().ChangeRemainskind(ItemName);
|
||
// obj.GetComponent<SpriteRenderer>().sprite = transform.GetComponent<Image>().sprite;
|
||
}
|
||
BagControl.Instance.CloseBagTwo();
|
||
BagControl.Instance.bag.CutItems(ItemName);
|
||
BagControl.Instance.Refresh();
|
||
}
|
||
}
|
||
}
|
||
public void OnDrag(PointerEventData eventData)
|
||
{
|
||
if (canDrag)
|
||
{
|
||
rectTransform.anchoredPosition = eventData.position;
|
||
}
|
||
}
|
||
|
||
public void OnEndDrag(PointerEventData eventData)
|
||
{
|
||
if (canDrag)
|
||
{
|
||
YishiRemainsEvent(eventData);
|
||
canDrag = false;
|
||
rectTransform.gameObject.SetActive(false);
|
||
}
|
||
|
||
|
||
}
|
||
public void YishiRemainsEvent(PointerEventData eventData) //仪式遗物
|
||
{
|
||
// 发射一条射线检测鼠标下的第一个UI
|
||
RaycastResult result = RaycastUI(eventData);
|
||
// 检测UI元素的tag
|
||
if (result.gameObject != null && result.gameObject.CompareTag("bagitem")) // 替换 "YourTargetTag" 为你想要检测的 tag
|
||
{
|
||
if (result.gameObject.TryGetComponent(out UIItem UiItem))
|
||
{
|
||
if (UiItem != this)//不等于自身
|
||
{
|
||
if (NoBag)//自身不是背包
|
||
{
|
||
if (!UiItem.NoBag)//是背包item
|
||
{
|
||
if (UiItem.ItemName == ItemName || UiItem.ItemName == "" || BagControl.Instance.bag.GetRemainsItem(UiItem.ItemName).kind != RemainsKind.YishiRemains)//两种类相同(合并)或者item为空
|
||
{
|
||
BagControl.Instance.RemoveIn(ItemName);//移入背包
|
||
Refresh();
|
||
}
|
||
else
|
||
{
|
||
BagControl.Instance.RemoveIn(ItemName);//先移入背包
|
||
BagControl.Instance.RemoveOut(UiItem.ItemName);//再移出背包
|
||
ItemName = UiItem.ItemName;
|
||
GetUI();
|
||
}
|
||
|
||
}
|
||
else//两个都可交互(都不为背包item)
|
||
{
|
||
if (UiItem.ItemName == "")
|
||
{
|
||
UiItem.ItemName = ItemName;
|
||
Refresh();
|
||
UiItem.GetUI();
|
||
}
|
||
else
|
||
{
|
||
ExChange(UiItem);//交换
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
else//自身是背包
|
||
{
|
||
if (UiItem.NoBag)//另一个不是背包
|
||
{
|
||
if (UiItem.ItemName == "")//空的,
|
||
{
|
||
Debug.LogWarning("背包,不是背包" + ItemName);
|
||
string name = ItemName;
|
||
BagControl.Instance.RemoveOut(ItemName);//直接移出包
|
||
UiItem.ItemName = name;
|
||
UiItem.GetUI();
|
||
}
|
||
else//不为空,也需交换
|
||
{
|
||
string name = ItemName;
|
||
BagControl.Instance.RemoveIn(UiItem.ItemName);//移入背包
|
||
BagControl.Instance.RemoveOut(ItemName);
|
||
UiItem.ItemName = ItemName;
|
||
UiItem.GetUI();
|
||
}
|
||
}
|
||
}
|
||
BagControl.Instance.Refresh();
|
||
YiShiUI.Instance.PrayerText();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
private RaycastResult RaycastUI(PointerEventData eventData)
|
||
{
|
||
|
||
// 存储射线结果
|
||
List<RaycastResult> results = new List<RaycastResult>();
|
||
|
||
// 创建射线检测器,找到与鼠标位置相交的UI元素
|
||
GraphicRaycaster raycaster = BagControl.Instance.graphicRaycaster; // 获取当前Canvas上的GraphicRaycaster
|
||
|
||
if (raycaster != null)
|
||
{
|
||
Debug.LogWarning("---射线检测没问题-");
|
||
raycaster.Raycast(eventData, results);
|
||
Debug.LogWarning(results.Count > 0);
|
||
if (results.Count > 0)
|
||
{
|
||
Debug.LogWarning(results[0].gameObject.name);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("---射线检测有问题-");
|
||
}
|
||
|
||
// 返回列表的第一个元素,如果没有元素则返回空的RaycastResult
|
||
return results.Count > 0 ? results[0] : new RaycastResult();
|
||
}
|
||
|
||
public void Refresh()
|
||
{
|
||
transform.GetComponent<Image>().sprite = BagControl.Instance.Blocksprite;
|
||
transform.Find("数量/Text").GetComponent<Text>().text = "";
|
||
ItemName = "";
|
||
}
|
||
|
||
public void GetUI()
|
||
{
|
||
transform.GetComponent<Image>().sprite = BagControl.Instance.bag.GetSprite(ItemName);
|
||
|
||
transform.Find("数量/Text").GetComponent<Text>().text = Count.ToString();
|
||
}
|
||
|
||
public void AwkaeItem(string name, int count)
|
||
{
|
||
ItemName = name;
|
||
Count = count;
|
||
GetUI();
|
||
}
|
||
/// <summary>
|
||
/// 交换
|
||
/// </summary>
|
||
private void ExChange(UIItem uIItem)
|
||
{
|
||
string Aitemname = ItemName;
|
||
ItemName = uIItem.ItemName;
|
||
uIItem.ItemName = Aitemname;
|
||
GetUI();
|
||
uIItem.GetUI();
|
||
}
|
||
|
||
public void OnPointerEnter(PointerEventData eventData)
|
||
{
|
||
BagControl.Instance.MiaosuRemains(ItemName);
|
||
}
|
||
|
||
public void OnPointerExit(PointerEventData eventData)
|
||
{
|
||
BagControl.Instance.MiaosuRemains("");
|
||
}
|
||
} |