Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fattie · Jul 15, 2012 at 01:26 PM · javascript

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 ...

  1. make a class called say Handy

  2. Put functions such as FindUnderHereNamed() in that class

  3. 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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jul 15, 2012 at 05:17 PM 0
Share

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!

avatar image whydoidoit · Jul 15, 2012 at 05:20 PM 0
Share

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).

avatar image Fattie · Jul 15, 2012 at 05:34 PM 0
Share

(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".

avatar image whydoidoit · Jul 15, 2012 at 05:50 PM 0
Share

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#.

avatar image whydoidoit · Jul 15, 2012 at 06:03 PM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges