简体   繁体   English

在 Unity 中注册单击并按住磁贴

[英]Register click and hold on a tile in Unity

I have a procedurally generated tilemap that the player can interact with through mouse clicks.我有一个程序生成的瓷砖地图,玩家可以通过鼠标点击与之交互。 Each time the player clicks I get its screen position convert it to world cords and find the correct tile.每次玩家点击我都会得到它的屏幕 position 将其转换为世界线并找到正确的瓷砖。 This is fine but only works with single clicks and has no idea about a continuous hold of the mouse.这很好,但仅适用于单击,并且不知道连续按住鼠标。

I am trying to figure out how to have a timed system like click and hold, both Minecraft and Terraira have ideas of break progress for each block / tile which is what I really want.我试图弄清楚如何拥有一个像点击并按住这样的定时系统,Minecraft 和 Terraira 都有我真正想要的每个块/瓷砖的突破进度的想法。

Anyone have an Idea how to track this and reset it when the player releases the mouse.任何人都知道如何在玩家释放鼠标时跟踪并重置它。

Instead of doing your own raycasting when the pointer goes down and then calling your interaction method, add a monobehaviour to each of your tiles that implements the IPointerDownHandler, IPointerExitHandler, and IPointerUp handler.不要在指针下降时进行自己的光线投射,然后调用您的交互方法,而是为每个实现 IPointerDownHandler、IPointerExitHandler 和 IPointerUp 处理程序的图块添加一个单一行为。 Make sure you have an eventhandler in the scene, and a physcisRaycaster on your active camera!确保您在场景中有一个事件处理程序,并在您的活动相机上有一个 physcisRaycaster!

OnPointerDown -> set a time stamp to time.unscaledTime OnPointerDown -> 将时间戳设置为 time.unscaledTime

OnPointerExit -> reset timestamp to -1 or what ever (some religious coders would prefere a nullable float, so then set it to null), to cancel the interaction OnPointerExit -> 将时间戳重置为 -1 或其他(一些宗教编码人员更喜欢可以为空的浮点数,因此将其设置为空),以取消交互

OnPointerUp -> if your timer is > -1 (or NOT null), check if time.unscaledTime - your time stamp > your time threshold, if so -> call your interaction method. OnPointerUp -> 如果您的计时器 > -1(或不为空),请检查 time.unscaledTime - 您的时间戳 > 您的时间阈值,如果是 -> 调用您的交互方法。 Reset the timer重置计时器

More logic can be added of course.当然可以添加更多逻辑。

EDIT: from your comment, since you don't want to have the logic per-tile, instead, when you click, save the tile that is “in world position of the screen position click”, and WHILE the mouse is down, check (in some frequency, per frame or every few ms), if the pointer is STILL on the same tile.编辑:从您的评论中,由于您不希望每个图块都有逻辑,因此,当您单击时,保存“在屏幕 position 单击的世界 position 中”的图块,并在鼠标按下时检查(以某种频率,每帧或每隔几毫秒),如果指针仍然在同一个图块上。 if so, keep your timer running, otherwise reset it.如果是这样,请保持计时器运行,否则重置它。 if the timer expires, your interaction may start.如果计时器到期,您的互动可能会开始。 if the pointer goes up, reset如果指针上升,重置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM