码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-10 20:55:12      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:code   using   开发   set   object   www   values   arch   index   

结合了下面这两个方案:

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上缩放(自适应屏幕)

标签:code   using   开发   set   object   www   values   arch   index   

原文地址:http://www.cnblogs.com/kuluodisi/p/7341079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!