Custom Inspector for Particle System
Im making a custom inspector for the particle system, I have a slider which alters the emission rate. I have 2 problems:
1. The particle system is no longer rendered in scene view.
2. The slider immediately reverts. If I play the game I can use the slider to change the emission but the slider dosn't move (or returns next frame). The same is true for the floatField.
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(ParticleSystem))]
public class ParticleCustomizer : Editor {
private ParticleSystem ps;
public override void OnInspectorGUI() {
ps = (ParticleSystem)target;
ps.emissionRate = EditorGUILayout.Slider("Emission:", 50, 0, 100);
}
}
Answer by mikelortega · Nov 13, 2015 at 09:13 AM
The second problem is you should set the slider value to ps.emissionRate, like this:
ps.emissionRate = EditorGUILayout.Slider("Emission:", ps.emissionRate, 0, 100);
But the thing is, you shouldn't overwrite the particle system's default editor inspector. This inspector allows you simulating in edit time and many other things that you shouldn't overwrite.
Answer by fuzzalicious · Nov 13, 2015 at 03:42 PM
Ah, of course. ^^ As for overwriting, I want to add functionality to the editor like randomization, don't I then have to overwrite?
To do that, I would create a new component that implements your randomization functionality, and add it to the gameobject that contains the particle system. So you have a GameObject with the particle system and your own functionality next to it.
How do I go about accessing the particle in edit mode when using a custom class? When I overrode the particle I could access it simply using Editor.target. Now I have an empty custom class which uses [RequireComponent(typeof(UnityEngine.ParticleSystem))] and then another class as an editor for the custom class.
I mean using a new component and it's editor. Something like this:
ParticleCustomizer.cs
using UnityEngine;
[ExecuteInEdit$$anonymous$$ode]
[RequireComponent(typeof(ParticleSystem))]
public class ParticleCustomizer : $$anonymous$$onoBehaviour
{
ParticleSystem m_Ps;
void Start()
{
m_Ps = GetComponent<ParticleSystem>();
}
public float emissionRate
{
set { m_Ps.emissionRate = value; }
get { return m_Ps.emissionRate; }
}
}
EditParticleCustomizer.cs
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(ParticleCustomizer))]
public class EditParticleCustomizer : Editor
{
public override void OnInspectorGUI()
{
ParticleCustomizer pc = (ParticleCustomizer)target;
pc.emissionRate = EditorGUILayout.Slider("Emission:", pc.emissionRate, 0, 100);
}
}