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 /
avatar image
0
Question by Chocolade · Aug 11, 2017 at 08:55 PM · c#scripting problemscript.scripteditor

Why i don't see the button in the Inspector on debug mode ?

I have this button script:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(LevelMap))]
 public class CustomButton : Editor
 {
     public override void OnInspectorGUI()
     {
         LevelMap Generate = (LevelMap)target;
         if (GUILayout.Button("Generate Map"))
         {
             Generate.GenerateNew();
         }
     }
 }
 

And a script that is attached to a empty GameObject the Level Map:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 [ExecuteInEditMode]
 public class LevelMap : MonoBehaviour
 {
     public GameObject Node;
     public Vector3 nodeSize;
     public int Rows;
     public int Columns;
     public int mapWidth;
     public int mapHeight;
     public float Spacing = 2.0f;
     public float spawnSpeed = 0;
 
     private void Start()
     {
         Generate();
     }
 
     private void Generate()
     {
         for (int x = 0; x < mapWidth; x++)
         {
             for (int z = 0; z < mapHeight; z++)
             {
                 GameObject block = Instantiate(Node, Vector3.zero, Node.transform.rotation) as GameObject;
                 block.transform.parent = transform;
                 block.transform.localPosition = new Vector3(x, 0, z);
             }
         }
     }  
     public void GenerateNew()
     {
         Generate();
     }
 }
 

One of the problems is that in the Inspector if the mode is normal i see the Generate Button. But i don't see the public variables of Level Map. If i change the mode to Debug then i will see the Level Map public variables but i will not see the button.

How can i make that i will see the button and the variables at the same mode ? (normal or debug)

Another problem is how can i make that if generated a new map now if i will change one or more of the variables values in the editor it will effect the GenerateMap in real time ? For example if i change the Spacing variable value if it was for example 2 and i change it to 4 then generate a new map but with spacing 4.

So the button is like for reset to generate whole new map but changing variables values will effect the current map only.

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
1
Best Answer

Answer by FlaSh-G · Aug 11, 2017 at 10:54 PM

The inspector's debug mode's express purpose is to not display any regular editor GUI, but rather display as many of the components' fields as possible. As the name suggests, the debug mode is only for debugging components, not for regular use.

To let your custom inspector gui display the default UI as if there was no editor class, you can use DrawDefaultInspector like this:

 public override void OnInspectorGUI()
 {
     LevelMap Generate = (LevelMap)target;
     if (GUILayout.Button("Generate Map"))
     {
         Generate.GenerateNew();
     }
     DrawDefaultInspector();
 }
Comment
Add comment · Show 4 · 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 Bunny83 · Aug 12, 2017 at 04:14 AM 0
Share

That's true. However the whole approach seem to be just wrong. First of all the button in the inspector just triggers a runtime methods of the component. This can be implemented simply as context menu item. So it doesn't require any editor script in the first place.

Next is that almost 90% when i see the use of "ExecuteInEdit$$anonymous$$ode" it's actually used in a wrong / problematic way. That attribute is ment to provide "runtime behaviour" at edit time. Though this should only be used to provide the user with a visual representation. So it makes sense to use it for simulating particle systems or a water wave simulation to show how they will look at runtime. Code that actually edits / modifies the scene shouldn't be executed by such a script.

When you use ExecuteInEdit$$anonymous$$ode, Start will be called each time the scene is loaded during edit time. Since you don't clean up your old objects you will create additional objects each time Start is called during edit mode. Start is also called after an assembly reload which happens each time a script got changed outside of Unity and Unity recompiles it's project assemblies..

You shouldn't use ExecuteInEdit$$anonymous$$ode at all in this case. Just call your Generate method once from the Reset callback. Reset is called only once when the object is created (or when you select "Reset" from the context menu). In addition you can simply add a Context$$anonymous$$enu to allow the user to manually trigger the creation. Also the code probably should remove the old objects (if there are any) before creating new objects.

avatar image Chocolade Bunny83 · Aug 12, 2017 at 05:03 AM 0
Share

I tried in the bottom of the script $$anonymous$$ap Level to add:

 [Context$$anonymous$$enu("Generate $$anonymous$$ap")]
     void GenerateNew()
     {
         for (int i = 0; i < objects.Count; i++)
         {
             DestroyImmediate(objects[i]);
         }
         Generate();
     }

But i don't see it in the inspector. Also when running the game i don't see it.

I also remove the the line: [ExecuteInEdit$$anonymous$$ode]

Then i tried to add to the bottom:

 [UnityEditor.$$anonymous$$enuItem("GameObject/UI/Text Area", false, 10)]
     public static void GenerateNew()
     {
         for (int i = 0; i < objects.Count; i++)
         {
             DestroyImmediate(objects[i]);
         }
         Generate();
     }

But then i need to change everything to be static including the transform it self.

So i'm trying to make all the suggestions you gave me but so far not working. Could you show me please how to do the whole suggestions ?

avatar image FlaSh-G Bunny83 · Aug 12, 2017 at 09:31 AM 0
Share

Valid points by @Bunny83 here. I totally ignored all this in the posted script.

The proposed context menu entry is supposed to completely replace the inspector. Should look something like this: $$anonymous$$enuItem

This is the only point I'd disagree with @Bunny83 on. If you want to call a non-static method on a specific object, drawing an extra button into the inspector is totally fine. If that method should also be called on runtime, it's also totally fine to call that method from the editor script ins$$anonymous$$d of re-implementing it for the editor.

Even if it's only supposed to be called in editor, it might be valid to implement it in the runtime class for the sake of loose coupling.

So, depending on your situation, stay with the custom inspector code, it's fine. The ExecuteInEdit$$anonymous$$ode however should definitely go.

avatar image Bunny83 FlaSh-G · Aug 13, 2017 at 12:06 PM 0
Share

I didn't say that implementing a custom inspector to add such a button is wrong, just unnecessary. I said:

This can be implemented simply as context menu item. So it doesn't
require any editor script in the first place.

ps: The context menu item adds an item to the context menu of the component, not to the window menu:

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

384 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How can i check and fire an event when the user look at specific object ? 0 Answers

How can i List objects by name but also in small text or big text or any kind ? 1 Answer

Why the speed parameter in Animator change the animation speed only for door open but when the door is closing the speed still very fast ? 1 Answer

How can i make an entrance and exit in this maze ? 1 Answer

How can I animate linerenderer lines over time ? 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