qq_52019234 2022-09-23 00:51 采纳率: 100%
浏览 66
已结题

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

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


private IEnumerator F(int begin, int end)
    {
        if (begin > end)
            yield break;
        float tmp = Obj[begin].GetComponent<Transform>().lossyScale.y;
        int i = begin;
        int j = end;
        while (i != j)
        {
            while (Obj[j].GetComponent<Transform>().lossyScale.y >= tmp && j > i)
                j--;
            while (Obj[i].GetComponent<Transform>().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<MeshRenderer>().material;
                Material mal1 = Obj[j + 1].GetComponent<MeshRenderer>().material;
                Debug.Log(Obj[j]);
                Debug.Log(Obj[j + 1]);
                Vector3 a = Obj[j + 1].GetComponent<Transform>().position;
                Vector3 b = Obj[j].GetComponent<Transform>().position;
                Vector3 c = Obj1[j + 1].GetComponent<Transform>().position;
                Vector3 d = Obj1[j].GetComponent<Transform>().position;
                var tweener = Obj[j].GetComponent<Transform>().DOMoveX(a.x, 2).SetId("1");
                var tweener1 = Obj[j + 1].GetComponent<Transform>().DOMoveX(b.x, 2).SetId("1");
                var tweener2 = Obj1[j].GetComponent<Transform>().DOMoveX(c.x, 2).SetId("1");
                var tweener3 = Obj1[j + 1].GetComponent<Transform>().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<Transform>().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

  • 写回答

4条回答 默认 最新

  • 陈言必行 Unity领域优质创作者 2022-09-23 11:25
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月3日
  • 已采纳回答 9月25日
  • 创建了问题 9月23日

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包