在unity中找到一个基于变换的游戏对象
本文关键字:于变换 变换 对象 游戏 一个 unity | 更新日期: 2023-09-27 18:02:24
我有一个transform
,想找到有transform
的gameObject
。
例如,我如何找到
的gameObject
Vector3 myTransform = new Vector3(33,12,0);
获取一个场景中所有的变换,然后循环检查它们的transform.position
是否等于你想要的:
Vector3 myVector = new Vector3(33,12,0);
Transform myTransform;
void start()
{
Transform[] allTransforms = GameObject.FindObjectsOfType(typeof(Transform)) as Transform[];
foreach (transform someTransform in allTransforms)
{
if(someTransform.position != myVector)
continue;
myTransform = someTransform;
break;
}
}
您可以创建一个自定义类来实现
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
List<Vector3> vector3 = new List<Vector3>();
Vector3 myTransform = new Vector3(33, 12, 0);
vector3.Add(myTransform);
Vector3 results = vector3.Find(x => x == myTransform);
}
}
public class Vector3 : IEqualityComparer<Vector3>
{
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
public Vector3(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Boolean Equals(Vector3 a, Vector3 b)
{
return a == b;
}
public int GetHashCode(Vector3 vector3)
{
return (vector3.x.ToString() + "^" + vector3.y.ToString() + "^" + vector3.z.ToString()).GetHashCode();
}
}
}
我平等