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
0
Question by VRKeith · Jan 13, 2017 at 08:35 AM · c#editorinspectoreditor-scripting

Custom Editor Script resets values on Play

Alright, this has been driving me nuts.

I've worked with Unity for years, but this is the first time I've ever really made an Editor script. After scrounging around the internet, trying to sift out outdated code and whatnot, I made a script that I can use to specify Ranges (I know a Vector2 would work, but I have plans for this script). However, when I hit play, any values entered in the Inspector reset to 0 (or a default value). What am I doing wrong?

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 #if UNITY_EDITOR
 using UnityEditor;
 using UnityObject = UnityEngine.Object;
 #endif
 
 [System.Serializable]
 public class Range<T> where T : IComparable
 {
     T _min;
     T _max;
 
     public T Min
     {
         get { return _min; }
         set { _min = value;  }
     }
     public T Max
     {
         get { return _max; }
         set { _max = value; }
     }
 
     public Range() {}
 
     public Range(T min, T max)
     {
         if(min.CompareTo(max) > 0)
         {
             Debug.LogError("Error: Range " + min + " is greater than " + max);
         }
         this._min = min;
         this._max = max;
     }
 
     public bool Contains(T value)
     {
         return value.CompareTo(this._min) >= 0 && value.CompareTo(this._max) <= 0;
     }
 }
 #if UNITY_EDITOR
 public abstract class RangeDrawer<T> : PropertyDrawer where T : IComparable
 {
     [SerializeField]
     private Range<T> _range;
     private bool _Foldout;
     private const float kButtonWidth = 18f;
 
     private static readonly Dictionary<Type, Func<Rect, object, object>> _Fields =
         new Dictionary<Type, Func<Rect, object, object>>()
         {
             { typeof(int), (rect, value) => EditorGUI.IntField(rect, (int)value) },
             { typeof(float), (rect, value) => EditorGUI.FloatField(rect, (float)value) },
             { typeof(string), (rect, value) => EditorGUI.TextField(rect, (string)value) },
             { typeof(bool), (rect, value) => EditorGUI.Toggle(rect, (bool)value) },
             { typeof(Vector2), (rect, value) => EditorGUI.Vector2Field(rect, GUIContent.none, (Vector2)value) },
             { typeof(Vector3), (rect, value) => EditorGUI.Vector3Field(rect, GUIContent.none, (Vector3)value) },
             { typeof(Bounds), (rect, value) => EditorGUI.BoundsField(rect, (Bounds)value) },
             { typeof(Rect), (rect, value) => EditorGUI.RectField(rect, (Rect)value) },
         };
 
     private static V DoField<V>(Rect rect, Type type, V value)
     {
         Func<Rect, object, object> field;
         if (_Fields.TryGetValue(type, out field))
             return (V)field(rect, value);
 
         if (type.IsEnum)
             return (V)(object)EditorGUI.EnumPopup(rect, (Enum)(object)value);
 
         if (typeof(UnityObject).IsAssignableFrom(type))
             return (V)(object)EditorGUI.ObjectField(rect, (UnityObject)(object)value, type, true);
 
         Debug.Log("Type is not supported: " + type);
         return value;
     }
 
     private void CheckInitialize(SerializedProperty property, GUIContent label)
     {
         if (_range == null)
         {
             var target = property.serializedObject.targetObject;
             _range = fieldInfo.GetValue(target) as Range<T>;
             if (_range == null)
             {
                 _range = new Range<T>();
                 fieldInfo.SetValue(target, _range);
             }
 
             _Foldout = EditorPrefs.GetBool(label.text);
         }
     }
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         CheckInitialize(property, label);
         if (_Foldout)
             return 51f;
         return 17f;
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         CheckInitialize(property, label);
         position.height = 17f;
 
         var foldoutRect = position;
         foldoutRect.width -= 2 * kButtonWidth;
         EditorGUI.BeginChangeCheck();
         _Foldout = EditorGUI.Foldout(foldoutRect, _Foldout, label, true);
         if (EditorGUI.EndChangeCheck())
             EditorPrefs.SetBool(label.text, _Foldout);
         if (!_Foldout)
             return;
         position.y += 17f;
         T value;
         EditorGUI.BeginChangeCheck();
         value = DoField(position, typeof(T), _range.Min);
         if(EditorGUI.EndChangeCheck())
         {
             _range.Min = value;
         }
         position.y += 17f;
         EditorGUI.BeginChangeCheck();
         value = DoField(position, typeof(T), _range.Max);
         if(EditorGUI.EndChangeCheck())
         {
             _range.Max = value;
         }
         property.serializedObject.ApplyModifiedProperties();
     }
 }
 #endif
 
 

And, for the curious, here's how it's used:

 public class SomeClass : MonoBehaviour {
     [System.Serializable] public class floatRange: Range<float> { }
 
     [SerializeField]
     public floatRange range;
 }
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
4
Best Answer

Answer by Adam-Mechtley · Jan 13, 2017 at 10:27 AM

There are a couple of problems here at a glance.

  1. Your _min and _max fields are not marked with the SerializeField attribute, so there is no way to save changes to them between domain reloads (e.g., playing/stopping the game)

  2. You are editing values on your target (inspected) object directly instead of its serialized data. This by itself is not a problem necessarily, but puts the burden of a lot more work on you to make sure your changes are saved, that they are undoable, and that they work with multiple object selections. Instead of getting the target object and reading/writing to its fields, you should instead use something like property.FindRelative("_min").floatValue if e.g., property.propertyType == SerializedPropertyType.Float. You may find my answer on this question helpful.

Comment
Add comment · Show 1 · 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 VRKeith · Jan 23, 2017 at 07:33 AM 0
Share

I am an idiot, and you are the man! Thank you!

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

8 People are following this question.

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

Related Questions

How to access one class instance in editor script? 1 Answer

How To Force an Inspector to Repaint 3 Answers

Editor Script, Index Out of Range Exception after Play 1 Answer

Gizmos.DrawLine is dissapearing after returning to editor after Playing the scene 0 Answers

TexturePropertySingleLine in Editor class 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