【问题标题】:Glitch in the set active and Input.touch in UnityUnity 中 set active 和 Input.touch 中的故障
【发布时间】:2019-12-03 09:36:52
【问题描述】:

我有一个应用程序可以在用户点击时生成模型,当用户点击模型时,会弹出一个菜单。

当用户点击除画布元素之外的任何其他位置时,菜单将关闭。

但这不起作用,当我按下画布元素时,它会检测到画布元素,但菜单仍然关闭。

这是我的代码:

void Update()
    {
        UpdatePlacementPose();
        UpdatePlacementIndicator();
        if (Input.touchCount > 0 )//&& Input.GetTouch(0).phase == TouchPhase.Began)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            RaycastHit hit;
            debugText.text = "touched";

            // Check if finger is over a UI element
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                Debug.Log("Touched the UI");
                debugText1.text = "Touched the UI";
                isui = true;
            }
            else
            {
                isui = false;
                if (Physics.Raycast(ray, out hit) && (hit.transform.name != "Quad"))
                {
                    //debugText.text = hit.transform.name;
                    debugText.text = Input.touchCount.ToString();
                    if (IsSpawned)
                    {
                        debugText1.text = ColourImage.activeSelf.ToString();
                        ColourImage.SetActive(true);
                        //debugText.text = objectToPlace.name;
                    }
                }
                else
                {
                    if (placementPoseIsValid && Input.touchCount > 0 && !IsSpawned)
                    {
                        PlaceObject();
                        IsSpawned = true;
                        PlayerPrefs.SetString("type", "medium");
                    }
                    else
                    {
                      ColourImage.SetActive(false);
                    }

                }
            }


        }

        //if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        //{
        //    PlaceObject();
        //}
    }

【问题讨论】:

    标签: c# unity3d augmented-reality


    【解决方案1】:

    最好的解决方案是在你的 UI 菜单后面创建“拦截器”对象。

    private GameObject CreateBlocker()
    {
        GameObject gameObject = new GameObject("Blocker");
        RectTransform rectTransform = gameObject.AddComponent<RectTransform>();
        rectTransform.SetParent(transformObjectBehindMenu, 
        false);
        rectTransform.anchorMin = (Vector2)Vector3.zero;
        rectTransform.anchorMax = (Vector2)Vector3.one;
        rectTransform.sizeDelta = Vector2.zero;
        Canvas canvas = gameObject.AddComponent<Canvas>();
        canvas.overrideSorting = true;
        canvas.sortingOrder += 1;
        gameObject.AddComponent<GraphicRaycaster>();
        gameObject.AddComponent<Image>().color = Color.clear;
        gameObject.AddComponent<Button>().onClick.AddListener(delegate { 
        CloseMenu(); });
        return gameObject;
    }
    

    在您的模型点击时调用此方法,您可以在菜单中导航并在您点击其他任何地方时关闭

    【讨论】:

      【解决方案2】:

      您可以使用GraphicRaycaster 来测试点击事件是否针对 UI 元素。

      这是一个简单的Demo来给出想法

      public class Demo : MonoBehaviour
      {
      
      #pragma warning disable 0649
          [SerializeField] private EventSystem _eventSystem;
          [SerializeField] private GraphicRaycaster _graphicRaycaster;
      #pragma warning restore 0649
      
          private void Update()
          {
              if (Input.GetMouseButtonDown(0))
              {
                  if (TestClickEvent(Input.mousePosition))
                  {
                      Debug.Log("Clicked UI Element");
                  }
              }
          }
      
          private bool TestClickEvent(Vector3 mousePos)
          {
              PointerEventData data = new PointerEventData(_eventSystem);
              data.position = mousePos;
      
              List<RaycastResult> results = new List<RaycastResult>();
      
              _graphicRaycaster.Raycast(data, results);
              if(results.Count > 0)
              {
                  //You can loop through the results to test for a specific UI element
                  return true;
              }       
              return false;
          }
      }
      

      更多信息见官方documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        相关资源
        最近更新 更多