Posts

Showing posts with the label github

Unity3D events, delegates and Action

Image
Delegates and events are very powerful features of C#. With them you can write clean and efficient code also in Unity. However, I have found delegates bit confusing to understand. Most of the examples I have seen are not so easy to understand, especially if you are a self-taught coder like me. :) Most of the examples I have found are using both delegate and event definitions. Something like this: public delegate void PlayerHit(int damage);  public event PlayerHit onPlayerHit;  But personally, I like using Action<> type instead. It allows you to replace two lines with one and it will make your C# code simpler. It also removes some confusing buzz around delegate definition. Here's an example: public static event Action<int> PlayerHit; So, at least for me this looks more understandable. Please find below two very simple code snippets from my GitHub repository  to see a real life example using Action in defining delegate. In this example event passe...

Re-usable Unity3D scripts at GitHub!

Image
During last couple of years we have created some generic Unity3D C# scripts. These scripts have been design to be re-used at least to some extent, to ease up new game project creation. Now some of them are available for public at GitHub: https://github.com/jliias/CommonUnityScripts We have selected the ones that we think are most readable and understandable and also well (?) commented. New general purpose scripts will be added when there is something we think is worth publishing. Please have a look and see if there's something useful also for you! -Jussi