ParticleEffectForUGUI/Editor/UIParticleEditor.cs

102 lines
3.4 KiB
C#
Raw Normal View History

2018-06-22 18:48:14 +08:00
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
2018-11-28 18:55:56 +08:00
using System.Collections.Generic;
using System.Linq;
2018-06-22 18:48:14 +08:00
namespace Coffee.UIExtensions
{
2018-11-28 15:36:16 +08:00
[CustomEditor (typeof (UIParticle))]
2018-06-22 18:48:14 +08:00
[CanEditMultipleObjects]
public class UIParticleEditor : GraphicEditor
{
//################################
// Constant or Static Members.
//################################
2018-11-28 15:36:16 +08:00
static readonly GUIContent contentParticleMaterial = new GUIContent ("Particle Material", "The material for rendering particles");
static readonly GUIContent contentTrailMaterial = new GUIContent ("Trail Material", "The material for rendering particle trails");
2018-11-28 18:55:56 +08:00
static readonly List<ParticleSystem> s_ParticleSystems = new List<ParticleSystem> ();
2018-06-22 18:48:14 +08:00
//################################
// Public/Protected Members.
//################################
/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
2018-11-28 15:36:16 +08:00
protected override void OnEnable ()
2018-06-22 18:48:14 +08:00
{
2018-11-28 15:36:16 +08:00
base.OnEnable ();
_spParticleSystem = serializedObject.FindProperty ("m_ParticleSystem");
_spTrailParticle = serializedObject.FindProperty ("m_TrailParticle");
2018-11-28 18:55:56 +08:00
_spScale = serializedObject.FindProperty ("m_Scale");
_spIgnoreParent = serializedObject.FindProperty ("m_IgnoreParent");
2018-06-22 18:48:14 +08:00
}
/// <summary>
/// Implement this function to make a custom inspector.
/// </summary>
2018-11-28 15:36:16 +08:00
public override void OnInspectorGUI ()
2018-06-22 18:48:14 +08:00
{
2018-11-28 15:36:16 +08:00
serializedObject.Update ();
2018-06-22 18:48:14 +08:00
2018-11-28 15:36:16 +08:00
EditorGUILayout.PropertyField (_spParticleSystem);
2018-06-22 18:48:14 +08:00
EditorGUI.indentLevel++;
var ps = _spParticleSystem.objectReferenceValue as ParticleSystem;
if (ps)
{
2018-11-28 15:36:16 +08:00
var pr = ps.GetComponent<ParticleSystemRenderer> ();
var sp = new SerializedObject (pr).FindProperty ("m_Materials");
2018-06-22 18:48:14 +08:00
2018-11-28 15:36:16 +08:00
EditorGUILayout.PropertyField (sp.GetArrayElementAtIndex (0), contentParticleMaterial);
EditorGUILayout.PropertyField (sp.GetArrayElementAtIndex (1), contentTrailMaterial);
sp.serializedObject.ApplyModifiedProperties ();
2018-06-22 18:48:14 +08:00
2018-11-28 15:36:16 +08:00
if (!Application.isPlaying && pr.enabled)
2018-06-22 18:48:14 +08:00
{
2018-11-28 15:36:16 +08:00
EditorGUILayout.HelpBox ("ParticleSystemRenderer will be disable on playing.", MessageType.Info);
2018-06-22 18:48:14 +08:00
}
}
EditorGUI.indentLevel--;
2018-11-28 15:36:16 +08:00
EditorGUI.BeginDisabledGroup (true);
EditorGUILayout.PropertyField (_spTrailParticle);
EditorGUI.EndDisabledGroup ();
2018-06-22 18:48:14 +08:00
2018-11-28 18:55:56 +08:00
var current = target as UIParticle;
EditorGUILayout.PropertyField (_spIgnoreParent);
EditorGUI.BeginDisabledGroup (!current.isRoot);
EditorGUILayout.PropertyField (_spScale);
EditorGUI.EndDisabledGroup ();
current.GetComponentsInChildren<ParticleSystem> (true, s_ParticleSystems);
if (s_ParticleSystems.Any (x => x.GetComponent<UIParticle> () == null))
{
2018-11-28 18:55:56 +08:00
GUILayout.BeginHorizontal ();
EditorGUILayout.HelpBox ("There are child ParticleSystems that does not have a UIParticle component.\nAdd UIParticle component to them.", MessageType.Warning);
GUILayout.BeginVertical ();
if (GUILayout.Button ("Fix"))
{
foreach (var p in s_ParticleSystems.Where (x => !x.GetComponent<UIParticle> ()))
{
p.gameObject.AddComponent<UIParticle> ();
}
}
GUILayout.EndVertical ();
GUILayout.EndHorizontal ();
}
2018-11-28 18:55:56 +08:00
s_ParticleSystems.Clear ();
2018-11-28 15:36:16 +08:00
serializedObject.ApplyModifiedProperties ();
2018-06-22 18:48:14 +08:00
}
//################################
// Private Members.
//################################
SerializedProperty _spParticleSystem;
SerializedProperty _spTrailParticle;
2018-11-28 18:55:56 +08:00
SerializedProperty _spScale;
SerializedProperty _spIgnoreParent;
2018-06-22 18:48:14 +08:00
}
}