unity特效ParticleSystem在UI上缩放(自适应屏幕)

Posted 爬坑菜鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity特效ParticleSystem在UI上缩放(自适应屏幕)相关的知识,希望对你有一定的参考价值。

结合了下面这两个方案:

http://www.xuanyusong.com/archives/4271

http://www.unity.5helpyou.com/3630.html

第一个方案,应付不了复杂些的特效;

两篇文章结合后的代码如下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ScaleParticles : MonoBehaviour {
    private List<float> m_initialSizes = new List<float>();

    public void CacheParticleScale() {
        // Save off all the initial scale values at start.
        ParticleSystem[] particles = gameObject.GetComponentsInChildren<ParticleSystem>();
        for (int i=0;i<particles.Length;i++) {
            m_initialSizes.Add(particles[i].startSize);
            
            ParticleSystemRenderer renderer = particles[i].GetComponent<ParticleSystemRenderer>();
            if (renderer) {
                m_initialSizes.Add(renderer.lengthScale);
                m_initialSizes.Add(renderer.velocityScale);
            }
        }
    }

    public void ResetParticleScale() {
        float designWidth = 1920;//开发时分辨率宽
        float designHeight = 1080;//开发时分辨率高
        float designScale = designWidth / designHeight;
        float scaleRate = (float)Screen.width / (float)Screen.height;
        float scaleFactor = scaleRate / designScale;

        // Scale all the particle components based on parent.
        int arrayIndex = 0;
        ParticleSystem[] particles = gameObject.GetComponentsInChildren<ParticleSystem>();
        for (int i = 0; i < particles.Length; i++) {
            float rate = 1;
            if (scaleRate < designScale) {
                rate = scaleFactor;
            }
            else {
                rate = 1;
            }

            particles[i].startSize = m_initialSizes[arrayIndex++] * rate;
            ParticleSystemRenderer renderer = particles[i].GetComponent<ParticleSystemRenderer>();
            if (renderer) {
                renderer.lengthScale = m_initialSizes[arrayIndex++] *
                rate;
                renderer.velocityScale = m_initialSizes[arrayIndex++] *
                rate;
            }
        }
    }
}

 

以上是关于unity特效ParticleSystem在UI上缩放(自适应屏幕)的主要内容,如果未能解决你的问题,请参考以下文章

Unity开发踩坑-粒子特效(ParticleSystem)性能优化

Unity 特效 粒子 ParticleSystem 划重点

[Unity 3D] 权游红袍女在火中看到了什么,我看到了...(粒子系统 | 火焰特效 | ParticleSystem | 手把手制作)

[Unity 3D] 权游红袍女在火中看到了什么,我看到了...(粒子系统 | 火焰特效 | ParticleSystem | 手把手制作)

Unity3D粒子系统ParticleSystem

Unity—ParticleSystem(粒子系统)与Animator(动画状态机)批量管理器