zoukankan      html  css  js  c++  java
  • 解决UI与3d物体的事件响应问题

    解决UI与3d物体的事件响应问题

    1.3D物体也可以使用EventSystems里的接口,需要相机上添加PhysicsRaycaster组件。

    2.UI响应事件向下传递:

    //向下传递UI响应事件
        private void PassdownEvent(PointerEventData eventData, ExecuteEvents.EventFunction<IPointerClickHandler> function)
        {
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(eventData, results); //获得本次点击的所有物体
            for (int i = 0; i < results.Count; i++)
            {
                //Debug.Log(results[i].gameObject.name);
                if (results[i].gameObject != gameObject) //移除自己
                {
                    ExecuteEvents.Execute(results[i].gameObject, eventData, function);
                }
            }
        }

    3.当鼠标点击在UI上时,不响应鼠标点击事件

    private GraphicRaycaster uiRaycaster;
    private
    void Update() { //当鼠标点在UI上时,不响应鼠标点击事件 if (Input.GetMouseButtonDown(0) && !IsPointClickUI()) { //......... } }//判断是否点击到ui了,返回true是点击到ui了,反之没有 public bool IsPointClickUI() { PointerEventData eventData = new PointerEventData(EventSystem.current); eventData.position = Input.mousePosition; eventData.pressPosition = Input.mousePosition; List<RaycastResult> resultAppendList = new List<RaycastResult>(); uiRaycaster.Raycast(eventData, resultAppendList); return resultAppendList.Count > 0; }
  • 相关阅读:
    __dict__和dir()的区别:未完
    [leetcode] Subsets II
    [leetcode] Decode Ways
    [leetcode] Gray Code
    [leetcode] Merge Sorted Array
    [leetcode] Partition List
    [leetcode] Scramble String
    [leetcode] Maximal Rectangle
    [leetcode] Remove Duplicates from Sorted List II
    [leetcode] Remove Duplicates from Sorted List
  • 原文地址:https://www.cnblogs.com/luoyanghao/p/15127273.html
Copyright © 2011-2022 走看看