如何解决Unity 多点触控无法达到其方法
我遵循了 this 教程,基本上我所做的一切都是一样的。但是我无法访问 testTouch 类中的方法。这是 TouchInput 类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class touchInput : MonoBehavIoUr
{
public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
#if UNITY_EDITOR
//统一编辑器的相同内容如教程中所示 #endif
if (Input.touchCount > 0)
{
touchesOld = new GameObject[touchList.Count];
touchList.copyTo(touchesOld);
touchList.Clear();
foreach (Touch touch in Input.touches)
{
Ray ray = GetComponent<Camera>().ScreenPointToRay(touch.position);
if (Physics.Raycast(ray,out hit,touchInputMask))
{
GameObject recepient = hit.transform.gameObject;
touchList.Add(recepient);
if (touch.phase == TouchPhase.Began)
{
recepient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Ended)
{
recepient.SendMessage("OnTouchUp",SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Stationary)
{
recepient.SendMessage("OnTouchStay",SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Canceled)
{
recepient.SendMessage("OnTouchExit",SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld)
{
if (!touchList.Contains(g))
g.SendMessage("OnTouchExit",SendMessageOptions.DontRequireReceiver);
}
}
}
}
这里是 testTouch 类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testTouch : MonoBehavIoUr
{
public Color defaultColor; //set manually in the editor
public Color selectedColor; //set manually in the editor
public Material mat;
void Start()
{
mat = this.GetComponent<SpriteRenderer>().material;
// mat.color = selectedColor;
}
void OnTouchDown()
{
mat.color = selectedColor;
Debug.Log("hello");
}
void OnTouchUp()
{
mat.color = defaultColor;
Debug.Log("hello");
}
void OnTouchStay()
{
mat.color = selectedColor;
Debug.Log("hello");
}
void OnTouchExit()
{
mat.color = defaultColor;
Debug.Log("hello");
}
我没有收到任何错误,也无法访问那些 Debug.Logs。当我点击 2Dcircle 时没有任何反应。
我尝试了编辑器和我的安卓手机。它们都不起作用。
解决方法
2D 对撞机上的 3D 光线投射 (Physics.Raycast()
) 不起作用。
用 3D 圆形碰撞器替换 2D 圆形碰撞器。
没有将 Raycast
作为第三个参数的 layerMask
方法的重载。
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
您错误地调用了 Raycast
方法的另一个重载。
public static bool Raycast(Ray ray,out RaycastHit hitInfo,float maxDistance = Mathf.Infinity,int layerMask = DefaultRaycastLayers,QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。