Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question about DrawDefaultInspector()

Discussion in 'Scripting' started by sjameselvis, Apr 14, 2021.

  1. sjameselvis

    sjameselvis

    Joined:
    Aug 28, 2019
    Posts:
    50
    Hello,

    I have seen someone in a tutorial use:
    Code (CSharp):
    1. if (DrawDefaultInspector())
    2. {
    3.     DoSomething();
    4. }
    He said that when a value in the editor is changed DoSomething() will be called. I can not find much about using DrawDefaultInspector() for this purpose.
    I also can not manage to make it work for me. I've made a custom editor to be able to increment/decrement values with check boxes and I wanted to use
    if (DrawDefaultInspector())
    to do the incrementation/decrementation when a box is checked.
    Screenshot 2021-04-14 155141.png
    When I check/uncheck any boxes nothing happens. Does DrawDefaultInspector() return true only when a variable serialized by the default Inspector is edited? Is so what would be an alternative for the custom editor? At the moment I achieve this by checking each bool at a time if it's true, which doesn't really seem right for me.

    Thanks for your time = )
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    For this use, I don't think you want to care about the return value from DrawDefaultInspector().

    I think you would only care about that bool if you were going to take action based on a change to those properties via the default inspector.

    If I understand your use, your new up/down boxes are the ones taking the action, right?

    If you call DrawDefaultInspector it will draw whatever it would normally draw there. This might be nothing.
     
  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    Code (CSharp):
    1.         EditorGUI.BeginChangeCheck();
    2.  
    3.         //Your field goes here
    4.  
    5.         if (EditorGUI.EndChangeCheck())
    6.         {
    7.             //The value of your field has changed
    8.         }
     
    sjameselvis and Kurt-Dekker like this.
  4. sjameselvis

    sjameselvis

    Joined:
    Aug 28, 2019
    Posts:
    50
    When one of the boxes is checked I set the bool to false and add/subtract 1 from a value. These bools do not have any other uses than to be used in the editor. They act like buttons. (I know that I could use normal EditorGUI buttons for this too)
     
  5. sjameselvis

    sjameselvis

    Joined:
    Aug 28, 2019
    Posts:
    50
    This works. Thanks!
    Can someone tell me when and what the function
    DrawDefaultInspector()
    returns so that I won't have any confusion in the future?
     
    Last edited: Apr 14, 2021
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I wasn't even aware that
    DrawDefaultInspector()
    returned a bool, and the docs say nothing about what determines if it returns true or false.

    The purpose of the method is to, as its name states, just draw the default inspector of a script. Perhaps it returning a bool implies it can fail to draw the default inspector, and you would use the return value to draw a fallback inspector like this?
    Code (CSharp):
    1. if(!DrawDefaultInspector()) {
    2.   //Draw a custom fallback inspector if the default inspector could not be drawn.
    3. }
    I've yet to see something like that occur, but that's my guess.
     
    Last edited: Apr 14, 2021
    Kurt-Dekker likes this.
  7. sjameselvis

    sjameselvis

    Joined:
    Aug 28, 2019
    Posts:
    50
    I've noticed the same thing. That's why I was confused about it's use. But I think your guess is right, it does make sense.
     
  8. silencesquare

    silencesquare

    Joined:
    Nov 20, 2021
    Posts:
    1
    upload_2022-11-11_18-23-4.png

    If you look into the source code, it returns true if the serialized value changes.
     
    angrypenguin likes this.