使用Unity3D的IPointerDownHandler方法,但使用"整个屏幕"

Fat*_*tie 18 c# touch unity-game-engine

在Unity中,你需要检测场景中某些东西的手指触摸(手指绘图).

在现代Unity中执行此操作的唯一方法非常简单:


第1步.在该物体上放置一个对撞机.("地面"或其他任何东西.)1

步骤2.在相机的"检查器"面板上,单击以添加物理雷达(相关的2D或3D).

步骤3.只需使用下面的示例A中的代码.

(提示 - 不要忘记确保有一个EventSystem ......有时Unity会自动添加一个,有时不会!)


太棒了,不容易.Unity最终通过UI层正确处理un /传播.在桌面,设备,编辑器等上均匀而完美地工作.万岁Unity.

都好.但是如果你想在屏幕上画画呢?

因此,您非常简单地想要"在屏幕上"从用户那里滑动/触摸/绘图.(例如,简单地说就是操作轨道摄像机.)就像在任何普通的3D游戏中,摄像机四处奔跑并移动.

你不希望手指在世界空间中的某个物体上的位置,你只需要抽象的"手指运动"(即在玻璃上的位置).

你用什么对撞机?没有对撞机你能做到吗?由于这个原因,添加对撞机似乎很愚蠢.

我们做的是这样的:

我只是做了某种平面对撞机,实际上是把它安装在相机下面.所以它只是坐在相机平截头体上并完全覆盖屏幕.

在此输入图像描述

(对于代码,则不需要使用ScreenToWorldPoint,因此只需使用示例B中的代码 - 非常简单,效果很好.)

我的问题是,使用我所描述的"镜头下相机"似乎有点奇怪,只是为了触摸玻璃.

这是什么交易?

(注意 - 请不要回答涉及Unity古老的"Touches"系统,这个系统今天对于真实项目无法使用,你不能忽视.UI使用传统方法.)


代码示例A - 在场景对象上绘图.使用ScreenToWorldPoint.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;

 public class FingerMove:MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
 {
     public void OnPointerDown (PointerEventData data)
     {
         Debug.Log("FINGER DOWN");
         prevPointWorldSpace =
                 theCam.ScreenToWorldPoint( data.position );
     }

     public void OnDrag (PointerEventData data)
     {
         thisPointWorldSpace =
                theCam.ScreenToWorldPoint( data.position );
         realWorldTravel =
                thisPointWorldSpace - prevPointWorldSpace;
         _processRealWorldtravel();
         prevPointWorldSpace = thisPointWorldSpace;
     }

     public void OnPointerUp (PointerEventData data)
     {
         Debug.Log("clear finger...");
     }
Run Code Online (Sandbox Code Playgroud)

代码示例B ...您只关心用户在设备的玻璃屏幕上执行的操作.这里更容易:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;

 public class FingerMove:MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
 {
     private Vector2 prevPoint;
     private Vector2 newPoint;
     private Vector2 screenTravel;

     public void OnPointerDown (PointerEventData data)
     {
         Debug.Log("FINGER DOWN");
         prevPoint = data.position;
     }

     public void OnDrag (PointerEventData data)
     {
         newPoint = data.position;
         screenTravel = newPoint - prevPoint;
         prevPoint = newPoint;
         _processSwipe();
     }

     public void OnPointerUp (PointerEventData data)
     {
         Debug.Log("FINEGR UP...");
     }

     private void _processSwipe()
     {
         // your code here
         Debug.Log("screenTravel left-right.. " + screenTravel.x.ToString("f2"));
     }
 }
Run Code Online (Sandbox Code Playgroud)

1如果你刚刚接触Unity:在那个步骤非常可能,让它成为一个名为"Draw"的层; 在物理设置中,"绘制"与任何东西互动; 在第二步中,Raycaster将图层设置为"Draw".

Pro*_*mer 6

首先,您需要了解有一些3方法可以使用以下OnPointerDown函数检测对象的单击:

1.您需要一个UI组件才能检测到该OnPointerDown功能的点击.这适用于其他类似的UI事件.

2.OnPointerDown使用2D/Sprite GameObject上的函数检测点击的另一种方法是附加Physics2DRaycaster到Camera,然后OnPointerDown 在单击时调用.请注意,必须将2D碰撞器连接到它上面.

3.如果这是带有碰撞器而非2D碰撞器的3D物体,则必须PhysicsRaycaster连接到摄像机才能OnPointerDown调用该功能.

使用第一种方法执行此操作似乎更合理,而不是覆盖屏幕的大型对撞机或2D对撞机.您所要做的就是创建一个CanvasPanel GameObject,并将Image整个屏幕上的组件连接到它.

Dude I just don't see using .UI as a serious solution: imagine we're doing a big game and you're leading a team that is doing all the UI (I mean buttons, menus and all). I'm leading a team doing the walking robots. I suddenly say to you "oh, by the way, I can't handle touch ("!"), could you drop in a UI.Panel, don't forget to keep it under everything you're doing, oh and put one on any/all canvasses or cameras you swap between - and pass that info back to me OK!" :) I mean it's just silly. One can't essentially say: "oh, Unity doesn't handle touch"

不像你描述它的方式那么难.你可以写很长的代码,就可以创建一个Canvas,PanelImage.将图像alpha更改为0.您所要做的就是将该代码附加到相机或空GameObject,它将在播放模式下自动执行所有这些操作.

使每个想要在屏幕上接收事件的GameObject订阅它,然后使用ExecuteEvents.Execute该事件发送到附加到该GameObject的脚本中的所有接口.

例如,下面的示例代码将OnPointerDown事件发送到名为target的GameObject.

ExecuteEvents.Execute<IPointerDownHandler>(target,
                              eventData,
                              ExecuteEvents.pointerDownHandler);
Run Code Online (Sandbox Code Playgroud)

你会遇到的问题:

隐藏Image组件将阻止其他UI或GameObject接收光线投射.这是这里最大的问题.

Solution:

Since it will cause some blocking problems, it is better to make the Canvas of the Image to be on top of everything. This will make sure that it is now 100 blocking all other UI/GameObject. Canvas.sortingOrder = 12; should help us do this.

Each time we detect an event such as OnPointerDown from the Image, we will manually send resend the OnPointerDown event to all other UI/GameObjects beneath the Image.

First of all, we throw a raycast with GraphicRaycaster(UI), Physics2DRaycaster(2D collider), PhysicsRaycaster(3D Collider) and store the result in a List.

Now, we loop over the result in the List and resend the event we received by sending artificial event to the results with:

ExecuteEvents.Execute<IPointerDownHandler>(currentListLoop,
                              eventData,
                              ExecuteEvents.pointerDownHandler);
Run Code Online (Sandbox Code Playgroud)

Other problems you will run into:

您将无法在Toggle组件 上发送模拟事件GraphicRaycaster.这是Unity中的一个错误.我花了2天才意识到这一点.

也无法将伪滑块移动事件发送到Slider组件.我不知道这是不是一个错误.

除了上面提到的这些问题,我能够实现这一点.它分为3部分.只需创建一个文件夹并将所有脚本放入其中.

脚本:

1.WholeScreenPointer.cs- 创建Canvas,GameObject和隐藏的脚本的主要部分Image.它完成所有复杂的工作,以确保Image始终覆盖屏幕.它还将事件发送到所有订阅GameObject.

public class WholeScreenPointer : MonoBehaviour
{
    //////////////////////////////// SINGLETON BEGIN  ////////////////////////////////
    private static WholeScreenPointer localInstance;

    public static WholeScreenPointer Instance { get { return localInstance; } }
    public EventUnBlocker eventRouter;

    private void Awake()
    {
        if (localInstance != null && localInstance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            localInstance = this;
        }
    }
    //////////////////////////////// SINGLETON END  ////////////////////////////////


    //////////////////////////////// SETTINGS BEGIN  ////////////////////////////////
    public bool simulateUIEvent = true;
    public bool simulateColliderEvent = true;
    public bool simulateCollider2DEvent = true;

    public bool hideWholeScreenInTheEditor = false;
    //////////////////////////////// SETTINGS END  ////////////////////////////////


    private GameObject hiddenCanvas;

    private List<GameObject> registeredGameobjects = new List<GameObject>();

    //////////////////////////////// USEFUL FUNCTIONS BEGIN  ////////////////////////////////
    public void registerGameObject(GameObject objToRegister)
    {
        if (!isRegistered(objToRegister))
        {
            registeredGameobjects.Add(objToRegister);
        }
    }

    public void unRegisterGameObject(GameObject objToRegister)
    {
        if (isRegistered(objToRegister))
        {
            registeredGameobjects.Remove(objToRegister);
        }
    }

    public bool isRegistered(GameObject objToRegister)
    {
        return registeredGameobjects.Contains(objToRegister);
    }

    public void enablewholeScreenPointer(bool enable)
    {
        hiddenCanvas.SetActive(enable);
    }
    //////////////////////////////// USEFUL FUNCTIONS END  ////////////////////////////////

    // Use this for initialization
    private void Start()
    {
        makeAndConfigWholeScreenPinter(hideWholeScreenInTheEditor);
    }

    private void makeAndConfigWholeScreenPinter(bool hide = true)
    {
        //Create and Add Canvas Component
        createCanvas(hide);

        //Add Rect Transform Component
        //addRectTransform();

        //Add Canvas Scaler Component
        addCanvasScaler();

        //Add Graphics Raycaster Component
        addGraphicsRaycaster();

        //Create Hidden Panel GameObject
        GameObject panel = createHiddenPanel(hide);

        //Make the Image to be positioned in the middle of the screen then fix its anchor
        stretchImageAndConfigAnchor(panel);

        //Add EventForwarder script
        addEventForwarder(panel);

        //Add EventUnBlocker
        addEventRouter(panel);

        //Add EventSystem and Input Module
        addEventSystemAndInputModule();
    }

    //Creates Hidden GameObject and attaches Canvas component to it
    private void createCanvas(bool hide)
    {
        //Create Canvas GameObject
        hiddenCanvas = new GameObject("___HiddenCanvas");
        if (hide)
        {
            hiddenCanvas.hideFlags = HideFlags.HideAndDontSave;
        }

        //Create and Add Canvas Component
        Canvas cnvs = hiddenCanvas.AddComponent<Canvas>();
        cnvs.renderMode = RenderMode.ScreenSpaceOverlay;
        cnvs.pixelPerfect = false;

        //Set Cavas sorting order to be above other Canvas sorting order
        cnvs.sortingOrder = 12;

        cnvs.targetDisplay = 0;

        //Make it child of the GameObject this script is attached to
        hiddenCanvas.transform.SetParent(gameObject.transform);
    }

    private void addRectTransform()
    {
        RectTransform rctrfm = hiddenCanvas.AddComponent<RectTransform>();
    }

    //Adds CanvasScaler component to the Canvas GameObject 
    private void addCanvasScaler()
    {
        CanvasScaler cvsl = hiddenCanvas.AddComponent<CanvasScaler>();
        cvsl.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
        cvsl.referenceResolution = new Vector2(800f, 600f);
        cvsl.matchWidthOrHeight = 0.5f;
        cvsl.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
        cvsl.referencePixelsPerUnit = 100f;
    }

    //Adds GraphicRaycaster component to the Canvas GameObject 
    private void addGraphicsRaycaster()
    {
        GraphicRaycaster grcter = hiddenCanvas.AddComponent<GraphicRaycaster>();
        grcter.ignoreReversedGraphics = true;
        grcter.blockingObjects = GraphicRaycaster.BlockingObjects.None;
    }

    //Creates Hidden Panel and attaches Image component to it
    private GameObject createHiddenPanel(bool hide)
    {
        //Create Hidden Panel GameObject
        GameObject hiddenPanel = new GameObject("___HiddenPanel");
        if (hide)
        {
            hiddenPanel.hideFlags = HideFlags.HideAndDontSave;
        }

        //Add Image Component to the hidden panel
        Image pnlImg = hiddenPanel.AddComponent<Image>();
        pnlImg.sprite = null;
        pnlImg.color = new Color(1, 1, 1, 0); //Invisible
        pnlImg.material = null;
        pnlImg.raycastTarget = true;

        //Make it child of HiddenCanvas GameObject
        hiddenPanel.transform.SetParent(hiddenCanvas.transform);
        return hiddenPanel;
    }

    //Set Canvas width and height,to matach screen width and height then set anchor points to the corner of canvas.
    private void stretchImageAndConfigAnchor(GameObject panel)
    {
        Image pnlImg = panel.GetComponent<Image>();

        //Reset postion to middle of the screen
        pnlImg.rectTransform.anchoredPosition3D = new Vector3(0, 0, 0);

        //Stretch the Image so that the whole screen is totally covered
        pnlImg.rectTransform.anchorMin = new Vector2(0, 0);
        pnlImg.rectTransform.anchorMax = new Vector2(1, 1);
        pnlImg.rectTransform.pivot = new Vector2(0.5f, 0.5f);
    }

    //Adds EventForwarder script to the Hidden Panel GameObject 
    private void addEventForwarder(GameObject panel)
    {
        EventForwarder evnfwdr = panel.AddComponent<EventForwarder>();
    }

    //Adds EventUnBlocker script to the Hidden Panel GameObject 
    private void addEventRouter(GameObject panel)
    {
        EventUnBlocker evtrtr = panel.AddComponent<EventUnBlocker>();
        eventRouter = evtrtr;
    }

    //Add EventSystem
    private void addEventSystemAndInputModule()
    {
        //Check if EventSystem exist. If it does not create and add it
        EventSystem eventSys = FindObjectOfType<EventSystem>();
        if (eventSys == null)
        {
            GameObject evObj = new GameObject("EventSystem");
            EventSystem evs = evObj.AddComponent<EventSystem>();
            evs.firstSelectedGameObject = null;
            evs.sendNavigationEvents = true;
            evs.pixelDragThreshold = 5;
            eventSys = evs;
        }

        //Check if StandaloneInputModule exist. If it does not create and add it
        StandaloneInputModule sdlIpModl = FindObjectOfType<StandaloneInputModule>();
        if (sdlIpModl == null)
        {
            sdlIpModl = eventSys.gameObject.AddComponent<StandaloneInputModule>();
            sdlIpModl.horizontalAxis = "Horizontal";
            sdlIpModl.verticalAxis = "Vertical";
            sdlIpModl.submitButton = "Submit";
            sdlIpModl.cancelButton = "Cancel";
            sdlIpModl.inputActionsPerSecond = 10f;
            sdlIpModl.repeatDelay = 0.5f;
            sdlIpModl.forceModuleActive = false;
        }
    }

    /*
     Forwards Handler Event to every GameObject that implements  IDragHandler, IPointerDownHandler, IPointerUpHandler interface
     */

    public void forwardDragEvent(PointerEventData eventData)
    {
        //Route and send the event to UI and Colliders
        for (int i = 0; i < registeredGameobjects.Count; i++)
        {
            ExecuteEvents.Execute<IDragHandler>(registeredGameobjects[i],
                                    eventData,
                                    ExecuteEvents.dragHandler);
        }

        //Route and send the event to UI and Colliders
        eventRouter.routeDragEvent(eventData);
    }

    public void forwardPointerDownEvent(PointerEventData eventData)
    {
        //Send the event to all subscribed scripts
        for (int i = 0; i < registeredGameobjects.Count; i++)
        {
            ExecuteEvents.Execute<IPointerDownHandler>(registeredGameobjects[i],
                              eventData,
                              ExecuteEvents.pointerDownHandler);
        }

        //Route and send the event to UI and Colliders
        eventRouter.routePointerDownEvent(eventData);
    }

    public void forwardPointerUpEvent(PointerEventData eventData)
    {
        //Send the event to all subscribed scripts
        for (int i = 0; i < registeredGameobjects.Count; i++)
        {
            ExecuteEvents.Execute<IPointerUpHandler>(registeredGameobjects[i],
                    eventData,
                    ExecuteEvents.pointerUpHandler);
        }

        //Route and send the event to UI and Colliders
        eventRouter.routePointerUpEvent(eventData);
    }
}
Run Code Online (Sandbox Code Playgroud)

2.EventForwarder.cs- 它只是从隐藏中接收任何事件Image并将其传递给WholeScreenPointer.cs脚本进行处理.

public class EventForwarder : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
    WholeScreenPointer wcp = null;
    void Start()
    {
        wcp = WholeScreenPointer.Instance;
    }

    public void OnDrag(PointerEventData eventData)
    {
        wcp.forwardDragEvent(eventData);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        wcp.forwardPointerDownEvent(eventData);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        wcp.forwardPointerUpEvent(eventData);
    }
}
Run Code Online (Sandbox Code Playgroud)

3.EventUnBlocker.cs- 它Image通过向上面的任何对象发送假事件来解锁隐藏的光线.无论是UI,2D还是3D对撞机.

public class EventUnBlocker : MonoBehaviour
{
    List<GraphicRaycaster> grRayCast = new List<GraphicRaycaster>(); //UI
    List<Physics2DRaycaster> phy2dRayCast = new List<Physics2DRaycaster>(); //Collider 2D (Sprite Renderer)
    List<PhysicsRaycaster> phyRayCast = new List<PhysicsRaycaster>(); //Normal Collider(3D/Mesh Renderer)

    List<RaycastResult> resultList = new List<RaycastResult>();

    //For Detecting button click and sending fake Button Click to UI Buttons
    Dictionary<int, GameObject> pointerIdToGameObject = new Dictionary<int, GameObject>();

    // Use this for initialization
    void Start()
    {

    }

    public void sendArtificialUIEvent(Component grRayCast, PointerEventData eventData, PointerEventType evType)
    {
        //Route to all Object in the RaycastResult
        for (int i = 0; i < resultList.Count; i++)
        {
            /*Do something if it is NOT this GameObject. 
             We don't want any other detection on this GameObject
             */

            if (resultList[i].gameObject != this.gameObject)
            {
                //Check if this is UI
                if (grRayCast is GraphicRaycaster)
                {
                    //Debug.Log("UI");
                    routeEvent(resultList[i], eventData, evType, true);
                }

                //Check if this is Collider 2D/SpriteRenderer
                if (grRayCast is Physics2DRaycaster)
                {
                    //Debug.Log("Collider 2D/SpriteRenderer");
                    routeEvent(resultList[i], eventData, evType, false);
                }

                //Check if this is Collider/MeshRender
                if (grRayCast is PhysicsRaycaster)
                {
                    //Debug.Log("Collider 3D/Mesh");
                    routeEvent(resultList[i], eventData, evType, false);
                }
            }
        }
    }

    //Creates fake PointerEventData that will be used to make PointerEventData for the callback functions
    PointerEventData createEventData(RaycastResult rayResult)
    {
        PointerEventData fakeEventData = new PointerEventData(EventSystem.current);
        fakeEventData.pointerCurrentRaycast = rayResult;
        return fakeEventData;
    }

    private void routeEvent(RaycastResult rayResult, PointerEventData eventData, PointerEventType evType, bool isUI = false)
    {
        bool foundKeyAndValue = false;

        GameObject target = rayResult.gameObject;

        //Make fake GameObject target
        PointerEventData fakeEventData = createEventData(rayResult);


        switch (evType)
        {
            case PointerEventType.Drag:

                //Send/Simulate Fake OnDrag event
                ExecuteEvents.Execute<IDragHandler>(target, fakeEventData,
                          ExecuteEvents.dragHandler);
                break;

            case PointerEventType.Down:

                //Send/Simulate Fake OnPointerDown event
                ExecuteEvents.Execute<IPointerDownHandler>(target,
                         fakeEventData,
                          ExecuteEvents.pointerDownHandler);

                //Code Below is for UI. break out of case if this is not UI
                if (!isUI)
                {
                    break;
                }
                //Prepare Button Click. Should be sent in the if PointerEventType.Up statement
                Button buttonFound = target.GetComponent<Button>();

                //If pointerId is not in the dictionary add it
                if (buttonFound != null)
                {
                    if (!dictContains(eventData.pointerId))
                    {
                        dictAdd(eventData.pointerId, target);
                    }
                }

                //Bug in Unity with GraphicRaycaster  and Toggle. Have to use a hack below
                //Toggle Toggle component
                Toggle toggle = null;
                if ((target.name == "Checkmark" || target.name == "Label") && toggle == null)
                {
                    toggle = target.GetComponentInParent<Toggle>();
                }

                if (toggle != null)
                {
                    //Debug.LogWarning("Toggled!: " + target.name);
                    toggle.isOn = !toggle.isOn;
                    //Destroy(toggle.gameObject);
                }
                break;

            case PointerEventType.Up:

                //Send/Simulate Fake OnPointerUp event
                ExecuteEvents.Execute<IPointerUpHandler>(target,
                        fakeEventData,
                        ExecuteEvents.pointerUpHandler);

                //Code Below is for UI. break out of case if this is not UI
                if (!isUI)
                {
                    break;
                }

                //Send Fake Button Click if requirement is met
                Button buttonPress = target.GetComponent<Button>();

                /*If pointerId is in the dictionary, check 

                 */
                if (buttonPress != null)
                {
                    if (dictContains(eventData.pointerId))
                    {
                        //Check if GameObject matches too. If so then this is a valid Click
                        for (int i = 0; i < resultList.Count; i++)
                        {
                            GameObject tempButton = resultList[i].gameObject;
                            if (tempButton != this.gameObject && dictContains(eventData.pointerId, tempButton))
                            {
                                foundKeyAndValue = true;
                                //Debug.Log("Button ID and GameObject Match! Sending Click Event");

                                //Send/Simulate Fake Click event to the Button
                                ExecuteEvents.Execute<IPointerClickHandler>(tempButton,
                                      new PointerEventData(EventSystem.current),
                                      ExecuteEvents.pointerClickHandler);
                            }
                        }
                    }
                }
                break;
        }

        //Remove pointerId since it exist 
        if (foundKeyAndValue)
        {
            dictRemove(eventData.pointerId);
        }
    }

    void routeOption(PointerEventData eventData, PointerEventType evType)
    {
        UpdateRaycaster();
        if (WholeScreenPointer.Instance.simulateUIEvent)
        {
            //Loop Through All GraphicRaycaster(UI) and throw Raycast to each one
            for (int i = 0; i < grRayCast.Count; i++)
            {
                //Throw Raycast to all UI elements in the position(eventData)
                grRayCast[i].Raycast(eventData, resultList);
                sendArtificialUIEvent(grRayCast[i], eventData, evType);
            }
            //Reset Result
            resultList.Clear();
        }

        if (WholeScreenPointer.Instance.simulateCollider2DEvent)
        {
            //Loop Through All Collider 2D (Sprite Renderer) and throw Raycast to each one
            for (int i = 0; i < phy2dRayCast.Count; i++)
            {
                //Throw Raycast to all UI elements in the position(eventData)
                phy2dRayCast[i].Raycast(eventData, resultList);
                sendArtificialUIEvent(phy2dRayCast[i], eventData, evType);
            }
            //Reset Result
            resultList.Clear();
        }

        if (WholeScreenPointer.Instance.simulateColliderEvent)
        {
            //Loop Through All Normal Collider(3D/Mesh Renderer) and throw Raycast to each one
            for (int i = 0; i < phyRayCast.Count; i++)
            {
                //Throw Raycast to all UI elements in the position(eventData)
                phyRayCast[i].Raycast(eventData, resultList);
                sendArtificialUIEvent(phyRayCast[i], eventData, evType);
            }
            //Reset Result
            resultList.Clear();
        }
    }

    public void routeDragEvent(PointerEventData eventData)
    {
        routeOption(eventData, PointerEventType.Drag);
    }

    public void routePointerDownEvent(PointerEventData eventData)
    {
        routeOption(eventData, PointerEventType.Down);
    }

    public void routePointerUpEvent(PointerEventData eventData)
    {
        routeOption(eventData, PointerEventType.Up);
    }

    public void UpdateRaycaster()
    {
        convertToList(FindObjectsOfType<GraphicRaycaster>(), grRayCast);
        convertToList(FindObjectsOfType<Physics2DRaycaster>(), phy2dRayCast);
        convertToList(FindObjectsOfType<PhysicsRaycaster>(), phyRayCast);
    }

    //To avoid ToList() function
    void convertToList(GraphicRaycaster[] fromComponent, List<GraphicRaycaster> toComponent)
    {
        //Clear and copy new Data
        toComponent.Clear();
        for (int i = 0; i < fromComponent.Length; i++)
        {
            toComponent.Add(fromComponent[i]);
        }
    }

    //To avoid ToList() function
    void convertToList(Physics2DRaycaster[] fromComponent, List<Physics2DRaycaster> toComponent)
    {
        //Clear and copy new Data
        toComponent.Clear();
        for (int i = 0; i < fromComponent.Length; i++)
        {
            toComponent.Add(fromComponent[i]);
        }
    }

    //To avoid ToList() function
    void convertToList(PhysicsRaycaster[] fromComponent, List<PhysicsRaycaster> toComponent)
    {
        //Clear and copy new Data
        toComponent.Clear();
        for (int i = 0; i < fromComponent.Length; i++)
        {
            toComponent.Add(fromComponent[i]);
        }
    }

    //Checks if object is in the dictionary
    private bool dictContains(GameObject obj)
    {
        return pointerIdToGameObject.ContainsValue(obj);
    }

    //Checks if int is in the dictionary
    private bool dictContains(int pointerId)
    {
        return pointerIdToGameObject.ContainsKey(pointerId);
    }

    //Checks if int and object is in the dictionary
    private bool dictContains(int pointerId, GameObject obj)
    {
        return (pointerIdToGameObject.ContainsKey(pointerId) && pointerIdToGameObject.ContainsValue(obj));
    }

    //Adds pointerId and its value to dictionary
    private void dictAdd(int pointerId, GameObject obj)
    {
        pointerIdToGameObject.Add(pointerId, obj);
    }

    //Removes pointerId and its value from dictionary
    private void dictRemove(int pointerId)
    {
        pointerIdToGameObject.Remove(pointerId);
    }

    public enum PointerEventType
    {
        Drag, Down, Up
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

1.WholeScreenPointer脚本附加到空的GameObject或Camera.

2.要接受现场任何情况下,简单地实现IDragHandler,IPointerDownHandler,IPointerUpHandler在任何脚本,然后调用WholeScreenPointer.Instance.registerGameObject(this.gameObject);一次.屏幕上的任何事件现在都将发送到该脚本.不要忘记在OnDisable()函数中取消注册.

例如,附加Test到您想要接收触摸事件的任何GameObject:

public class Test : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
    void Start()
    {
        //Register this GameObject so that it will receive events from WholeScreenPointer script
        WholeScreenPointer.Instance.registerGameObject(this.gameObject);
    }

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

    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("Pointer Down: ");
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("Pointer Up: ");
    }

    void OnDisable()
    {
        WholeScreenPointer.Instance.unRegisterGameObject(this.gameObject);
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:

You only need to call WholeScreenPointer.Instance.registerGameObject(this.gameObject); if you want to receive event anywhere on the screen. If you just want to receive event from current Object, then you don't have to call this. If you do, you will receive multiple events.

Other Important functions:

Enable WholeScreen Event - WholeScreenPointer.Instance.enablewholeScreenPointer(true);

Disable WholeScreen Event - WholeScreenPointer.Instance.enablewholeScreenPointer(false); Finally, this can be improved more.


Gök*_*urt 4

我要发布的问题和答案似乎几乎都是基于意见的。尽管如此,我还是会尽力回答。

如果您尝试检测屏幕上的指针事件,那么用对象表示屏幕没有任何问题。在您的例子中,您使用 3D 对撞机来覆盖相机的整个视锥体。然而,Unity 中有一种原生方法可以做到这一点,即使用覆盖整个屏幕的 2D UI 对象。屏幕最好用 2D 对象来表示。对我来说,这似乎是一种自然的方式。

为此,我使用通用代码:

public class Screen : MonoSingleton<Screen>, IPointerClickHandler, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler, IScrollHandler {
    private bool holding = false;
    private PointerEventData lastPointerEventData;

    #region Events
    public delegate void PointerEventHandler(PointerEventData data);

    static public event PointerEventHandler OnPointerClick = delegate { };

    static public event PointerEventHandler OnPointerDown = delegate { };
    /// <summary> Dont use delta data as it will be wrong. If you are going to use delta, use OnDrag instead. </summary>
    static public event PointerEventHandler OnPointerHold = delegate { };
    static public event PointerEventHandler OnPointerUp = delegate { };

    static public event PointerEventHandler OnBeginDrag = delegate { };
    static public event PointerEventHandler OnDrag = delegate { };
    static public event PointerEventHandler OnEndDrag = delegate { };
    static public event PointerEventHandler OnScroll = delegate { };
    #endregion

    #region Interface Implementations
    void IPointerClickHandler.OnPointerClick(PointerEventData e) {
        lastPointerEventData = e;
        OnPointerClick(e);
    }

    // And other interface implementations, you get the point
    #endregion

    void Update() {
        if (holding) {
            OnPointerHold(lastPointerEventData);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Screen是一个单例,因为游戏上下文中只有一个屏幕。对象(如相机)订阅其指针事件,并相应地安排自己。这也保持了单一职责的完整性。

您可以将其用作将其附加到代表所谓玻璃(屏幕表面)的对象。如果您认为 UI 上的按钮会从屏幕中弹出,那么它们下面应该是玻璃。为此,玻璃必须是 的第一个子级Canvas。当然,Canvas必须在屏幕空间中渲染才能使其有意义。

这里的一个技巧是没有意义的,那就是Image向玻璃添加一个不可见的组件,这样它就会接收事件。这就像玻璃的光线投射目标。

等级制度 督察

您还可以使用Input(Input.touches等) 来实现这个玻璃对象。它将检查每次Update调用中的输入是否发生变化。对我来说,这似乎是一种基于轮询的方法,而上面的方法是一种基于事件的方法。

您的问题似乎是在寻找一种方法来证明使用该类的合理Input性。恕我直言,不要让自己变得更困难。使用有效的方法。并接受 Unity 并不完美的事实。