8

UGUI研究院之UI粒子特效自适应缩放(二十二)

 3 years ago
source link: https://www.xuanyusong.com/archives/4271
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

UGUI研究院之UI粒子特效自适应缩放(二十二)

雨松MOMO 【UGUI研究院】 围观3107421 条评论 编辑日期:2016-12-14 字体:

我们UI自适应采用的是Canvas Scaler 设置 Expand模式。 那么UI可以很好的自适应屏幕,可是粒子特效就会出问题所以写下了这个脚本来自适应UI的粒子特效。

比如技能图标的特效光圈。

UGUI研究院之UI粒子特效自适应缩放(二十二) - 雨松MOMO程序研究院 - 1

当屏幕缩放后,UI变小了,可是特效出问题了。

UGUI研究院之UI粒子特效自适应缩放(二十二) - 雨松MOMO程序研究院 - 2

测试环境unity5.3.5

代码如下所示:

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;

欢迎大家测试~

作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK