Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 greggman · Aug 05, 2015 at 04:53 PM · editor-scriptingeditorwindowpropertyfield

How to get PropertyDrawers in a custom EditorWindow

I'm trying to get PropertyDrawer support in a custom EditorWindow. So, first I make a serializable class

 using UnityEngine;
 
 [System.Serializable]
 public class FooBar
 {
     [TextArea(5, 5)]
     public string someText = "Hello";
 
     [Range(1.0f, 2.0f)]
     public float someRange = 0.25f;
 
     public int someInt;
 }


It's got PropertyDrawer attributes on someText and someRange. If add that to a GameObject I see it works in the inspector

alt text

So then I make a custom editor and draw an instance of the same class using EditorGUILayout.PropertyField

 using System;
 using UnityEditor;
 using UnityEngine;
 
 public class FooWindow : EditorWindow {
     public FooBarHolder foobarHolder;
     private SerializedObject m_serializedObject;
     private SerializedProperty m_serializedProperty ;
     private Vector2 m_scrollPos = new Vector2(0, 0);
 
     void Init()
     {
         if (foobarHolder == null) {
             var tmp = Resources.FindObjectsOfTypeAll<FooBarHolder>();
             if (tmp.Length > 0)
             {
                 foobarHolder = tmp[0];
             }
             else
             {
                 foobarHolder = ScriptableObject.CreateInstance<FooBarHolder>();
             }
         }
 
         if (m_serializedObject == null) {
             m_serializedObject = new UnityEditor.SerializedObject(foobarHolder);
             m_serializedProperty  = m_serializedObject.FindProperty("foobar");
         }
     }
 
     void OnGUI()
     {
         // This is here because as we edit this source, when unity recompiles, code below
         // will fail because everything will have been seralized, purged, an restored.
         Init();
         m_serializedObject.Update();
         EditorGUILayout.BeginVertical();
         m_scrollPos = EditorGUILayout.BeginScrollView(m_scrollPos, GUILayout.Width(EditorGUIUtility.currentViewWidth), GUILayout.Height (position.height));
         EditorGUILayout.PropertyField(m_serializedProperty , true);
         m_serializedObject.ApplyModifiedProperties();
         EditorGUILayout.EndVertical();
         EditorGUILayout.EndScrollView();
     }
 
     [MenuItem ("Window/FooWindow")]
     static void ShowIt()
     {
         FooWindow w = EditorWindow.GetWindow<FooWindow>();
         w.Init();
     }
 }
 
 // The only reason this exists is becaue I don't know how to call m_serilziedObject.FindProperty
 // without a property name. In other words, when I want a SeralizedProperty for the object itself,
 // not one of its children. But putting FooBar in a FooBarHolder I can call FindProperty and
 // pass in "foobar"
 public class FooBarHolder : ScriptableObject{
     public FooBar foobar;
 
     void OnEnable() {
         hideFlags = HideFlags.DontSave;
         if (foobar == null) {
             foobar = new FooBar();
         }
     }
 }

All the fields are shown but none of the attributes for PropertyDrawers are applied.

alt text

Any idea how to get PropertyDrawers to work in a custom EditorWindow?

screen-shot-2015-08-05-at-63944-pm.png (21.5 kB)
screen-shot-2015-08-05-at-64806-pm.png (21.1 kB)
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 Positive7 · Aug 05, 2015 at 09:02 PM 0
Share

Apply$$anonymous$$odifiedProperties(); http://docs.unity3d.com/ScriptReference/SerializedObject.Apply$$anonymous$$odifiedProperties.html

avatar image greggman · Aug 05, 2015 at 09:14 PM 0
Share

I dont' see how that's supposed to help. First off I'm calling it at line 40 above. Second off the docs have nothing to do with propertydrawers getting used. Third, they aren't getting used even though I'm calling SerializedObject.Apply$$anonymous$$odifiedProperties

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

25 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

Related Questions

Editing a List Propertyfield in Editor Extension overwrites previously saved entries. How to fix? 1 Answer

Function Reference in custom EditorWindow (no Inspector) 0 Answers

Trying to do custom editor using propertyfield of a List<> but want to stuff each item with a dropdown picklist from another list. 1 Answer

Reordable List in a window 1 Answer

Editor GUI Foldout header style customization 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