Button in Inspector to Call Function in C#

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”

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(ColliderCreator))] 
public class ColliderCreatorEditor : Editor {
	
    // some declaration missing??
	
    override public void  OnInspectorGUI () {
        if(GUILayout.Button("End")) {
	    target.endTrigger(); // how do i call this?
         }
        DrawDefaultInspector();
    }
}

The Class to be called

public class ColliderCreator : MonoBehaviour {

	public bool start = false;
	....
        public void endTrigger()

Can you help me?

target is of type object

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

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(ColliderCreator))] 
public class ColliderCreatorEditor : Editor {
	
    // some declaration missing??
	
    override public void  OnInspectorGUI () {
        ColliderCreater colliderCreator = (ColliderCreator)target;
        if(GUILayout.Button("End")) {
	    colliderCreator.endTrigger(); // how do i call this?
         }
        DrawDefaultInspector();
    }
}

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/

2 Likes

thanks that works!!!

The blog link is broken, I’m interested in the “clean design” though - sorry for the necrobump

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/20120106175115/http://altdevblogaday.com:80/2011/08/22/extending-the-unity3d-editor
It’s far less extended than what I expected, but at least you have that resource.

1 Like

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 :slight_smile: 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.

1 Like

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.

1 Like