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
2
Question by chearner · Jun 05, 2010 at 08:04 PM · iphonegenerics

generics and c# in iPhone unity

Im using lastest iPhone version...works in regular Unity.

I cut and pasted this script from the wiki. http://www.unifycommunity.com/wiki/index.php?title=Particle_Spiral_Effect

It give me the error CS1002: Expecting ';'

It happens of every line that uses a generic, like ParticleAnimator animator = emitter.transform.GetComponent();

Can anyone explain what is different here or what the problem is when using the iPhone version of Unity? Looks like it may be a limitation of ParticleEmitter and ParticleAnimator types in iPhone or something.

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
10
Best Answer

Answer by Peter G · Jun 05, 2010 at 09:34 PM

Adding to what Mike said, here are some generic methods I compiled together for the iPhone.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class GM { // GM = Generic Methods

 public static T[] FindObjectsOfType<T>() { 
    T[] objects = UnityEngine.Object.FindObjectsOfType(typeof(T)) as T[]; 
    return objects; 
 }

 public static T GetComponent<T>(Component caller) where T : Component
 {
     T t = caller.GetComponent(typeof(T)) as T;
     return t;
 }

 public static T AddComponent<T>(GameObject parent) where T : Component  
 {
     T t = parent.AddComponent(typeof(T)) as T;

     return t;
 }

 public static T[] GetComponentsInChildren<T>(Component parent) where T : Component
 {
     Component[] componentArray = parent.GetComponentsInChildren(typeof(T));

     T[] tArray = System.Array.ConvertAll<Component, T>( componentArray, delegate(Component component)   {return component as T;}); 
     return tArray;
 }

public static T[] GetComponentsInChildren<T>(GameObject parent) where T : Component { Component[] componentArray = parent.GetComponentsInChildren(typeof(T));

     T[] tArray = System.Array.ConvertAll&lt;Component, T&gt;( componentArray, delegate(Component component)   {return component as T;}); 
     return tArray;
 }

}

I couldn't extend the component or gameObject classes with these so you simply have to add the object as a passed value, and the class is GM instead of gameObject or Component. Performance wise, I haven't done any benchmark tests. I don't believe they are any faster than 1.1 casting methods because that is how they work. If anything, they might be a little slower, but any of these functions wouldn't be called very often so it doesn't make much difference.

So this

GetComponent(typeof(MeshRenderer)) as MeshRenderer;

becomes this:

GM.GetComponent<MeshRenderer>(this);

and this:

gameObject.AddComponent("GUITexture");

becomes

GM.AddComponent<GUITexture>(gameObject);
Comment
Add comment · Show 3 · 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 chearner · Jun 06, 2010 at 02:54 AM 0
Share

Thank you, could you add a method above for GetComponentsInChildren...I tried but can't get it to work.

avatar image Peter G · Jun 06, 2010 at 12:10 PM 0
Share

You can thank a couple of forum users for the GetComponentsInChildren. http://forum.unity3d.com/viewtopic.php?t=42686&postdays=0&postorder=asc&highlight=getcomponentsinchildren+error☆t=0

avatar image chearner · Jun 06, 2010 at 07:34 PM 0
Share

I added this to the generic methods for GetComponentsInChildren and seemed to work. public static T[] GetComponentsInChildren(Component caller) where T : Component { T[] t = caller.GetComponentsInChildren(typeof(T)) as T[]; return t; }

avatar image
4
Best Answer

Answer by Mike 3 · Jun 05, 2010 at 08:25 PM

IPhone does support generics, but it hasn't got some of the helper functions as generic yet. GetComponent for example hasn't been updated to allow generics.

I assume this is probably because it's still possible to compile against the older version of .NET, so generics in the API would stop it from compiling

The only solution would be to provide your own generics wrapper for GetComponent, or just use the non generic function

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 ovidiubuligan · Jun 29, 2013 at 02:27 PM 0
Share

is this still the case with unity 4.1 ? (I don't have a mac/Iphone but I am interested)

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

1 Person is following this question.

avatar image

Related Questions

C# generics and getters/setters with Unity iPhone 1 Answer

Are builtin 3D arrays and generics okay on iPhone? 1 Answer

C# from Unity to Unity iPhone 2 Answers

Exporting my Mac game to........ 2 Answers

What shader blending modes don't work on iOS? 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