【发布时间】: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