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 /
  • Help Room /
avatar image
1
Question by unityAston · Feb 20, 2016 at 06:29 AM · c#editoreditor-scriptingcustom editorgizmo

DrawGizmo - Accessing Editor script in DrawGizmo

I have been making a custom editor for a hexagon grid, and also a custom gizmo for that grid using the [DrawGizmo] attribute.

alt text

It currently works basically like this:

  • I've got the HexGrid class, that contains all the functionality for storing and communicating with the hex grid:

       public class HexGrid : MonoBehaviour
         {
             public float HexRadius;
         
             public Color GizmoColor;
             public int GizmoDrawRange;
         
             // Rest of class...
         }
    
    
  • And then I've got the HexGridEditor class that also contains the HexGridGizmo method, that uses the [DrawGizmo] attribute:

       [CustomEditor(typeof(HexGrid))]
         public class HexGridEditor : Editor
         {
    
         // Variables, references, functions...
         
             [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy | GizmoType.Pickable)]
             static void HexGridGizmo(HexGrid hexGrid, GizmoType gizmoType)
             {
                 Color color = hexGrid.GizmoColor;
                 int DrawRange = hexGrid.GizmoDrawRange;
    
                 // Rest of gizmo functionality...
             }
         }
    
    

This works fine, but it's not how I want it organized. I ideally want all editor and gizmo functionality to be contained in the HexGridEditor class, and have the HexGrid class only contain runtime functionality. So to be more precise, I want the GizmoColor and GizmoDrawRange fields to be contained in the editor class, but I can't get that to work.

  • If I just put the GizmoColor and GizmoDrawRange fields in the HexGridEditor class I can't reference them from the HexGridGizmo function, because it's a static function. And it has to be static according to the script reference.

      [CustomEditor(typeof(HexGrid))]
          public class HexGridEditor : Editor
          {
     
          public Color GizmoColor;
          public int GizmoDrawRange;
          
              [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy | GizmoType.Pickable)]
              static void HexGridGizmo(HexGrid hexGrid, GizmoType gizmoType)
              {
                  Color color = GizmoColor;
                  int DrawRange = GizmoDrawRange;
     
                  // DOES NOT WORK:
                  // Can't reference GizmoColor and GizmoDrawRange from static method.
              }
          }
    
    
  • The renderer function takes as its first argument the object for which the gizmo is being drawn, and as we saw in my current setup (first two code snippets) that lets me reference that objects fields, so what if I use the HexGridEditor class as that argument instead of the HexGrid class? That doesn't work either and throws a strange (and seemingly undocumented) error: classID != -1.

     [CustomEditor(typeof(HexGrid))]
           public class HexGridEditor : Editor
           {
      
           public Color GizmoColor;
           public int GizmoDrawRange;
           
               [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.InSelectionHierarchy | GizmoType.Pickable)]
               static void HexGridGizmo(HexGridEditor hexGridEditor, GizmoType gizmoType)
               {
                   Color color = HexGridEditor.GizmoColor;
                   int DrawRange = HexGridEditor.GizmoDrawRange;
      
                   // THROWS ERROR (in Unity): classID != -1.
               }
           }
    
    

So how do I solve this in a tidy way? Is there some way to pass the HexGridEditor class into the gizmo function? Some other way, where editor and runtime functionality is neatly separated?

Much thanks in advance!

untitled1.png (300.5 kB)
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
0
Best Answer

Answer by unityAston · Feb 25, 2016 at 02:43 AM

I got help via reddit from /user/Sarkahn on /r/Unity3D:

Whew, that's a pretty huge wall of text for such a simple problem! You're really overcomplicating it - passing the Editor to the DrawGizmos doesn't really make any sense.

If I'm understanding this right you have two editor-specific values ( GizmoColor and DrawRange) that you want to be moved out of the runtime class and into the editor class.

If you want editor-specific data to live only in your editor class just stick them in your editor class. You say you want to use them for DrawGizmos in your editor class and DrawGizmos is static, so any variables it uses need to be static. So make your variables static.

If you need your variables to instance specific ( so different hex grids have different colors and draw ranges) and you absolutely must keep them out of your hex-grid class then the simplest solution is EditorPrefs.

TLDR: Make your drawRange and gizmoColor variables static.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Property Drawer ArgumentException 1 Answer

Good practices for storing readonly data 0 Answers

How to properly import mesh C# Editor 1 Answer

Why am I getting this strange behaviour? 1 Answer

Custom Editors - Array of Custom Classes 2 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