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 schadmigo · Mar 26, 2017 at 09:19 AM · editor-scriptingcustom editoroninspectorgui

Editor Scripting: best way to change variables of a custom editor?

Hello,

I am trying to learn editor scripting. So I made a CustomEditor which changes the size of a selected cube.

Now I am unsure about how to change the variable "size" from the editor, ideally I'd like to just type the number into the inspector. Which does not work, probably because custom inspectors create different instances for every selected object. The best solution I found so far is to use a slider, but that feels like a somewhat dirty hack, so I would like to know how this is supposed to be done.

 // I have a custom Editor called CubeEditor, which is supposed to change the sizes of cubes
 [CustomEditor(typeof(Cube))]
 public class CubeEditor : Editor {
 
 
     public float size = 2;
 
     void OnSceneGUI()
     {
         //scale selected cube according to size
         Selection.activeTransform.localScale = (new Vector3(size, size, size));
     }
 
     public override void OnInspectorGUI()
     {
         //this is the best solution I have come up so far
         size =  GUILayout.HorizontalSlider(size,0f,100f);
         DrawDefaultInspector();       
     }
 
 
 }
 

Thank You

Comment
Add comment · Show 1
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 schadmigo · Mar 24, 2017 at 02:06 PM 0
Share

For anyone interested, I now have a solution which I still deem overly complicated, but, hey, it works.

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 // I have a custom Editor called CubeEditor, which is supposed to change the sizes of cubes
 [CustomEditor(typeof(Cube))]
 public class CubeEditor : Editor {
     static float size = 2;
 
     //OnEnable is called whenever a different cube gets selected + on initialization
     void OnEnable()
     {
         //load current size from cube
         if (Selection.activeTransform) { size = Selection.activeTransform.localScale.x; }
     }
 
     //OnInspectorGUI is only called when a cube is selected (due to the attribute [CustomEditor(typeof(Cube))])
     public override void OnInspectorGUI()
     {
         //a text field + draw a slider 
         GUILayout.BeginHorizontal();
         size = float.Parse(GUILayout.TextField(size.ToString(),GUILayout.Width(100f)));
         size = GUILayout.HorizontalSlider(size, 0f, 100f);
         //apply "size" to the selected object
         Selection.activeTransform.localScale = (new Vector3(size, size, size));
         GUILayout.EndHorizontal();     
     }
 
 
 }
 

2 Replies

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

Answer by alexanderameye · Mar 26, 2017 at 12:42 PM

To change the size of a cube in edit-mode with a simple inspector variable, try this:

Script attached to the cube: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Size : MonoBehaviour
  {
     public float SizeOfCube = 1.0f;
 }
 

Script in your Editor folder: using UnityEditor; using UnityEngine;

 [CustomEditor( typeof(Size))]
 public class SizeEditor : Editor
 {
 
     public override void OnInspectorGUI()
     {
         Size size = target as Size;
 
         DrawDefaultInspector();
 
         size.transform.localScale = Vector3.one * size.SizeOfCube;
     }
 }
 
Comment
Add comment · 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
1

Answer by DSivtsov · Dec 13, 2020 at 03:59 AM

@alexanderameye showed variant when size of Cube stored in special Class Size which attached to GameObject Cube and change it value through the property Editor.target.

I decide to create a script which customize Scale properties of Transform component for every object in Editor and change it value through the property Editor.serializedObject that demands to use methods for SerializedObject & SerializedProperty objects, but have some advantage - "Automatic handling of multi-object editing, undo, and Prefab overrides".

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 // Custom Editor using SerializedProperties.
 // Automatic handling of multi-object editing, undo, and Prefab overrides.
 [CustomEditor(typeof(Transform))]
 [CanEditMultipleObjects]
 public class ChangeScaleBySlider : Editor
 {
     const string scalePropertySerializedName = "m_LocalScale";
     SerializedProperty scaleProperty;
 
     void OnEnable()
     {
         // Setup the SerializedProperties
         this.scaleProperty = this.serializedObject.FindProperty(scalePropertySerializedName);
     }
 
     public override void OnInspectorGUI()
     {
         // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
         serializedObject.Update();
 
         // Show the default GUI controls except scaleProperty
         DrawPropertiesExcluding(serializedObject, scalePropertySerializedName);
 
         // Show the custom GUI controls
         float commonScale = this.scaleProperty.vector3Value.x;
         commonScale = EditorGUILayout.Slider("Scale", commonScale, 0, 10f);
         this.scaleProperty.vector3Value = new Vector3(commonScale, commonScale, commonScale);
 
         // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
         serializedObject.ApplyModifiedProperties();
     }
 }

P.S> The rotation field property drawer is handled a bit differently, because for rotation field the internal store format is Quaternion, but the Editor showing the euler rotation format. Therefore the use of DrawPropertiesExcluding(...) (and DrawDefaultInspector() also) doesn't give possibility to receive standard GUI format. It is required to completely rewrite the graphical interface for the Transform component, if you what to receive GUI one to one with original (Here the link to corresponding article).

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

( Schrödinger's boolean?) custom Editor class OnSceneGUI() and OnInspectorGUI() accessing variable problem!! 2 Answers

How to create enum out of array of string? 2 Answers

Custom gizmo tools 0 Answers

Using EditorGUILayout in my PropertyDrawer for an attribute causes ArgumentException 1 Answer

How to prevent prefab of custom editor inspector from overriding array size. 1 Answer


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