更改为随机背景会导致android出现滞后

时间:2019-06-07 12:01:06

标签: c# unity3d

我正在尝试更改游戏的背景并且可以正常工作,但也会导致android系统出现延迟。

我捆绑了两个不同的解决方案,但结果相同。首先,背景变化是由一个int变量管理的,该变量在两个数字之间具有一个随机值,并且一个浮点数每秒增加10f用作得分。

这里还有一些其他代码,但是我已经分别删除了它们,似乎是导致延迟的背景代码。

这是当前代码:

        rand1 = Random.Range(0, 4);
        rand2 = Random.Range(4, 8);
        rand3 = Random.Range(8, 12);


        //start off with a random blue background
        BGlist[rand1].SetActive(true);
        // BGmesh.material = BGmat[rand];  //OTHER SOLUTION - change material of mesh renderer

    }


    void Update()
    {
        //Set starttext to false if the character move
        if (lvlmang.left || lvlmang.right)
        {
            startText.SetActive(false);

            //every second score and speed increases
            aliveTime += Time.deltaTime;
            if (aliveTime >= 1)
            {
                scores += 10f;
                scores2 += 10f;
                speedManager += 10f;

                speedManager2 += 10f;

                speedManager3 += 10f;




                scoreText.text = scores.ToString();
                scoreText2.text = scores2.ToString();
                aliveTime -= (int)aliveTime; //reset
            }

        }
        //Increase speed after 8 seconds
        if (speedManager == 80f)
        {
            pControl.speed += 0.25f;
            speedManager = 0f;
        }

        //start screen fade every 30 seconds (preparing for background change)
        if (speedManager2 == 300f)
        {
           fadingBG.StartFading();
            speedManager2 = 0f;
        }

        if (speedManager3 == 310f || speedManager3 == 1213)
        {
            //Random yellow background


            //Set the previous backgrounds game object to false
                BGlist[0].SetActive(false);
                BGlist[1].SetActive(false);
                BGlist[2].SetActive(false);
                BGlist[3].SetActive(false);



            BGlist[rand2].SetActive(true);  //Random yellow background
            //BGmesh.material = BGmat[rand]; // OTHER SOLUTION - change material of mesh renderer
            fadingBG.StopFading();
            speedManager3 += 1f;

        }


        if (speedManager3 == 611f || speedManager3 == 1514)
        {
            //Random red background


            //Set the previous backgrounds game object to false
            BGlist[4].SetActive(false);
            BGlist[5].SetActive(false);
            BGlist[6].SetActive(false);
            BGlist[7].SetActive(false);


            BGlist[rand3].SetActive(true); //Random red background
            // BGmesh.material = BGmat[rand];
            fadingBG.StopFading();
            speedManager3 += 1f;

        }


        if (speedManager3 == 912f || speedManager3 == 1815)
        {
            //Random blue background

            //Set the previous backgrounds game object to false
            BGlist[8].SetActive(false);
            BGlist[9].SetActive(false);
            BGlist[10].SetActive(false);
            BGlist[11].SetActive(false);

            BGlist[rand1].SetActive(true); //Random blue background
            // BGmesh.material = BGmat[rand];
            fadingBG.StopFading();
            speedManager3 += 1f;

        }
    }

因此,我尝试更改了网格渲染器的材质,并为我设置为活动和错误的每个背景制作了单独的游戏对象。

我正在使用网格渲染器使跟随相机的背景不断滚动。

1 个答案:

答案 0 :(得分:1)

主要问题是您在Update中调用Random并破坏了性能(我知道,因为几年前我犯了同样的错误)。 当您知道要生成多个随机值时,应仅执行1次(例如在“开始”中),将它们存储在数组中,以后再使用。如果数组为空,则可以再次调用该方法以生成1次多个随机值。 请记住,永远不要在Update中调用Random,避免在每一帧进行任何困难的计算(这不仅包括Random,还可能进行除法以避免这种情况)。