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;
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM