我应该使用什么代替OnMouseXXX事件处理程序? [英] What shall I use instead of OnMouseXXX event handlers?

查看:349
本文介绍了我应该使用什么代替OnMouseXXX事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个小应用程序,在其中一些对象中使​​用了OnMouseDrag和OnMouseDown.我认为这将是最好的选择,因为此方法不在Update方法之列.但是,在编译应用程序时,Unity说游戏脚本或其他自定义代码包含OnMouseXXX事件处理程序.此类处理程序的存在可能会影响手持设备的性能."

I've build a little app where I use OnMouseDrag and OnMouseDown in some objects. I thought that it would be the best option because this methods were out of the Update method. However at compiling the app Unity says "Game scripts or other custom code contains OnMouseXXX event handlers. Presence of such handlers might impact performance on handheld devices."

那么...我应该用什么代替OnMouseDrag和OnMouseDown?

So... what should I use instead of OnMouseDrag and OnMouseDown?

我已经看到我可以使用Input.GetMouseButtonDown.但是要做到这一点,我将不得不向具有OnMouseDown的每个对象添加一个Update方法.是否比拥有OnMouseDrag更糟糕?

I've seen that i could use Input.GetMouseButtonDown. But to do this I will have to add an Update method to every object that has the OnMouseDown. Isn't it worst than having the OnMouseDrag?

而且,为什么OnMouseXXX事件对手持设备的性能不利?

And, why are OnMouseXXX events bad for the performance in on handheld devices?

推荐答案

而且,为什么OnMouse事件对掌上电脑的性能不利? 设备?

And, why are OnMouse events bad for the performance in on handheld devices?

它们的性能还不错.我没有遇到过任何性能下降的问题,但是出现此错误的原因是因为Unity可能会在移动设备上做更多的工作才能使其正常工作.并非出于此目的,但您不应仅通过使用它们而注意到任何性能下降.

They are not bad for performance. I haven't experienced any bad performance with them but the reason you are getting that error is because it is likely that Unity is doing more work on mobile in order to get it working. It's not made for that but you should't notice any performance hit just by using them.

那么...我应该用什么代替OnMouseDrag和OnMouseDown?

So... what should I use instead of OnMouseDrag and OnMouseDown?

OnMouseDragOnMouseDown函数是专门为台式机设计的.是的,它们也可以在移动设备上使用,但应避免使用.

The OnMouseDrag and OnMouseDown functions were specifically made for desktops. Yes, they work on mobile devices too but should be avoided.

应避免使用它们,因为它们不适用于所有对象.例如,它们不适用于新的UGUI系统.

They should be avoided because they don't work for all Objects. For example, they don't work on the new UGUI system.

您应该使用OnBeginDragOnEndDrag,这两个都需要实现IBeginDragHandlerIEndDragHandler.

You should be using OnBeginDrag and OnEndDrag which both requires you implement IBeginDragHandler and IEndDragHandler.

OnBeginDragOnEndDrag函数将在包括3D,2D和UI在内的所有GameObjects上运行.对于2D,您需要将Physics2DRaycaster连接到相机.对于3D,必须在相机上附加PhysicsRaycaster.

The OnBeginDrag and OnEndDrag functions will work on all GameObjects including 3D, 2D and the UI. For 2D you need to attach Physics2DRaycaster to the camera. For 3D, you have to attach PhysicsRaycaster to the camera.

请参见文章,了解如何使其在任何GameObject上都能正常工作.

See this post for how to get it work on any GameObject.

using UnityEngine.EventSystems;

public class Dragger: MonoBehaviour,
    IBeginDragHandler, IEndDragHandler
{
    public void OnBeginDrag(PointerEventData eventData)
    {
        Debug.Log("Drag Begin");
    }

    public void OnDrag(PointerEventData eventData)
    {
        Debug.Log("Dragging");
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Debug.Log("Drag Ended");
    }
}

这篇关于我应该使用什么代替OnMouseXXX事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆