c# - 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.
解决方案
那么有一些提示可能对你有帮助
- 如果需要,请使用对象池,因为您不必一次又一次地销毁和实例化云。
- 尝试避免 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
}
推荐阅读
- gcc - 原子困境:它甚至可以在任何可用情况下替换互斥锁吗?
- azure - Azure ADLS Gen2 监控文件的 ACL 权限
- c++ - 如何用自己的程序打包 Opencv 并将其处理到新机器上
- android-studio - 有没有办法测量和比较部署在手机中的深度学习模型的电池消耗?
- python - Flask Reverse 布局模式以从多个组件/模板创建页面
- docker - GO / Golang 在 Docker 问题上运行项目 - 找不到包(内部文件夹)
- python - 使用 cv2 对图像的显着性图的值进行归一化
- microservices - 编排传奇不会“幸存”服务故障
- python - 从 doc 到 docx 的 Libreoffice 转换卡在 ubuntu 终端中
- powershell - 如何在我刚刚在同一个 powershell 脚本中创建的文件夹中创建一个 gpresult 文件?