我如何获得列表中的 GameObject 索引?
How i can get GameObject index in the list?
开始时,对象将 link 添加到列表中。然后当我点击这个对象时,我需要获取这个对象的索引引用。怎么做?
我需要的小例子:
public static List<GameObject> myObjects = new List<GameObject> ();
public GameObject ObjectA; //this is prefab
void Start(){
for (int i = 0; i < 10; i++) {
GameObject ObjectACopy = Instantiate (ObjectA);
myObjects.Add (ObjectACopy);
}
}
ObjectA 脚本:
void OnMouseDown(){
Debug.Log(//Here i need to return index of this clicked object from the list);
}
遍历 Objects
列表。检查它是否与单击的游戏对象匹配。被点击的GameObject可以在OnMouseDown
函数中用gameObject
属性获取。如果它们匹配,return 来自该循环的当前索引。如果它们不匹配,return -1 作为错误代码。
void OnMouseDown()
{
int index = GameObjectToIndex(gameObject);
Debug.Log(index);
}
int GameObjectToIndex(GameObject targetObj)
{
//Loop through GameObjects
for (int i = 0; i < Objects.Count; i++)
{
//Check if GameObject is in the List
if (Objects[i] == targetObj)
{
//It is. Return the current index
return i;
}
}
//It is not in the List. Return -1
return -1;
}
这应该可行,但最好停止使用 OnMouseDown
函数,改用 OnPointerClick
函数。
public class Test : MonoBehaviour, IPointerClickHandler
{
public static List<GameObject> Objects = new List<GameObject>();
public void OnPointerClick(PointerEventData eventData)
{
GameObject clickedObj = eventData.pointerCurrentRaycast.gameObject;
int index = GameObjectToIndex(clickedObj);
Debug.Log(index);
}
int GameObjectToIndex(GameObject targetObj)
{
//Loop through GameObjects
for (int i = 0; i < Objects.Count; i++)
{
//Check if GameObject is in the List
if (Objects[i] == targetObj)
{
//It is. Return the current index
return i;
}
}
//It is not in the List. Return -1
return -1;
}
}
有关 OnPointerClick
的更多信息,请参阅 post。
编辑:
Objects.IndexOf
也可以用于此。这个答案是为了让你明白如何自己做,以便以后解决类似的问题。
开始时,对象将 link 添加到列表中。然后当我点击这个对象时,我需要获取这个对象的索引引用。怎么做? 我需要的小例子:
public static List<GameObject> myObjects = new List<GameObject> ();
public GameObject ObjectA; //this is prefab
void Start(){
for (int i = 0; i < 10; i++) {
GameObject ObjectACopy = Instantiate (ObjectA);
myObjects.Add (ObjectACopy);
}
}
ObjectA 脚本:
void OnMouseDown(){
Debug.Log(//Here i need to return index of this clicked object from the list);
}
遍历 Objects
列表。检查它是否与单击的游戏对象匹配。被点击的GameObject可以在OnMouseDown
函数中用gameObject
属性获取。如果它们匹配,return 来自该循环的当前索引。如果它们不匹配,return -1 作为错误代码。
void OnMouseDown()
{
int index = GameObjectToIndex(gameObject);
Debug.Log(index);
}
int GameObjectToIndex(GameObject targetObj)
{
//Loop through GameObjects
for (int i = 0; i < Objects.Count; i++)
{
//Check if GameObject is in the List
if (Objects[i] == targetObj)
{
//It is. Return the current index
return i;
}
}
//It is not in the List. Return -1
return -1;
}
这应该可行,但最好停止使用 OnMouseDown
函数,改用 OnPointerClick
函数。
public class Test : MonoBehaviour, IPointerClickHandler
{
public static List<GameObject> Objects = new List<GameObject>();
public void OnPointerClick(PointerEventData eventData)
{
GameObject clickedObj = eventData.pointerCurrentRaycast.gameObject;
int index = GameObjectToIndex(clickedObj);
Debug.Log(index);
}
int GameObjectToIndex(GameObject targetObj)
{
//Loop through GameObjects
for (int i = 0; i < Objects.Count; i++)
{
//Check if GameObject is in the List
if (Objects[i] == targetObj)
{
//It is. Return the current index
return i;
}
}
//It is not in the List. Return -1
return -1;
}
}
有关 OnPointerClick
的更多信息,请参阅
编辑:
Objects.IndexOf
也可以用于此。这个答案是为了让你明白如何自己做,以便以后解决类似的问题。