Unity 编辑器下控制粒子播放跟随位移

        在之前的文章《 Unity 编辑器下控制播放粒子》讲到在Unity编辑器的Scene视图进行控制播放粒子ParticleSystem,但是当这个粒子是挂载在人物身体部位的时候,会有可能出现不跟随位移的情况。查找原因,发现是 Resimulate 被勾选中了,这个选项是指当粒子参数改变时,立即更新粒子效果。要让粒子也能跟随移动,必须将这个选项取消掉。

可以简单的在编辑器下,取消掉这个选项,如下:
Unity 编辑器下控制粒子播放跟随位移_第1张图片

但是,对于其他人员可能不知道这个原因,手动设置不够智能,需要进一步在代码中主动控制。操纵这个选项,需要取得编辑器类,通过反射来得到,如下:
 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;

你可能感兴趣的:(unity)