Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by El__Nacho · Jan 06, 2017 at 03:09 PM · c#resetcustom-inspectorplaymodeserialize

[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
 
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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 ();
     }
  }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image El__Nacho · Jan 07, 2017 at 03:35 PM 0
Share

Thank you Adam-$$anonymous$$echtley, it's works as I wanted :)

avatar image unity_sJRi2TCc5h5h8A · Apr 23, 2020 at 01:33 PM 0
Share

Wow, I've been seaching for this for 20 $$anonymous$$utes! It works perfectly!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

268 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges