2,543
社区成员
发帖
与我相关
我的任务
分享
GameObject[] shapes;
float[] shapes_x;
// Use this for initialization
void Start()
{
shapes = new GameObject[6];
shapes_x = new float[6];
shapes[0] = GameObject.Find("Cube");
shapes[1] = GameObject.Find("Cube1");
shapes[2] = GameObject.Find("Cube2");
shapes[3] = GameObject.Find("Cube3");
shapes[4] = GameObject.Find("Cube4");
shapes[5] = GameObject.Find("Cube5");
}
bool flag = false;
// Update is called once per frame
void Update()
{
if (flag == false) //表示已经排序过来,不然会导致一直排序,会闪烁
{
for (int j = 0; j < shapes.Length; j++)
{
for (int i = shapes.Length - 1; i > j; i--)
{
float x0 = shapes[i].transform.position.x; //提取2个cube位置信息
float y0 = shapes[i].transform.position.y;
float z0 = shapes[i].transform.position.z;
float x1 = shapes[j].transform.position.x;
float y1 = shapes[j].transform.position.y;
float z1 = shapes[j].transform.position.z;
float isize = shapes[i].transform.localScale.y; //提取2个cube长度,用来对比大小
float jsize = shapes[j].transform.localScale.y;
if (isize > jsize)
{
GameObject tem;
tem = shapes[i];
shapes[i] = shapes[j];
shapes[j] = tem;
}
}
}
for (int k = 0; k < shapes.Length; k++)
{
shapes_x[k] = shapes[k].transform.position.x;
}
System.Array.Sort<float>(shapes_x);
for (int j = 0; j < shapes.Length; j++)
{
for (int i = 0; i < shapes.Length; i++)
{
if (j == i)
{
shapes[j].transform.position = new Vector3(Mathf.Lerp(shapes[j].transform.position.x, shapes_x[i], 50f * Time.deltaTime), shapes[j].transform.position.y, shapes[j].transform.position.z);
}
}
}
flag = true;
}
}