UNITY - Select 带有 Raycast 的对象 - 适用于 Windows,WebGL(触摸屏)错误
UNITY - Select object with Raycast - Works on Windows, errors onWebGL (TouchScreen)
我想 select 场景中的一个对象在屏幕上触摸它。我制作了这段代码,它在 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;
}
}
这不是你在 2018 年(年份,不是 Unity 版本)的做法。在你的相机上放一个 PhysicsRaycaster,添加一个 EventSystem,然后实现 IPointer***Handler(Down、Up、Enter、Exit、Click,随便你怎么说)。
如果有人想知道我如何 "solved" 问题是我的代码。
是一个简单的解决方法,它使用布尔值来控制我是否在同一事件中继续按下监视器。
我再说一遍,这只是使用 WebGL
解决此 "incompatibility" 的一种解决方法
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;
}
}
我想 select 场景中的一个对象在屏幕上触摸它。我制作了这段代码,它在 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;
}
}
这不是你在 2018 年(年份,不是 Unity 版本)的做法。在你的相机上放一个 PhysicsRaycaster,添加一个 EventSystem,然后实现 IPointer***Handler(Down、Up、Enter、Exit、Click,随便你怎么说)。
如果有人想知道我如何 "solved" 问题是我的代码。 是一个简单的解决方法,它使用布尔值来控制我是否在同一事件中继续按下监视器。 我再说一遍,这只是使用 WebGL
解决此 "incompatibility" 的一种解决方法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;
}
}