Search Unity

Button in Inspector to Call Function in C#

Discussion in 'Scripting' started by grooc, Aug 30, 2011.

  1. grooc

    grooc

    Joined:
    May 23, 2011
    Posts:
    15
    Hello,

    i have searched fpr a long time, but i really don't know what i am missing exactly. I want to have a button in the inspector, that calls a function of that script. I know that things work easier in js, but i have no experience in it. I know that some reference is missing, because i can not use "target" like shown in many js examples about this. unfortunatly, i could not find any example in C#.

    I have the COlliderCreatorEditor Class in a folder Called Editor. The other Script is in a folder called "Scripts"

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. [CustomEditor(typeof(ColliderCreator))]
    6. public class ColliderCreatorEditor : Editor {
    7.    
    8.     // some declaration missing??
    9.    
    10.     override public void  OnInspectorGUI () {
    11.         if(GUILayout.Button("End")) {
    12.         target.endTrigger(); // how do i call this?
    13.          }
    14.         DrawDefaultInspector();
    15.     }
    16. }

    The Class to be called
    Code (csharp):
    1. public class ColliderCreator : MonoBehaviour {
    2.  
    3.     public bool start = false;
    4.     ....
    5.         public void endTrigger()
    Can you help me?
     
  2. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    target is of type object
    http://unity3d.com/support/documentation/ScriptReference/Editor-target.html

    Your code has no idea what it is until you explicitly tell it what it is.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. [CustomEditor(typeof(ColliderCreator))]
    7. public class ColliderCreatorEditor : Editor {
    8.    
    9.     // some declaration missing??
    10.    
    11.     override public void  OnInspectorGUI () {
    12.         ColliderCreater colliderCreator = (ColliderCreator)target;
    13.         if(GUILayout.Button("End")) {
    14.         colliderCreator.endTrigger(); // how do i call this?
    15.          }
    16.         DrawDefaultInspector();
    17.     }
    18. }
    19.  
    While my code works, the following link explains a much cleaner design for creating editors and accessing Target.
    http://altdevblogaday.com/2011/08/22/extending-the-unity3d-editor/
     
    Eristen likes this.
  3. grooc

    grooc

    Joined:
    May 23, 2011
    Posts:
    15
    thanks that works!!!
     
  4. Daydreams_5

    Daydreams_5

    Joined:
    Sep 1, 2020
    Posts:
    4
  5. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    This design works fine, especially if you limit yourself to buttons and simple things. But when you start to use inputs (like IntField and so on) you may want to be able to ctrl-z.
    I'm not 100% sure it's what was shared by Ntero in 2011 since things have changed *quite* a bit since then, but the best example I could find in the unity manual is the one for PropertyField.
    https://docs.unity3d.com/ScriptReference/EditorGUILayout.PropertyField.html

    It's slightly more work, but the Serialized Property approach allows you to create custom editors for custom classes using Property Drawer, which is, to me, one of the coolest things of Unity.
    https://docs.unity3d.com/ScriptReference/PropertyDrawer.html

    If you want a blog post explaining this FAR better than I just did, here is a fresh link (let's hope this one lasts)
    https://www.raywenderlich.com/939-extend-the-unity3d-editor



    EDIT: I managed to get a snapchot of the broken link using the WayBackMachine:
    https://web.archive.org/web/2012010...om:80/2011/08/22/extending-the-unity3d-editor
    It's far less extended than what I expected, but at least you have that resource.
     
    Daydreams_5 likes this.
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    Yes, I was about to suggest trying the WayBackMachine as well -.- However keep in mind that articles from 2011 are most likely completely outdated. They don't have to and might apply pretty much the same now, just take them with a grain of salt. A lot things have changed.

    Even the newer blog post you mentioned is already "a bit old". At that time UIElements weren't a thing yet. Though I still prefer plain IMGUI for simple things. If you need help with the IMGUI, have a look at my IMGUI crash course. This is also a bit outdated but the basics still hold true. Maybe I'll add a section about serializedobjects / serializedproperties some day :) The big advantage (and curse) of serialized properties is the heavy abstraction of the actual data. This allows easy multi object editing and some sort of modularized editor frameworks. Though some things are getting tricky with them since you don't have direct access to the actual C# classes and types, just the serialized data.

    About being able to CTRL+Z your changes, Have a look at the Undo class.

    Also keep in mind that there is no such thing as "clean code" or "clean design". It's all about readability, maintainability and how easy it is to understand the code. Though since different people have different mindsets and paradigms there's no "one-size-fits-it-all solution". So of course you should learn from others, but don't just copy everything one to one. Nothing is ever perfect.
     
    diXime likes this.
  7. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Yeah, everything you say is true, that's why I could only wildly guess what was it all about. I'll be honest about the ctrl-z thing, It's not the main purpose and I may have oversimplified things. Plus, it's really not something that bugs me too much, usually operations in inspector doesn't need that much to be undone, unless I risk to destroy an object and its asset (with DestroyImmediate(obj, true)).
    Well, thanks for completing my answer with sources.
     
    Bunny83 likes this.