随风逐叶 随风逐叶
首页
  • Quick Reference (opens new window)
  • EgretEngine开发者文档 (opens new window)
  • TinaX框架
  • SSH教程
  • VSCode插件开发
关于
  • 分类
  • 标签
  • 归档

rontian

从事游戏开发10多年的老菜鸟一枚!
首页
  • Quick Reference (opens new window)
  • EgretEngine开发者文档 (opens new window)
  • TinaX框架
  • SSH教程
  • VSCode插件开发
关于
  • 分类
  • 标签
  • 归档
  • Unity

    • Unity3D UGUI生成并播放序列帧动画prefab
    • Unity3D UGUI 粒子特效层级与缩放
      • UGUI 新手引导镂空代码
      • Unity复制GameObject路径到剪切板
      • Unity模型变灰Shader
      • unity中判断iphone设备型号的方法
      • Unity Time详解
    • 游戏开发
    • Unity
    rontian
    2023-10-16
    目录

    Unity3D UGUI 粒子特效层级与缩放

    # 设置Canvas为Space Camera模式

    1.png

    # 粒子层级ParticleDepth组件

    2.png

    # UI粒子缩放

    (5.3以前)使用ParticleScaler组件 (5.3以后)设置ParticleSystem的scalingMode为Hierarchy即可 Hierarachy:当前粒子大小会受到自身和上一级对象的缩放影响 4.png

    # UI层级,UIDepth组件

    3.png

    # 设置每个Panel的层级

    public static void SetPanelOrder(GameObject panel, int order)
            {
                Canvas canvas = panel.GetComponent<Canvas>();
                if (canvas == null)
                {
                    canvas = panel.AddComponent<Canvas>();
                }
                canvas.overrideSorting = true;
                canvas.sortingOrder = order;
                GraphicRaycaster raycaster = panel.GetComponent<GraphicRaycaster>();
                if (raycaster == null)
                {
                    raycaster = panel.AddComponent<GraphicRaycaster>();
                }
                UIDepth[] subs = panel.GetComponentsInChildren<UIDepth>(true);
                foreach (UIDepth depth in subs)
                {
                    depth.SetOrder(order);
                }
                ParticleDepth[] particles = panel.GetComponentsInChildren<ParticleDepth>(true);
                foreach (ParticleDepth depth in particles)
                {
                    depth.SetOrder(order + 1);
                }
            }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    # 相关组件代码

    ParticleDepth.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    namespace LuaFramework
    {
        public class ParticleDepth : MonoBehaviour
        {
            public int order = 1;
            public int offset = 0;
            private Renderer[] Renderers;
            void Start()
            {
                SetOrder(order);
            }
    
            public void SetOrder(int order)
            {
                this.order = order;
                Renderers = GetComponentsInChildren<Renderer>(true);
                foreach (Renderer render in Renderers)
                {
                    render.sortingOrder = order+offset;
                }
            }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26

    ParticleScaler.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    namespace LuaFramework
    {
        public class ParticleScaler : MonoBehaviour
        {
            public class ParticleItem
            {
                public Transform transform;
                public Vector3 localScale;
                public Vector3 localPosition;
            }
            //是否在Update中更新
            public bool canUpdate = false;
            //UI设计分辨率
            public int design_width = 960;
            public int design_height = 640;
            //特效关联的对象(Scale会改变的对象)
            public Transform target = null;
            protected List<ParticleItem> items;
            void Awake()
            {
                items = new List<ParticleItem>();
                ParticleSystem[] list = GetComponentsInChildren<ParticleSystem>(true);
                foreach (ParticleSystem ps in list)
                {
                    items.Add(new ParticleItem() { transform = ps.transform, localScale = ps.transform.localScale, localPosition = transform.localPosition });
                }
            }
    
            void Start()
            {
                ScaleUpdate();
            }
    
            void Update()
            {
                if (canUpdate)
                {
                    ScaleUpdate();
                }
            }
    
            void ScaleUpdate()
            {
                int screen_width = Screen.width;
                int screen_height = Screen.height;
                float design_rate = (float)design_width / (float)design_height;
                float screen_rate = (float)screen_width / (float)screen_height;
                float scale_factor = screen_rate/design_rate;
                float target_fcator = 1;
                if (target != null)
                {
                    target_fcator = target.localScale.x;
                }
                foreach (ParticleItem item in items)
                {
                    if (item.transform != null)
                    {
                        item.transform.localScale = item.localScale * scale_factor * target_fcator;
                        item.transform.localPosition = item.localPosition * scale_factor * target_fcator;
                    }
                }
            }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68

    UIDepth.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    namespace LuaFramework
    {
        public class UIDepth : MonoBehaviour
        {
            public int order = 1;
            public int offset = 5;
            private Renderer[] Renderers;
            void Start()
            {
                SetOrder(order);
            }
    
            public void SetOrder(int order)
            {
                this.order = order;
    
                Canvas canvas = GetComponent<Canvas>();
                if (canvas == null)
                {
                    canvas = gameObject.AddComponent<Canvas>();
                }
                canvas.overrideSorting = true;
    			canvas.sortingOrder = this.order+offset;
                
                GraphicRaycaster raycaster = GetComponent<GraphicRaycaster>();
                if (raycaster == null)
                {
                    raycaster = gameObject.AddComponent<GraphicRaycaster>();
                }
            }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    上次更新: 2023/10/16, 17:53:06 访问次数: 0
    Unity3D UGUI生成并播放序列帧动画prefab
    UGUI 新手引导镂空代码

    ← Unity3D UGUI生成并播放序列帧动画prefab UGUI 新手引导镂空代码→

    最近更新
    01
    一些Shell常用的功能写法整理
    10-20
    02
    删除git仓库submodule的步骤
    10-20
    03
    django基本命令
    10-16
    更多文章>
    Copyright © 2017-2025 随风逐叶
    沪ICP备18008791号-1 | 沪公网安备31011502401077号

    网站访问总次数: 0次
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式