Unity occasion systems interfaces for Customized GUI behavior

A Unity Event, or the UnityEvent generic abstract course is quite ideal for customizing behavior that is GUI. In this tutorial, I shall explain to you exactly how I’m making use of it for my game Lemonade.

I Happened To Be trying to puzzle out what direction to go next on my game “Lemonade”. And after much idea, and a complete lot of wasting time, I decided to have a crack at making some better looking GUI, starting with how the GUI responds.

However, I didn’t want to use the default unity GUI components, like for example, the button component.

This meant that I would need to create my scripts that are own use Unity’s EventSystems collection, and implement a number of its interfaces.

This is not really complicated, however it’s a thing that individuals may not recognize they might do, particularly you a good starting point for making your own custom GUI behavior scripts if you are a beginner with Unity.

So in this tutorial, my aim is to give. This will give you more control over how your responds that are UI and then add persistence during your game’s interface.

Note: The assets can be found by you and code for this project here https://github.com/JoshuaFMarais/CustomGUIBehavior

  1. What we will do
  2. Create an abstract class called “CustomGUIBehavior”. we will create a custom inspector in it we will implement several of unity’s EventSystem interfaces

    Create a simple CustomButton class which will inherit from CustomGUIBehavior

    And lastly. This script shall allow us to set what function we want to call, from the Unity Editor.

    The GUI behavior script - Unity Event
    The GUI behavior script - Unity EventSo let’s get our hands dirty with Unity Events.

    1. The behavior that is GUI*)So to get started, let’s create our Abstract “CustomGUIBehavior” course.
    The GUI behavior script – Unity Event(we are implementing a bunch of Interfaces,

    like IPointerEnterHandler and IPointerClickHandler which are located in the

    Unity EventSystems Library*)As you can see. Of course, you may not need all of these for your
    project, you need.
    Also as it entirely depends on what note as you won’t be able to attach scripts to GameObjects without it.

    This that we are still inheriting from MonoBehavior, this is necessary part may not be required from

    the interface implementations, especially if you need the same state in
    multiple situations.(* for you; but I think it’s a good

    idea to put each of your “States” in its own function, and then call them) Interface Implementations – Unity Event
    In the image above, may be the Core of what our company is doing: the Interface implementations.
    As you can observe, our company is merely calling our functions within the interfaces that are various and letting the unity EventSystem deal with everything else.

    You should notice that we are calling a “ClickAction()” function in the OnPointerClick() interface. We will declare an function that is abstract “ClickAction()” Next.
    Declaring the function that is abstract – Unity Event

    Here you can see at the top of the script, the abstract “ClickAction”
    we just talked about.

    And finally, there are the properties we will expose to the

    editor so that we are calling the “NormalState” function on

    Custom Button (Script) - Unity Event
    Custom Button (Script) - Unity Eventawake, just to make sure that our button looks the way it should on play.

    As that you have better control over what the graphic/button/image will
    look like in your game.

    Notice well as getting TargetGraphic which will be referenced

    throughout this script.

    2. Our Personalized Button Mono

    The Custom Inspector - Unity Event
    The Custom Inspector - Unity EventOk therefore now our CustomGUIBehavior is completed, we need for our CustomButton script next we just need
    • to make another script which can extend and override its available functions
    • and use its properties.
    Updating The CustomButton Script - Unity Event
    Updating The CustomButton Script - Unity EventThis is all the code. All

    we do for now is inherit from “CustomGUIBehavior” and“Init” that is override and “ClickAction”, we are able to now include whatever we wish and it surely will be called if the individual clicks the switch. The switch will have all the also functionality that customGUIBehavior has which I think is pretty cool! just imagine what stuff that is crazy be performed with this particular as a base!

    Personalized Button (Script) – Unity Event

    1. When you connect this script to a UI item in your scene, you
    2. should see something such as this.
    3. Ok great! This might be fundamentally all you have to make your custom GUI that is own Behavior. But for it, there is one more thing we can do to make this button more useful and practical.

      3 if you are up. The Personalized Inspector(you know you can set what function(s) you want the button to call on click*)If you have ever used the Button component that unity provides by default, then. Here’s a screenshot below for reference:

      The Custom Inspector – Unity Event

      But! We need to code this functionality ourselves since we made our button from scratch. The customized editor script shall help us take care of that. So, first we need to add a couple of things to our customButton that is existing script

      Updating The CustomButton Script

      What this switch is going to do

      play an audio when clicked

      Call a method so we’ve added a few properties to our little script that we select using the custom inspector we are going to create

      • Updating The CustomButton Script – Unity Event
      • Ok. The first 3 are going to be used to serialize the function we want to call when the button is clicked.
      • For our button to know what function to call, we need 3 things:[CustomEditor]The MethodeInfo of the function
      • What class or mono behavior the function belongs to
      Including the UnityEditor class - Unity Event
      Including the UnityEditor class - Unity EventAnd what game Object the mono behavior is attached to

      I have also added an audio source property, which I am assuming is attached to the GameObject this component is attached to.

      There’s also an clip property that is audio. I’ve produced a resource folder, and place an audio clip at run time if it is not set to anything.

      The class GUIEditorHelpers
      The class GUIEditorHelpersand in it named “click_sound”, so it can get it that’s about it for the CustomButton. So with that out of the way, lets get to that custom that is pesky script.

      What We shall make(*)When we are done, this is what the our CustomButton component should look like in the inspector.

      and when using a button in your game. you should only need to attach this component; no need for the default button!

      At the left that is top you can observe i’ve set the prospective item become a canvas, and set the chosen Monobehavior to “TestCanvasMono” that will be one of many elements connected to the canvas. Last but not least, i’ve chosen the function “PrintSomething”, which will be a function in the class that is“TestCanvasMono. All of this means you want using only the inspector that you can select any function. Also it shall be called when the user clicks the button. as long as the function does not have any parameters (You can easily edit the script to ever support what parameters you will need).

      So now we will be making, let get to it!

    4 that you know what. Let’s Code!

    A custom inspector script needs just a couple things to work:

    GetTargetMonoYou must include the “UnityEditor” library.

    The custom inspector class we are making needs to be in a folder named “Editor” or else it won’t work

    You must include the

    GetObjectNames attribute before the class decoration

    And the class must inherit from “Editor”

    GetObjectNames Including the UnityEditor class – Unity Event

    Basically, it should look something like the image above. But in our case, we need a few more things, like the “System.Reflection” library, which will allow us to classes that are scan see what’s in.

    We also get a reference to your target MonoBehavior and shop it in the“mono” that is variable

    I won’t explain every little detail of these scripts, because some of it is c# stuff that is basic. Which means this is where some c# that is basic will be helpful. Otherwise, you can download this project from my git hub or take a look just during the rule in its entirety. The class GUIEditorHelpers

    This is exactly what our editor script should seem like, don’t worry about how exactly this works for the present time, after we make the next, and final class because it should be clear! For now I’ll say that it simply makes use of 3 functions which we need to code. If you look at the image above, you should see that we are using a static class named “GUIEditorHelpers”, This is what we now need to create to make the magic happen!

    GUI editor helpers All of the code you see next, will go inside the static class shown bellow.

    GUI editor helpers

    Just for reference, you will see red lines on some of the images bellow, corresponding to the list(s) below them.



    All this one needs to do is use the class that is editorGUILayout make an Object Field show up within the inspector, and get back the item the consumer selects GetObjectNames

    GetObjectNames This function isn’t that essential. But i needed making it simple to transform all things in a selection, into a myriad of strings. It often when making popups.(* as i/you may need) GetObjectNames

    This function shall show a popup, with a selection of the many monobehaviors connected to the “TargetObj” within the function’s parameters. And invite us to choose what type

    Let’s is wanted by us break it down A.

    We first get all of the monobehaviors on the object using the “GetComponents” function


    If “currentMono” is equal to something; we need to try and find the index of the mono behavior its referencing. However, you may remove and add components at any time; We cannot guarantee where the current Mono will be in the array since you are able to change the order of the components in the inspector, and. That’s why, we utilize a for-loop to endure most of the mono behaviors attached with the “TargetObj” and discover the index by TYPE. If there is nothing discovered, the index will simply stay*)C.(* that is 0( We then get the new “current index” using the Popup function
    Lastly, we simply return the mono behavior.



    This is the function that is last need certainly to make. And it is the good reason we are using the “Reflections” library.(*)a.(*)We use the “GetMethods” function found in the reflection library to get (you guessed it) all the methods inside the “targetMono” passed in to this function. I would recommend looking in the Microsoft docs to get more detail on how this function can be used, but I’m messing that is sure with various arguments will continue to work too ?(*)b.(*)We then want to get most of the names among these functions to show in the*)c that is inspector((*)We search for the index of the method already selected (if there is one). Just like we did in “GetTargetMono()”.(*)d.(*)And lastly, we use the Popup that is almighty Function find the technique we wish, or alter it.(*)Using certainly one of my sprites that are own is what it looks like when in game. Its simple but you can do much more with this method. By the real way i understand the sound FX is terrible! But its the things I had lying around and I’m to sluggish getting a button that is good sound, so anyway…(*)And now we are Done! I hope this made sense that is enough launched your brain to your enormous opportunities available when coming up with your very own actions and customized inspectors.(*)Also Don’t forget that the code can be got by you from my GitHub.https://github.com/JoshuaFMarais/CustomGUIBehavior And I recommend doing so, as its a way that is great discover or make use of as a base on your own creations.(*)And On Twitter:(*)Hi everyone, welcome to the first gameplay video for #LemonadeTheGame and #voxel #destruction system test.(*)Took if you want to learn more about how Lemonade is coming along, follow me ages to get here?. but its finally looking like something?#Indiewatch #madewithunity #unity3d #games #solodev #devlog pic.twitter.com/TXN5QX2hmJ(*)— Jitarie Games (Joshua Marais) (@Jitarie1) 30, 2021(*)Join us!(* january)

Latest posts