- Home /
[Custom Inspector] Property resets after starting playmode
Hey guys, I try to use a custom inspector for my laser handler, but every time I set the property "EmissionColor" to something and start the playmode it resets. I know I have to serialize it, but I don't know how to do it in this case, can you please help me?
Here is my Code: using UnityEngine; using System.Collections; using UnityEditor;
[CustomEditor(typeof(LaserHandler))]
public class LaserHandlerEditor : Editor {
public override void OnInspectorGUI()
{
LaserHandler myLaserhandler = (LaserHandler)target;
DrawDefaultInspector();
if (myLaserhandler.type.Equals("Emitter"))
{
myLaserhandler.emissionColor = EditorGUILayout.TextField("Emission Color", myLaserhandler.emissionColor);
string emissionColor = myLaserhandler.emissionColor;
Utility.colorStringToBools(emissionColor, out myLaserhandler.emitRed, out myLaserhandler.emitGreen, out myLaserhandler.emitBlue);
}
else if (myLaserhandler.type.Equals("Filter"))
{
string filterColor = myLaserhandler.filterColor = EditorGUILayout.TextField("Filter Color", myLaserhandler.filterColor);
Utility.colorStringToBools(filterColor, out myLaserhandler.filterRed, out myLaserhandler.filterGreen, out myLaserhandler.filterBlue);
}
}
}
This is the class the inspector is for(only the variable "emissionColor" matter):
using UnityEngine;
using System.Collections;
public class LaserHandler : MonoBehaviour {
public LaserMechanism[] lasers = new LaserMechanism[16];
public bool[] input = new bool[16];
public string type;
public int rotation;
[HideInInspector]
public string emissionColor;
[HideInInspector]
public bool emitRed, emitGreen, emitBlue;
[HideInInspector]
public string filterColor;
[HideInInspector]
public bool filterRed, filterGreen, filterBlue;
public Object laserPrefab;
public void SetLaser(int direction,string Color)
{
Debug.Log(Color);
GameObject newLaser = Instantiate(laserPrefab, transform.position, transform.rotation) as GameObject;
LaserMechanism newMechanism = newLaser.GetComponent<LaserMechanism>();
newMechanism.start = this;
newMechanism.direction = direction;
lasers[direction] = newMechanism;
input[direction] = false;
newMechanism.SetupLaser(Color);
}
public void recieveLaser(LaserMechanism recievedLaser)
{
int Inputdirection = Utility.fixDirection(recievedLaser.direction + 8);
lasers[Inputdirection] = recievedLaser;
input[Inputdirection] = true;
HandleInput();
}
public void ResetLaser(int direction)
{
lasers[direction].RemoveLaser(); //tells Laser to be removed --> tells next Reciever to remove Input
GameObject.Destroy(lasers[direction].gameObject);
lasers[direction] = null;
input[direction] = false;
}
public void ResetRecieveLaser(LaserMechanism recievedLaser)
{
int Inputdirection = recievedLaser.direction + 8;
if (Inputdirection >= 16) Inputdirection -= 16;
lasers[Inputdirection] = null;
input[Inputdirection] = false;
HandleInput();
}
void Start()
{
if (type.Equals("Emitter"))
{
SetLaser(rotation,emissionColor);
}
}
void resetOutputLasers()
{
for(int outputDir=0; outputDir<15; outputDir++)
{
if (lasers[outputDir] != null && !input[outputDir]) ResetLaser(outputDir);
}
}
void HandleInput()
{
resetOutputLasers();
if (type.Equals("Mirror"))
{
Debug.Log("is Mirror");
for (int inputDirection = 0; inputDirection < 15; inputDirection++)
{
Debug.Log(inputDirection);
if (lasers[inputDirection] != null && input[inputDirection])
{
Debug.Log("Found input laser");
//Get reflection Direction
if (inputDirection == rotation || inputDirection == Utility.fixDirection(rotation + 4) || inputDirection == Utility.fixDirection(rotation - 4) || inputDirection == Utility.fixDirection(rotation + 8))
{
//Do nothing
}
else
{
int reflectionDir = Utility.fixDirection(rotation - (inputDirection - rotation));
string reflectionColor = lasers[inputDirection].laserColor;
SetLaser(reflectionDir,reflectionColor);
}
}
}
}
else if (type.Equals("Filter"))
{
//Get Input Colors
bool red, green, blue;
if(lasers[rotation]!=null && input[rotation])
{
red = lasers[rotation].red;
green = lasers[rotation].green;
blue = lasers[rotation].blue;
}
}
}
#region Modify Transform
public void RotateClockwise(int n) {
rotation = Utility.fixDirection(rotation + n);
HandleInput();
}
public void RotateCounterClockwise(int n)
{
rotation = Utility.fixDirection(rotation - n);
HandleInput();
}
#endregion
}
Answer by Adam-Mechtley · Jan 06, 2017 at 07:48 PM
The problem here is that you are not dirtying the serialized data on the target LaserHandler object. There are a number of ways to approach this, but in general the best option for setting properties inside of OnInspectorGUI is to use SerializedProperty instead of directly accessing fields on the target object. Doing so will properly serialize data, handle undo/redo, multi selection, etc. The pattern is something like this:
[CustomEditor (typeof (LaserHandler)), CanEditMultipleObjects]
public class LaserHandlerEditor : Editor {
SerializedProperty m_Type;
SerializedProperty m_EmissionColor;
SerializedProperty m_EmitRed;
SerializedProperty m_EmitGreen;
SerializedProperty m_EmitBlue;
protected virtual void OnEnable () {
m_Type = this.serializedObject.FindProperty ("type");
m_EmissionColor = this.serializedObject.FindProperty ("emissionColor");
m_EmitRed = this.serializedObject.FindProperty ("emitRed");
m_EmitGreen = this.serializedObject.FindProperty ("emitGreen");
m_EmitBlue = this.serializedObject.FindProperty ("emitBlue");
}
public override void OnInspectorGUI () {
DrawDefaultInspector ();
this.serializedObject.Update ();
if (m_Type.stringValue.Equals ("Emitter")) {
EditorGUI.BeginChangeCheck ();
EditorGUILayout.PropertyField (m_EmissionColor);
if (EditorGUI.EndChangeCheck ()) {
bool emitRed, emitGreen, emitBlue;
Utility.colorStringToBools (m_EmissionColor.stringValue, out emitRed, out emitGreen, out emitBlue);
m_EmitRed.boolValue = emitRed;
m_EmitGreen.boolValue = emitGreen;
m_EmitBlue.boolValue = emitBlue;
}
}
else if (m_Type.stringValue.Equals ("Filter")) {
// something similar here
}
this.serializedObject.ApplyModifiedProperties ();
}
}
Thank you Adam-$$anonymous$$echtley, it's works as I wanted :)
Wow, I've been seaching for this for 20 $$anonymous$$utes! It works perfectly!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
All compiler errors have to be fixed, but no errors? 1 Answer
Is there a serialization class similar to JsonUtility but for Byte Serialization. 1 Answer
The scene is opposite when in play mode,The scene moves from one side to other 0 Answers