首页 > 解决方案 > Fade out parallax layer in unity

问题描述

I made a fade out effect on a parallax layer and I've done this:

if(currentBackgroundPhase == BackgroundPhase.Night)
{
    foreach(SpriteRenderer sprite in GetComponentsInChildren<SpriteRenderer>())
    {
        if (sprite.name.Contains("Cloud"))
        {
            sprite.color = new Color(opaqueCloud.r, opaqueCloud.g, opaqueCloud.b, transitionTimeElapsed / TRANSITION_TIME);
        }
    }
}

The parallax keeps repositioning cloud sprites and this is the only way I can think to do this. I've looked profiler and didn't see a drop on performance when the if is called. Is this too expensive / unefficient because the GetComponensInChildren? If so, is there other way to do this?

I looked on profiler's scripts graphic to see if this is too much but didn't notice anything strange.

I can't test on a bad device because I don't have one, and I want this to work on every android device...

The maximum amount of spriterenderers that can be in children is 20 or so.

标签: c#unity3d

解决方案


那么有一些提示可能对你有帮助

  1. 如果需要,请使用对象池,因为您不必一次又一次地销毁和实例化云。
  2. 尝试避免 Foreach 循环现在并不明显,但它确实对 for 循环有影响。

如果 for each 循环用于对象的集合或数组(即除原始数据类型以外的所有元素的数组),则在 for each 循环结束时调用 GC(垃圾收集器)以释放引用变量的空间。

foreach (Gameobject obj in Collection)
{
    //Code Do Task
}

而 for 循环用于使用索引迭代元素,因此与非原始数据类型相比,原始数据类型不会影响性能。

for (int i = 0; i < Collection.length; i++){
    //Get reference using index i;
    //Code Do Task
}

推荐阅读