- Home /
Basic U/S question, how to do 'common' routines...
You will not be surprised to hear this is another very lame Unityscript question......
Say you have a commonly-used routine, an example below.
I just simply .. .copy and paste this at the bottom of all scripts :-)
What is the proper way to handle, in Unityscript, what's the usual idiom, for a routine just like that which is value-neutral and you use in many different classes. What's the deal there?
Further, is there a whole pipeline, perhaps, an editor script of a folder or way to automatically add your everyday routines to all projects (based on, whatever you explain is the answer to the previous paragraph, say...)
Thanks for any advice !
////////////////////////////////////////////////////
function FindUnderHereNamed(name:String):Transform
// purely an example of a commonly-used routine
{
var transforms = transform.GetComponentsInChildren(Transform);
for (var t : Transform in transforms)
if (t.name == name) return t;
return null;
}
////////////////////////////////////////////////////
Summary Solution kindly provided by WDIDI ...
make a class called say Handy
Put functions such as FindUnderHereNamed() in that class
Use them anywhere like Handy.FindUnderHereNamed()
That's the only realistic solution when using U/S.
Just for anyone reading, if you're sensibly using c#, as WD explains you can basically do "categories" (just like in objc), they are called extensions in c# and you use the natty "this Whatever x" syntax in the braces. Full example here ! http://answers.unity3d.com/questions/37651/how-can-i-write-and-use-extension-methods-in-js.html You can put all your "categories" on different classes .. all in the one holder class!
Answer by whydoidoit · Jul 15, 2012 at 02:41 PM
I'd make a utility class and have all of the functions in it be static so you don't have to have an instance to access them. I tend to have plug in classes for a bunch of these things, each of which has some named container.
In C# you often make that utility class static too so you can write extension methods but that doesn't apply to US.
I make all of my utility functions under a folder in plugins - then I export Unity packages for each set. Then it's just a matter (in my case) of copying either my whole Radical folder or importing the packages I want.
W.D. - to be clear, when you say "utility class" in the front there, that's "just a class" right (that has utility to us!) ... there's no actual keyword or quality involved? Thanks!
No not in US - it's just a class. Shouldn't have anything but static members in it really (C# enforces that with the static class definition).
(Thanks again...)
To be sure to be sure ... you mean the utility class would be called say, oh, "Handy"....
And from then on to use FindUnderHereNamed(), you would indeed need to type Handy.FindUnderHereNamed()
(Not unlike using, say, $$anonymous$$athf or any similar.)
It's not possible to sneakily (or otherwise) just type "FindUnderHereNamed()" where you need it (unless you lamely just paste it in "to the bottom" of scripts where you want to use it).
You can't, oh, add a function to all classes in some way, or perhaps have some sort of include file in U/S so that, I guess, effectively you're "pasting it on the bottom".
You could do that if you used C#, but unfortunately the only way you could otherwise do that is have a class that you inherit from $$anonymous$$onoBehaviour that has the functions, then make all of your scripts inherit from that - very ugly to do that in US though.
If there is a big benefit of C# it's this stuff - when you want to be efficient in your coding you can be much more so in C#.
Well basically in C# your static class can define extension methods that give new functions to anything.
So for instance my NetworkView apparently has a method caller Others() which sends the call to other recipients and Server() which sends it to the server. $$anonymous$$y code is more legible and makes sense. When you define an extension method in C# it is a static method in a static class and the first parameter is the thing it extends flagged with a this modifier:
public static void DoSomething(this GameObject go)
{
go.transform.position += go.transform.forward;
}
GameObject.Find("myObject").DoSomething();
No need for all the Handy stuff in that case.
Your answer
Follow this Question
Related Questions
How to get the velocity of an object 1 Answer
UNITY3D: TEXTFILE I/O : Don't write same things 0 Answers
How to make a List Constructor? 1 Answer
Can I convert unityscript to boo? 0 Answers
Dynamically load UnityScript file 1 Answer