Friday, 22 August 2014

Unity 4.6 - Simple Tutorial, new Events System and Raycasters!


Unity 4.6 is (almost) here! Everyone is talking about the new uGUI, the best thing that happened to Unity since the 2D native tools. I already tried the tools and I can say they are simple and awesome, but if you already had experience with nGUI you will see little new stuff in there.

Let's better talk about something else... the new Event System and Raycasters! :)

The new uGUI elements allow to create simple conection between them and other scripts in some sort of visual scripting. Good news are that they didn't limit it to only the new uGUI elements, but they expanded it to allow any unity component to use them, it the Canvas (Graphic Raycaster), 2D (Physics 2D Raycaster) or 3D world (Physics Raycaster).

Events and Raycasters will make the life easier of both programmers and designers.


The new event system is composed of two basic elements:

Raycaster: in this case 2D. Needs to be atached to a proper camera.


EventTrigger: needs to be in a GameObject with a proper collider.

Note 1: In other for this to work just like that you need to have somewhere in your scene the EventSystem component. This will happen automatically if you have any Canvas in your scene. If you don't have it, it will not work.

Note 2: Additionally to the EventSystem there is the Standalone Input for gamepads and Touch Input modules for touch screens, finally! :D

Note 3: Each of the 3 types of Raycaster needs to be atached to the camera or canvas that matches them and render the components that will receive events.


Now let's take a look of why this new feature is amazing:

Posibility of accessing any engine method and custom from your scripts!

You can pretty much call every method with the event, it just have to follow some simple rules:
- Has to be public
- Has to return void
- Has only one parameter of type: int, float, string or Object (Monobehaviour is an Object, so any component or script is valid as a parameter)

That's it! So easy! :D


The best thing is that the possibilities are endless, right now it allows for great control without a line of code, but I am pretty sure that with the possibility of creating custom Events will enchance the possibilities of visual scripting in Unity dramatically.


Here you can see some examples of the new uGUI in movement, the drag and drop example uses some hidden events that you can receive implementing some Interface handlers, but I guess there will be more information about it in the future documentation

Here you have basic uGUI tutorials, get them while they are hot ;)


What are your thoughts about it? Do you think Unity is moving into the right direction? What else would you like to see in the engine? I would love to hear from you :)

PS: if you expected some Northwards Of Roswell update, I am still here and soon there we will be showing you the progress :)

31 comments:

  1. Hi:) Can you give more examples with new event system except click handeling and drag and drop?

    ReplyDelete
    Replies
    1. After doing a little bit of research in that area:
      Event System can be expanded via BaseInputModule class. This is useful for different kind of inputs like console controllers and touch pads (this two are already implemented).
      Let's say you want to control the GUI of your game with movement based controls (like Wii), then you need to implement your specific BaseInputModule and follow the instructions in the docs.

      On the other hand we have the EventTrigger, that for the moment receives 15 kind of events (all related to click and selection handling). I still haven't figured out if you can add extra events to do whatever you have in your mind. Examples could be attaching an EventTrigger to your player character to receive Events from the enemies trying to attack it.

      In the worse case scenario we can still use the same function calls to do whatever we want to do, but we should be extra careful to not mess up with the intended use of that interfaces.

      If I will find out the way of creating custom EventTriggers in the correct (if exists) way I will put the solution in this blog :)

      Delete
  2. Congratulations for a great post!

    ReplyDelete
  3. how can we assign function at runtime to this. Indeed it's very helpuful

    ReplyDelete