我不能通过移动手指来触摸统一中的某些物体。
我使用EventSystem.current.currentSelectedGameObject.name
来检查选择了哪个对象。但是当我移动手指时,我只能访问第一个对象。在完成第一个物体的触摸后,第二个物体将无法理解我的触摸。我应该移开手指并再次触摸第二个物体。
答案 0 :(得分:0)
在这里,您经常测试Touch命中。 我建议你另一种方法。 相反,您可以让对象在选择它们时告诉您。 - 触发事件的每个对象上的OnMouse事件处理程序 - 一个侦听此事件的类,并将事件发送的游戏对象存储在集合中
例如:
对象的脚本:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class HoverDetection : MonoBehaviour
{
public delegate void SelectEvent(GameObject go);
public SelectEvent IsSelected;
public delegate void DestroyEvent(GameObject go);
public DestroyEvent Destroying;
void OnMouseEnter ()
{
var es = EventSystem.current;
if(es.currentSelectedGameObject != gameObject)
{
es.SetSelectedGameObject(gameObject);
OnSelect();
}
}
void OnSelect()
{
Debug.Log("OnHover");
IsSelected(gameObject);
}
public void OnDestroy()
{
Destroying(gameObject);
}
}
处理对象并将您的选择存储到集合中的脚本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class SelectedCollection : MonoBehaviour
{
public GameObject[] AllMyObjects; //The array of all your selectable objects. Up to you to populate it.
public List<GameObject> Collection{
get {return _collection;}
}
private List<GameObject> _collection = new List<GameObject>();
void Start()
{
AllMyObjects = GameObject.FindGameObjectsWithTag("SelectableObject"); // Just an exemple to populate your base array
foreach(GameObject go in AllMyObjects)
{
go.GetComponent<HoverDetection>().IsSelected += store; //Fired by SetSelectedGameObject(GameObject) see : http://docs.unity3d.com/500/Documentation/ScriptReference/EventSystems.EventSystem.SetSelectedGameObject.html
go.GetComponent<HoverDetection>().Destroying += clean; //Always unlink your delegats when an object is destroyed. Otherwise you risk some memory leaks.
}
}
private void store(GameObject go)
{
_collection.Add(go);
Debug.Log(_collection.Count);
}
private void clean(GameObject go)
{
go.GetComponent<HoverDetection>().IsSelected -= store;
go.GetComponent<HoverDetection>().IsSelected -= clean;
}
}
您需要做的就是根据Touch事件用适当的代码替换OnMouseEnter。