今天遇到一个需求,需要批量修改粒子系统的startDelay属性值,Unity编辑器本来支持多选然后赋值,但是现在需要是要在原有的值上加(+)一个值.于是就想到自己扩展下编辑器支持这个操作.
代码如下:
using UnityEngine;
using UnityEditor;
public class EffectEditorEx : EditorWindow
{
private static EffectEditorEx m_editor_ex;
[MenuItem("Duole/EffectEditorEx/Open",false,401)]
static void OpenEditor()
{
if (m_editor_ex == null)
{
m_editor_ex = EditorWindow.GetWindow<EffectEditorEx>("EffectEditorEx", true);
m_editor_ex.autoRepaintOnSceneChange = true;
}
m_editor_ex.Show(true);
}
private float m_start_delay = 0f;
private void OnGUI()
{
GUILayout.Label("Start Delay:");
m_start_delay = EditorGUILayout.FloatField(m_start_delay);
if (GUILayout.Button("+"))
{
var items = Selection.GetFiltered<ParticleSystem>(SelectionMode.ExcludePrefab);
foreach (var item in items)
{
var maindata = item.main;
maindata.startDelay = new ParticleSystem.MinMaxCurve(maindata.startDelay.constant + m_start_delay);
}
}
if (GUILayout.Button("x"))
{
var items = Selection.GetFiltered<ParticleSystem>(SelectionMode.ExcludePrefab);
foreach (var item in items)
{
var maindata = item.main;
maindata.startDelay = new ParticleSystem.MinMaxCurve(maindata.startDelay.constant * m_start_delay);
}
}
if (GUILayout.Button("="))
{
var items = Selection.GetFiltered<ParticleSystem>(SelectionMode.ExcludePrefab);
foreach (var item in items)
{
var maindata = item.main;
maindata.startDelay = new ParticleSystem.MinMaxCurve(m_start_delay);
}
}
}
}
这个过程中,坑的一点是ParticleSystem.main是只读的struct,我找了半天没找到赋值的方法.结果自己测试了下,发现直接修改这个ParticleSystem.main得到的MainModule中对应的值就可以了(一脸懵逼).