消息关闭
    暂无新消息!
我打算用Unity3d做一个可视化冒泡排序.  C# 写冒泡很简单,可是用在Unity3d就不是那么一回事啊。。。求大家帮我看看代码。
不知道哪里出问题了  代码如下

 void Update()
    {
            for (i = 0; i < shapes.Length-1;i++)
            {

                for (j = i+1; j < shapes.Length; j++)
                {
                    x0 = shapes[i].transform.position.x;  //提取2个cube位置信息
                    y0 = shapes[i].transform.position.y;
                    z0 = shapes[i].transform.position.z;
                    x1 = shapes[j].transform.position.x;
                    y1 = shapes[j].transform.position.y;
                    z1 = shapes[j].transform.position.z;

                isize = shapes[i].transform.localScale.y;  //提取2个cube长度,用来对比大小
                jsize = shapes[j].transform.localScale.y;

                if (isize > jsize)
                    {
                       shapes[j].transform.position = new Vector3(Mathf.Lerp(shapes[j].transform.position.x, shapes[i].transform.position.x, speed * Time.deltaTime), shapes[1].transform.position.y, shapes[1].transform.position.z);
                       shapes[i].transform.position = new Vector3(Mathf.Lerp(shapes[i].transform.position.x, shapes[j].transform.position.x, speed * Time.deltaTime), shapes[0].transform.position.y, shapes[0].transform.position.z);
                        //交换位置  
                    }
                }
            }

        }


4个回答

︿ 2
没。。我要通宵编程了

这个应该是update每秒几百次调用,而for循环也是每秒循环几百次,但是for里面苦逼的平移操作却来不及完成吧?
好像涉及到协程什么的,我真的一点办法都没有。。深夜我还会来看看的~~~
︿ 1

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;
        }
    }