关于#unity#的问题,如何解决?

大lao们,我想在unity实现一个快速排序算法的可视化,这是我脚本中快排算法的部分,并且利用了协程给动画时间,但是在前端执行的时候就不会动了,这个算法哪个地方出现了问题啊,下面是算法的代码


private IEnumerator F(int begin, int end)
    {
        if (begin > end)
            yield break;
        float tmp = Obj[begin].GetComponent().lossyScale.y;
        int i = begin;
        int j = end;
        while (i != j)
        {
            while (Obj[j].GetComponent().lossyScale.y >= tmp && j > i)
                j--;
            while (Obj[i].GetComponent().lossyScale.y <= tmp && j > i)
                i++;
            Debug.Log(Obj[0]);
            if (i < j)
            {
                GameObject x, y;
                Sequence s = DOTween.Sequence();
                Material mal = Obj[j].GetComponent().material;
                Material mal1 = Obj[j + 1].GetComponent().material;
                Debug.Log(Obj[j]);
                Debug.Log(Obj[j + 1]);
                Vector3 a = Obj[j + 1].GetComponent().position;
                Vector3 b = Obj[j].GetComponent().position;
                Vector3 c = Obj1[j + 1].GetComponent().position;
                Vector3 d = Obj1[j].GetComponent().position;
                var tweener = Obj[j].GetComponent().DOMoveX(a.x, 2).SetId("1");
                var tweener1 = Obj[j + 1].GetComponent().DOMoveX(b.x, 2).SetId("1");
                var tweener2 = Obj1[j].GetComponent().DOMoveX(c.x, 2).SetId("1");
                var tweener3 = Obj1[j + 1].GetComponent().DOMoveX(d.x, 2).SetId("1");
                mal.DOBlendableColor(Color.yellow, 2).OnComplete(() => { mal.DOColor(Color.white, 0).OnComplete(() => { mal.DOKill(); }); }).SetId("1");
                mal1.DOBlendableColor(Color.yellow, 2).OnComplete(() => { mal1.DOColor(Color.white, 0).OnComplete(() => { mal1.DOKill(); }); }).SetId("1");
                x = Obj[j];
                Obj[j] = Obj[j + 1];
                Obj[j + 1] = x;
                y = Obj1[j];
                Obj1[j] = Obj1[j + 1];
                Obj1[j + 1] = y;
                Debug.Log(Obj[j]);
                Debug.Log(Obj[j + 1]);
                yield return new WaitForSeconds(3);
            }
        }
        Obj[begin] = Obj[i];
        //arr[i] = tmp;
        Obj[i].GetComponent().localScale = new Vector3(1, tmp, 1);
        IEnumerator coroutine1 = F(begin, i - 1);
        IEnumerator coroutine2 = F(i + 1, end);
        StartCoroutine(coroutine1);
        StartCoroutine(coroutine2); 
    }
    public void Pause()
    {
        //play = 0;
        Time.timeScale = 0;
        StopAllCoroutines();
    }
    public void Play()
    {
        //play = 1;
        Time.timeScale = 1;
        IEnumerator coroutine = F(0, count-1);
        StartCoroutine(coroutine);
    }

img

你的那个循环里面的日志正常输出了吗? 循环里面写协程应该也没什么问题呀。
建议先把后面的那两个递归开启协程注释掉,调试一下第一次执行,看看逻辑是否正确

你想加动画,那不能这样循环,这样循环太快了,一瞬间就执行完了,眼睛根本看不到过程
你需要在update里写代码,每次执行循环的其中一步,而且有必要的话还要加个计数来判断,比如加个count,每次update时count++,count>5时才执行同时清零

我的建议是,先排好序记录好每个物体的交换数据,再开始执行动画