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 Nonakesh · Feb 18, 2012 at 04:05 PM · arraycustom class

Use custom class like array

Is it possible to use a custom class similar to an array, like accessing the children of transform, or the AnimationStates in a Animation object (which actually is really strange... you can access it by using a string?!)?

Here are examples of what I mean:

 for (var child : Transform in transform) {
     child.position += Vector3.up * 10.0;
 }

or

 for (var state : AnimationState in animation) {
     state.speed = 0.5;
 }
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

2 Replies

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

Answer by Bunny83 · Feb 19, 2012 at 01:49 PM

Transform implements a common interface called IEnumerable. This interface forces the class to provide a GetEnumerator() function which should return an object that implements the IEnumerator interface. With this enumerator you can "enumerate" through "whatever" the class want.

For example, this is how the Transform IEnumerator looks like:

// C# public sealed class Transform : Component, IEnumerable { private sealed class Enumerator : IEnumerator { private Transform outer; private int currentIndex = -1; public object Current { get { return this.outer.GetChild(this.currentIndex); } } internal Enumerator(Transform outer) { this.outer = outer; } public bool MoveNext() { int childCount = this.outer.childCount; return ++this.currentIndex < childCount; } public void Reset() { this.currentIndex = -1; } }

 public IEnumerator GetEnumerator()
 {
     return new Transform.Enumerator(this);
 }

 // Rest of Transform

}

Now if you use a transform in a foreach-loop the compiler sees that it implements the IEnumerable interface and use GetEnumerator() to get an enumerator-object which it can use to loop through the collection.

This:

// C#
foreach (Transform child in transform)
{
    // ...
}

performs actually something like this:

IEnumerator tmpEnumerator = transform.GetEnumerator();
while (tmpEnumerator.MoveNext())
{
    Transform child = (Transform)tmpEnumerator.Current;
    // ...
}

edit

To use a class "like an array" you can use an Indexer.

Indexers and additional interfaces are not supported by UnityScript(Unity's Javascript) at the moment. The only way would be to use C#

Comment
Add comment · Show 1 · 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 Nonakesh · Feb 19, 2012 at 11:59 PM 0
Share

Thank you! At least I know how it would work.... It's a shame, that this is not possible in UnityScript, but if I really need this feature I could still make a class in C# and use it in UnityScript!

avatar image
0

Answer by Berenger · Feb 18, 2012 at 09:58 PM

That for loop can be used with any arrays, no matter what is in there. I think AnimationStates is a hashtable, which mean you can access elements with a key, a string in that case.

Comment
Add comment · Show 4 · 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 Nonakesh · Feb 18, 2012 at 10:26 PM 0
Share

But animation and transform aren't arrays... $$anonymous$$y question is how I can make a class and access a variable like if it were a array. Is this even possible in JavaScript?

avatar image Berenger · Feb 18, 2012 at 11:45 PM 0
Share

Animation (not AnimationState, my bad) and Transform are arrays-like actually (it's called a collection) and your code already work, test it.

avatar image DaveA · Feb 19, 2012 at 04:34 AM 0
Share

I was about to say how Transform contains a collection of Transforms, but itself is not an array. But I see your point - it's being accessed in that 'for' loop as though it were. So we would normally think it would be

 for (t : Transform in trans) // where trans is declared as Transform[], but it's not!

$$anonymous$$aybe there's a way to have a 'get' method that returns []

avatar image Nonakesh · Feb 19, 2012 at 01:09 PM 0
Share

Is there any way to reproduce this with a custom class? For example if I have the custom class "Army" with the variable "soldiers : Soldier[]", it would be much cleaner to write: var army : Army;

 for(var soldier : Soldier in army) {
     //do stuff
 }

Of course I could write army.soldiers, but I am just interested if I could do the same thing as the Unity devs!

Btw... I know that the code above is working.. I wrote them as examples for what I want to recreate.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Set Level inquiry/data from the inspector 1 Answer

Enumeration over grid states 2 Answers

DateTime, get all dates for the current week? 1 Answer

[solved] How to edit an array of custom classes in the Inspector 1 Answer

Custom Window array inside custom class 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