I’m in the process of cleaning up my in game GUI, if for no other reason than my onGUI function looks like a God awful mess. I’m interested in using a delegate approach but I wanted to ask if its at all possible to add functions together. In C# if I understand correctly you can do the following.
does this work somehow (the above doesn’t work obivously)? can I do this with a delegate system built on Function in Javascript or is it specific to C# and I’m asking a retarded question. My reasoning is that I’d like to get away from the stupid number of booleans I have controlling the visibly of the various menus in onGUI. Equally though I don’t want my main HUD to disappear when I decide to open a character menu for example.
Just responding in case anyone out there wonders how I got on with this…
Well roughly a week on and I am beginning to see the light on using C# from the beginning. I wanted to develop a delegate GUI system using Javascript but I find that there is no work around for the abscence of adding (+=) and removing methods (-=) to the delegate this.currentGUIMethod. The best thing I was hoping for was being able to write a small script in C# (held in the Plugins folder) that would allow me to use this. However, while I have no doubt that its possible the lack of two way referencing (because the C# script must be compiled first) and my poor scripting skills have made me abandon such an idea.
I even tried to develop a callback method that might allow me to do the same thing purely in JS but this defeated me as well. Oh well at least I learnt something.
You can use delegates in UnityScript too from, but you have to use Delegate.Combine/Remove methods, instead of += and -= which are just overloads that use the below methods for easier usage.
My friend you are a genius, a giant amongst men. You know I’ve wasted so much time trying to figure that one out. There are loads of redundant posts lying around that led me to believe it wasn’t possible. I’m not a programmer by trade (bet you’d guessed that lol) took some Java/SQL/Python at Uni but that was back in 2004 (Bioinformatics MSc). I work in labs mostly, think I need to learn some fundamentals.
No, delegates are not serializable. You would have to write your own EditorScript.
You could of course use a string variable to save the method name and use reflections, but that’s pretty messy and not worth the effort. And it’s better this way, because in most situations you must subscribe AND unsubscribe the delegate, in order to prevent memory leaks.
i.e. if you have Object A which subscribes to Object B and you destroy Object A, it’s memory won’t be collected by the garbage collection if you have forgotten too unsubscribe. Only when Object B gets destroyed to, the Garbage collection will be able to release memory from Object A.
If it’s Object B is a static object for example, it will never be released and you can have a serious memory leak which leads to crashes or bad performance.