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
1
Question by jklw10 · Apr 10, 2020 at 12:33 AM · scripting problempropertydrawercustomizationonscenegui

how do i draw something in the scene for a property

i'm trying to make an attribute that i can add for anything be it monobehaviour or scriptable object that when it's property is drawn it draws something in the scene, currently the drawing part works but after closing the property drawer the custom OnSceneGui function is not removed from the scene and causes a null exception error (even though there's a null check) here's the most working version:

 [CustomPropertyDrawer(typeof(DraggablePoint), true)]
 public class DraggablePointDrawer : PropertyDrawer
 {
 
     SerializedProperty Prop;
 
     private void OnSceneGui(SceneView obj)
     {
         //SceneView.duringSceneGui -= OnSceneGui;
         
         Handles.color = Color.green;
         int id = GUIUtility.GetControlID(FocusType.Passive);
         if (Prop is object)
         {
             Handles.SphereHandleCap(id, Prop.vector3Value, Quaternion.identity, 1, EventType.Repaint);
             Prop.vector3Value = Handles.PositionHandle(Prop.vector3Value, Quaternion.identity);
         }
 
        // SceneView.duringSceneGui -= OnSceneGui;
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUILayout.PropertyField(property);
         if (fieldInfo.FieldType == typeof(Vector3))
         {
             Prop = property;
             SceneView.duringSceneGui -= OnSceneGui;
             SceneView.duringSceneGui += OnSceneGui;
         }//*/
         if (Prop is object)
         {
             property = Prop;
         }
         property.serializedObject.ApplyModifiedProperties();
     }
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return 1;
     }
     
 }
Comment
Add comment · Show 2
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 zORg_alex · May 25 at 06:27 AM 0
Share

I just bumped in the same issue. I can unsubscribe on Selection.selectionChanged. I can monitor if a value changed and I need to update the scene drawer. You can do a trick to unsubscribe when a selection is changed. I had a property with a preview in scene on mouse hover and a toggle that would keep preview on even when mouse is not hovering:

         private void SetUpPreview(bool newPreviewValue)
         {
             if (newPreviewValue != previewAlways)
             {
                 previewAlways = newPreviewValue;
                 SceneView.duringSceneGui -= _PreviewHandlesInternal;
                 if (previewAlways)
                 {
                     SceneView.duringSceneGui += _PreviewHandlesInternal;
                     Selection.selectionChanged += UnsubscribeSetUpPreview;
                 }
             }
 
             void UnsubscribeSetUpPreview()
             {
                 Selection.selectionChanged -= UnsubscribeSetUpPreview;
                 SceneView.duringSceneGui -= _PreviewHandlesInternal;
             }
         }

And yes, nested methods are a thing, so you can unsubscribe themselves, no problemo.

I wish there would be a way to get all property drawers of a type to ask them to draw from a static method, so you would do that only for active ones.

avatar image zORg_alex · May 25 at 06:33 AM 0
Share

I bet you should check if

 if (Prop)

instead of is object. It's a UnityObject. When it's null, it's not a null really. .NET doesn't treat it as null, but fails to read any fields or call on any members. It has it's == operator overloaded.

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

226 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

Related Questions

need help with armor customization 1 Answer

Is there a better way to have custom Script icons than Assets/Gizmos 0 Answers

How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer

How to store a gameobject in a variable 1 Answer

Multiple Text Objects in a button 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