【问题标题】:UNITY - Select object with Raycast - Works on Windows, errors onWebGL (TouchScreen)UNITY - 使用 Raycast 选择对象 - 适用于 Windows,WebGL 错误(触摸屏)
【发布时间】:2018-11-28 10:46:10
【问题描述】:

我想选择场景中的一个对象,在屏幕上触摸它。我已经编写了这段代码,它在 Unity 播放器上或在我为 Windows 编译应用程序时完美运行。当我为 WebGL 编译时,我有奇怪的行为(在 Firefox/Chrome 上测试)

我得到的错误是,如果我的手指一直按在对象上,即使我使用的是 TouchPhase.Began,我也会获得多次连续点击而不是一次点击。有人知道如何解决这个问题吗?是已知问题吗?

这是我的代码

using UnityEngine;
using System.Collections.Generic;
using System.Xml.XPath;
using UnityEngine.UI;


public class RaycastObjHit : MonoBehaviour
{


private GameObject working_object;
private GameObject touchedObject;


void Update()
{
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        Ray ray = Camera.current.ScreenPointToRay(Input.GetTouch(0).position);
        RaycastHit hit;   

        if (Physics.Raycast(ray, out hit))
        {
            string ObjHitName = hit.transform.name;
            Debug.Log(hit.transform.name);
            if (hit.collider != null)
            {
                touchedObject = hit.transform.gameObject;

                if (touchedObject.GetComponent<Renderer>().material.color != Color.red )
                {
                    changeColor(touchedObject.transform.name, Color.red);   
                }else{
                    changeColor(touchedObject.transform.name, Color.green);   
                }

            }
                Debug.Log("Touched " + touchedObject.transform.name);
         }
     }

}
public void changeColor(string objId, Color color)
{
    working_object = GameObject.Find(objId);
    working_object.GetComponent<Renderer>().material.color = color;
}

}

【问题讨论】:

    标签: unity3d touch raycasting unity-webgl


    【解决方案1】:

    这不是您在 2018 年(这一年,不是 Unity 版本)执行此操作的方式。在您的相机上放置一个 PhysicsRaycaster,添加一个 EventSystem,然后实现 IPointer***Handler(向下、向上、输入、退出、单击,您可以命名它)。

    【讨论】:

    • 你怎么能这么说?你能指出一些关于这种实现触摸应用程序的“新方法”的文章/教程吗?我的意思是,我几乎是 Unity 的新手,但我没有发现任何关于创建使用触摸屏的应用程序的最佳实践的具体内容。只有 TouchPhase,我拥有在触摸屏上应用程序所需的一切。我认为我的问题与 WebGL 和 TouchScreens 密切相关
    • TouchPhase 有点太低级了,只在触摸平台上有意义。此外,您可能会重新发明轮子。这是 IPointerClickHandler 的文档,别忘了在你的相机上放一个物理光线投射器:docs.unity3d.com/ScriptReference/…
    【解决方案2】:

    如果有人想知道我是如何“解决”问题的,这里是我的代码。 是一个简单的解决方法,它使用布尔值来控制我是否在同一事件期间一直按下监视器。 我再说一遍,这只是解决这种与 WebGL 的“不兼容”的解决方法

    using UnityEngine;
    using System.Collections.Generic;
    using System.Xml.XPath;
    using UnityEngine.UI;
    
    
    public class RaycastObjHit : MonoBehaviour
    {
    
    private GameObject working_object;
    private GameObject touchedObject;
    private bool touchBegan = false;
    
    void Update()
    {
        if (Input.touchCount == 1)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Began && !touchBegan)
            {
                touchBegan = true;
                Ray ray = Camera.current.ScreenPointToRay(Input.GetTouch(0).position);
                RaycastHit hit;
    
                if (Physics.Raycast(ray, out hit))
                {
                    string ObjHitName = hit.transform.name;
                    Debug.Log(hit.transform.name);
                    if (hit.collider != null)
                    {
                        touchedObject = hit.transform.gameObject;
    
                        if (touchedObject.GetComponent<Renderer>().material.color != Color.red)
                        {
                            changeColor(touchedObject.transform.name, Color.red);
                        }
                        else
                        {
                            changeColor(touchedObject.transform.name, Color.green);
                        }
    
                    }
    
                    Debug.Log("Touched " + touchedObject.transform.name);
                }
            } else if (Input.GetTouch(0).phase == TouchPhase.Ended && touchBegan){
                touchBegan = false;
            }
        }
    
    }
    public void changeColor(string objId, Color color)
    {
        working_object = GameObject.Find(objId);
        working_object.GetComponent<Renderer>().material.color = color;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多