unity3dUGUI之UI粒子特效自适应缩放

作者: 好怕怕 | 来源:发表于2016-12-19 22:38 被阅读269次
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
  
public class UIParticleScale : MonoBehaviour {
    private List<ScaleData> scaleDatas = null;
    void Awake()
    {
        scaleDatas = new List<ScaleData>();
        foreach( ParticleSystem p in transform.GetComponentsInChildren<ParticleSystem>(true)){
            scaleDatas.Add(new ScaleData(){transform = p.transform,beginScale = p.transform.localScale});
        }
    }
  
    void Start () 
    {
        float designWidth = 1136;//开发时分辨率宽
        float designHeight = 640;//开发时分辨率高
        float designScale  =   designWidth/designHeight;
        float scaleRate  =   (float)Screen.width/(float)Screen.height;
  
        foreach(ScaleData scale in scaleDatas)
        {
            if(scale.transform != null){
                if(scaleRate<designScale)
                {
                    float scaleFactor = scaleRate / designScale;
                    scale.transform.localScale = scale.beginScale * scaleFactor;
                }else{
                    scale.transform.localScale  = scale.beginScale;
                }
            }
        }
    }
     
#if UNITY_EDITOR
    void Update () {
        Start(); //Editor下修改屏幕的大小实时预览缩放效果
    }
#endif
    class ScaleData
    {
        public Transform transform;
        public Vector3 beginScale = Vector3.one;
    }
}

相关文章

网友评论

    本文标题:unity3dUGUI之UI粒子特效自适应缩放

    本文链接:https://www.haomeiwen.com/subject/qofxvttx.html