domingo, 22 de mayo de 2016

Unity Events


Before proceeding with custom classes and practical applications in Unity, I wanted to show Unity Events, these are the events we see in the UI interface and we can use in our own scripts, although they are very useful and easy to use, for some reason they are not well documented, to learn them I had to analyze the source code of the UI. Let's proceed to make our own event:

using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class TestUnityEvent : MonoBehaviour {

    public UnityEvent OnStart = new UnityEvent();

 // Use this for initialization
 void Start () {
        OnStart.Invoke();
    }
 
 // Update is called once per frame
 void Update () {
 
 }
}


First, we add using UnityEngine.Events; at the top of our code, then we create a variable of type UnityEvent and give it a name, in this case my I'll name it "OnStart" because I will execute it in the method Start. Then, to call the event, we use the name of the event plus ". Invoke ()" so the event is executed, as you can see, it is very easy, when we add this script to a GameObject it looks like this:


In this way, we can use these useful events that are very powerful and dynamic, they let us run other scripts methods visually, modify objects in the scene, etc. If we need to send a value with the event, such as the InputField which sends us a string each time the user updates the value or when he presses enter:


To make this kind of event, we just need to create a class that inherits from UnityEvent and between the symbols '< >' we add the value we want to return, here the code example for string:

[System.Serializable]
public class UnityEventString : UnityEngine.Events.UnityEvent<string>
{

}

The class does not need to have anything special inside, just that inherits from UnityEngine.Events.UnityEvent and declare the types you want to return besides using the attribute System.Serializable as I explained in my previous post, it is used to let Unity display the custom class in the Unity inspector (actually, it does more than that, but in this case we use it for that). To use the event, you must declare a variable of your custom class type and invoke the method ".Invoke (StringValue)"



using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class TestUnityEvent : MonoBehaviour {

    public UnityEvent OnStart = new UnityEvent();
    public UnityEventString OnStart2 = new UnityEventString();

    // Use this for initialization
    void Start () {
        OnStart.Invoke();
        OnStart2.Invoke("Hola mundo");
    }
 
 // Update is called once per frame
 void Update () {
 
 }
}

[System.Serializable]
public class UnityEventString : UnityEngine.Events.UnityEvent<string>
{

}



In this simple way we can use these powerful and dynamic events in our games, any questions or suggestions leave it in the comments !.

Custom Classes

Versión en Español

Analyzing the frequently asked questions in the forums and because after we learn to use arrays and lists, we want to solve all our problems with them, it becomes difficult and messy to manage indexes and our code is not as clean as we would like and bidimentional arrays are even more complicated. I want to teach you an alternative, custom classes, with them, we can handle many values and can give them many uses, in this case I want to teach them with a very basic example, the main idea is understand them first.

What I mean by custom class?

A custom class is very common in the world of programming, is any class that doesn't come with default language, but for people who are not completely familiar with object-oriented languages or as I, that started using Unity before learning to program completely, it is a bit complicated, for example, I thought that all classes should inherit from MonoBehaviour. A custom class is as easy as this:
public class CustomClass {
    public int Integer1;
    public string String1;
}
However, by not inherit from MonoBehaviour we lose some advantages such as the possibility of being added as component to GameObjects, methods that are automatically called, such as Start, Update, Awake, etc., among many others, but also we have other advantages such as the ability to serialize (I'll explain this in another post later), also it makes them much more optimized when having a large class hierarchy as is the case of a complex inventory and other advantages that I will cover in following entries.

Display values of custom classes in the Unity inspector

Custom classes can't be added as components to GameObject, so we don't get to see them in the inspector, even if we make a variable in a MonoBehaviour with the type of our class they won't be displayed, let's see an example:
using UnityEngine;
using System.Collections;

public class CustomClassesTutorial : MonoBehaviour { 
    public Item TestItem;
}

public class Item {
    public int Id;
    public int price;
    public Sprite Image;
}
By adding this script to a GameObject in Unity we see the following:
As shown in the picture, Item type variable is not visible in the inspector, this can be fixed only by adding the attribute [System.Serializable] before declaring class or adding up "using System;" and then only using [Serializable]:
using UnityEngine;
using System.Collections;

public class CustomClassesTutorial : MonoBehaviour { 
    public Item TestItem;
}

[System.Serializable]
public class Item {
    public int Id;
    public int price;
    public Sprite Image;
}
And this results in :
As you can see now, the inspector displays the attributes of the Item class. Well, now that we know what is a custom class, how to create them and display them in the inspector, in my next post we will go through an example where we can truly appreciate their uses.