Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
3
Question by Umaymdt · Sep 13, 2017 at 02:43 PM · editoreditor-scriptingscriptableobjecthandles

Drawing position handle from ScriptableObject editor.

I have a ScriptableObject and I want to make an editor so that when the instance of the ScriptableObject is selected in the inspector, it displays a position handle at a point based on a Vector3 variable saved in the ScriptableObject. I want to make it so that when the handle is moved, the variable within the ScriptableObject will update. Pretty much all handle examples I found on the internet use MonoBehaviours. How can I make this work?

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 Papiertig0r · Nov 20, 2020 at 07:27 PM 0
Share

Well, I just found out :D

I used the serializedObject and wrote the values directly to it. With serializedObject.Apply$$anonymous$$odifiedProperties(); these changes will be applied.

 public class Test : ScriptableObject
 {
     public Vector3 point;
 }
 
 [CustomEditor(typeof(Test), true)]
 public class TestEditor : Editor
 {
     private void OnEnable()
     {
         SceneView.duringSceneGui += OnSceneGUI;
     }
 
     void OnDisable()
     {
         SceneView.duringSceneGui -= OnSceneGUI;
     }
 
     private void OnSceneGUI(SceneView sceneView)
     {
         EditorGUI.BeginChangeCheck();
         SerializedProperty testPoint = serializedObject.FindProperty("point");
         Vector3 newPosition = Handles.PositionHandle(testPoint.vector3Value, Quaternion.identity);
 
         if(EditorGUI.EndChangeCheck())
         {
             serializedObject.Apply$$anonymous$$odifiedProperties();
         }
     }
 }


2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by vitiet · Dec 10, 2020 at 09:48 PM

Hello @Umaymdt ,

Even though it's quite late, I'd like to share a solution to this problem. I had the same problem too, and I stumbled upon this post about mastering unity editor handles: https://connect.unity.com/p/mastering-unityeditor-handles

Search for ScriptableObject in the post and you'll find an implementation for undo/redo. Here is the link to the script on Github: https://github.com/alelievr/MasterUnityHandles/blob/master/ExamplesScenes/Editor/CurveScriptableObjectEditor.cs

To make this work, first, you have to register/unregister OnSceneGUI for the event SceneView.duringSceneGui since onSceneGUIDelegated is deprecated. Next, pay attention to the OnSceneGUI(SceneView sv) method, you'll see the GUI.changed check. If changed, you just have to apply the changes made during Handles.PositionHandle operation or whichever method of your choice, back to the target.

Example code:

MyScriptableObject.cs

 using UnityEngine;

 [CreateAssetMenu(fileName = "New SO", menuName = "Create New SO")]
 public class MyScriptableObject : ScriptableObject
 {
     public Vector3 point;
 }

MyScriptableObjectEditor.cs

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(MyScriptableObject))]
 public class MyScriptableObjectEditor : Editor
 {
     void OnEnable()
     {
         SceneView.duringSceneGui += OnSceneGUI;
     }
 
     void OnDisable()
     {
         SceneView.duringSceneGui -= OnSceneGUI;
     }
 
     void OnSceneGUI(SceneView sv)
     {
         MyScriptableObject mySO = (MyScriptableObject)target;
         Vector3 point = mySO.point;

         point = Handles.PositionHandle(point, Quaternion.identity);
 
         if (GUI.changed)
         {
             // for undo operation
             Undo.RecordObject(target, "Move Point");
 
             // apply changes back to target component
             mySO.point = point;
         }
     }
 }


Hope this helps.

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 henryfjones · Apr 24 at 09:36 AM 0
Share

This is exactly what I was after, thank you!

avatar image
0

Answer by Papiertig0r · Nov 20, 2020 at 07:05 PM

I have the same issue, can't find anything after hours of googling and trying :/

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

80 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

Related Questions

Unity inspector scripting - scriptable objects - card game 0 Answers

Confused about custom GameObjects,Custom GameObject confusion 0 Answers

OnInteractivePreviewGUI multiple selection. Works but spams errors. 0 Answers

Editor Script Handles.Label problem. Labels don't move with gameobject 1 Answer

Showing Handles in the Game View 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