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 RazRael · Dec 29, 2016 at 10:03 AM · editorselectionundo

Custom editor: undo change to editor property

I'm writing a custom editor to quickly define paths in the Scene view: basic functionality is to select a point in the path, move it around, add a line starting from the selected point and so on.

How can I add an Undo step when the user selects a different point in the path? This is exactly what the Unity Editor does when the selected GameObject changes.

Here is my code for the selection logic, in my editor class:

 // 'Curve' is my MonoBehaviour which defines a path in the game world.
 
 [SerializeField]
 private Curve curve_selected = null; // reference to the selected curve
 
 [SerializeField]
 private int point_selected_index = 0; // index of the selected point in the curve
 
 private Vector3 ShowPoint(Curve curve, int point_index)
 {
     Vector3 point = curve.GetControlPoint(point_index); // gets the point in world space
     
     Handles.color = Color.white;
     if (Handles.Button(point, Quaternion.identity, 0.04f, 0.06f, Handles.DotCap))
     {
         // point has been clicked in the Scene view: select it.
         //Undo.RecordObject(this, "Point Change"); // doesn't work! ('this' is the editor class)
         this.curve_selected = curve;
         this.point_selected_index = point_index;
         Repaint();
     }
     
     return point;
 }

My editor already supports Undo for changes to the path (i.e. the Curve script); since the reference to the selected point in the path is in my editor class (the 'curve_selected' and 'point_selected_index' fields), I've been unable to record the change to the editor.

I would be able to add the Undo step if I moved the reference to my path script, but it doesn't seem the right place to me: the point selection logic is only relevant in the Editor, not during gameplay.

Comment
Add comment · Show 4
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 flashframe · Dec 29, 2016 at 10:45 AM 0
Share

Did you try wrapping it in an EditorGUI.BeginChangeCheck?

 EditorGUI.BeginChangeCheck();
 if (Handles.Button(point, Quaternion.identity, 0.04f, 0.06f, Handles.DotCap))
      {
          // point has been clicked in the Scene view: select it.
           if (EditorGUI.EndChangeCheck())
          {
                Undo.RecordObject(this, "Point Change"); //might need to be "target"
                this.curve_selected = curve;
                this.point_selected_index = point_index;
                Repaint();
           }
      }


(That code is off the top of my head, so might need adjusting)

avatar image RazRael flashframe · Dec 29, 2016 at 10:59 AM 0
Share

Tested now, does not work and stops the point selection from working. Likely EditorGUI.EndChangeCheck returns false even if the Handles.Button is pressed; to clarify, the above code is called in OnSceneGUI.

avatar image flashframe RazRael · Dec 29, 2016 at 11:29 AM 1
Share

Hmm, yeah sorry. On reflection, the ChangeCheck isn't wrapping anything that's changing. You can use Begin/EndChangeCheck in OnSceneGUI though.

I agree that your path selection variable really belongs in the editor class, but as a work around you could put it in your path script surrounding by #if UNITY_EDITOR // #endif

Hopefully someone has solution!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

73 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

Related Questions

How to properly handle Undo events in custom inspector? 0 Answers

Undo asset replace? *emergency D:* 3 Answers

Auto-Selecting something else in Scene-View? 0 Answers

Selecting a range of hex tiles within radius of main tile 1 Answer

Editor Selection and getting a different currentActiveGameObject. 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