Unity 編輯器下控制粒子播放跟隨位移,unity粒子
在之前的文章《Unity 編輯器下控制播放粒子》講到在Unity編輯器的Scene視圖進行控制播放粒子ParticleSystem,但是當這個粒子是掛載在人物身體部位的時候,會有可能出現不跟隨位移的情況。尋找原因,發現是Resimulate 被勾選中了,這個選項是指當粒子參數改變時,立即更新粒子效果。要讓粒子也能跟隨移動,必須將這個選項取消掉。
可以簡單的在編輯器下,取消掉這個選項,如下:
但是,對於其他人員可能不知道這個原因,手動設定不夠智能,需要進一步在代碼中主動控制。操縱這個選項,需要取得編輯器類,通過反射來得到,如下:
C# Code
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
|
|
using System;
using System.Reflection;
using UnityEditor;
public static class EditorParticleSystemHelper
{
private static Type realType;
private static PropertyInfo property_editorResimulation;
public static void InitType()
{
if (realType == null)
{
var assembly = Assembly.GetAssembly(typeof(Editor));
realType = assembly.GetType("UnityEditor.ParticleSystemEditorUtils");
property_editorResimulation = realType.GetProperty("editorResimulation", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
}
}
/// <summary>
/// 必須禁止粒子的Resimulation,這樣才能讓粒子跟隨位移
/// </summary>
public static bool editorResimulation
{
set
{
InitType();
property_editorResimulation.SetValue(null, value, null);
}
}
}
|
那麼只要在代碼中合適的地方,使用如下即可: C# Code
1
|
|
EditorParticleSystemHelper.editorResimulation = false; |