Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

c# - 在Unity中的激活状态和Input.touch中设置故障(Glitch in the set active and Input.touch in Unity)

I have an app that spawns a model on user click, and when the user taps the model, a menu pops up.

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

When the user taps anywhere else except the canvas element, the menu closes.

(当用户轻按画布元素以外的其他任何位置时,菜单都会关闭。)

But this is not working, when I press the canvas element, it detects the canvas element, but still, the menu closes.

(但这是行不通的,当我按下canvas元素时,它检测到canvas元素,但菜单仍然关闭。)

Here is my code:

(这是我的代码:)

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();
        //}
    }
  ask by Jerin Cherian translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The best solution is to create "blocker" object which is behind your UI menu.

(最好的解决方案是在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;
}

Call this method on your model click and you can navigate in menu and close when you click anywhere else

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...