英文:
Weird things going on with my Drag n' Drop Inventory System
问题
以下是翻译好的部分:
嗨,亲爱的互联网朋友们,
我偶然发现了我的拖放式库存系统出现了一个奇怪的问题,有时会捡起光标所悬停的对象之外的第二个对象。所有相关的脚本都在下面链接中。
MouseFollower:它不断地跟随光标,充当对象可以使用的父级,每次只能使用一个对象。代码链接:https://pastebin.com/HQ5FHwW4
ItemScript:这是库存UI中的每个项目都具有的脚本,这些项目是奇怪的长方块。代码链接:https://pastebin.com/iH0kBDNc
还有一个InventoryManager,画布上有它,但目前它只负责在打开和关闭库存时进行管理,没有其他功能。
我尝试添加了一个名为“MouseVisualizer”的小红色框,用来显示MouseFollower的当前位置,以查看问题是否与其位置有关,但结果不是这样。如果需要更多信息,请告诉我。
英文:
A video showcasing the problem: https://clipchamp.com/watch/73zJjlfc2X4
Hi there fellow Internet People,
I stumbled upon a weird problem going on with my Drag n' Drop Inventory System, where it sometimes, not always, picks up a second object that the one the cursor is hovering over. All the related scripts are linked below.
The MouseFollower, which is constantly following the cursor and acts as a parent the object can use, and only one at a time. The code is "MouseFollower".: https://pastebin.com/HQ5FHwW4
The ItemScript, which is the script that every item in the inventory UI has, which are the weird long blocks. The code is "ItemScript".: https://pastebin.com/iH0kBDNc
There is also an InventoryManager which the canvas has, but it is currently only managing whenever you open and close the inventory and nothing else.
I tried to add the small red box called "MouseVisualizer" that gives an idea of where the current position of the MouseFollower is, to see if the problem was something with it's position being somewhere else, but no. You can also see some of the Hierarchy at the very end of the video if that helps. If you need any more information, please let me know.
答案1
得分: 0
我用你的代码制作了一个非常简单的库存,我意识到当你悬停在一个物品上时,cursorHasEntered会被设置为true,现在这没有什么问题,但当鼠标离开时cursorHasEntered仍然保持为true。
你应该改成这样,代码相同,但我们添加了IPointerExitHandler,它可以检测指针离开物品:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ItemScript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
private bool CursorHasEntered = false;
private bool CanBeDropped = false;
public static bool MouseBusy = false;
public GameObject mousefollower;
public GameObject items;
// 当光标开始悬停在游戏对象上时检测
public void OnPointerEnter(PointerEventData pointerEventData)
{
CursorHasEntered = true;
Debug.Log("鼠标进入");
}
public void OnPointerExit(PointerEventData eventData)
{
CursorHasEntered = false;
Debug.Log("再见");
}
void Update()
{
if (Input.GetKey(KeyCode.Mouse0) == true && CursorHasEntered == true && MouseBusy == false)
{
CanBeDropped = true;
transform.SetParent(mousefollower.transform);
MouseBusy = true;
}
if (Input.GetKey(KeyCode.Mouse0) == false && CanBeDropped == true)
{
CursorHasEntered = false;
CanBeDropped = false;
StartCoroutine(NewParent());
MouseBusy = false;
}
}
public IEnumerator NewParent()
{
yield return new WaitForSeconds(0.1f);
transform.SetParent(items.transform);
}
}
希望这对你有帮助。
英文:
I made a very simple inventory using your code and i realized when you hover over an item cursorHasEntered is set to true , now there is nothing wrong with that but when the mouse exit cursorHasEntered is still set to true
you should do this instead , its the same code but we added IPointerExitHandler which detects when our pointer leaves the item
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ItemScript : MonoBehaviour, IPointerEnterHandler , IPointerExitHandler
{
private bool CursorHasEntered = false;
private bool CanBeDropped = false;
public static bool MouseBusy = false;
public GameObject mousefollower;
public GameObject items;
//Detect if the Cursor starts to pass over the GameObject
public void OnPointerEnter(PointerEventData pointerEventData)
{
CursorHasEntered = true;
Debug.Log("Mouse Entered");
}
public void OnPointerExit(PointerEventData eventData)
{
CursorHasEntered = false;
Debug.Log("Bye");
}
void Update()
{
if (Input.GetKey(KeyCode.Mouse0) == true && CursorHasEntered == true && MouseBusy == false)
{
CanBeDropped = true;
transform.SetParent(mousefollower.transform);
MouseBusy = true;
}
if (Input.GetKey(KeyCode.Mouse0) == false && CanBeDropped == true)
{
CursorHasEntered = false;
CanBeDropped = false;
StartCoroutine(NewParent());
MouseBusy = false;
}
}
public IEnumerator NewParent()
{
yield return new WaitForSeconds(0.1f);
transform.SetParent(items.transform);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论