Unity3D UGUI 粒子特效层级与缩放
# 设置Canvas为Space Camera模式
# 粒子层级ParticleDepth组件
# UI粒子缩放
(5.3以前)使用ParticleScaler组件
(5.3以后)设置ParticleSystem的scalingMode为Hierarchy即可
Hierarachy:当前粒子大小会受到自身和上一级对象的缩放影响
# UI层级,UIDepth组件
# 设置每个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
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
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
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
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