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 frarees · Oct 09, 2013 at 01:08 PM · typeinternaltypecastingresources.loadall

TypeInferenceRuleAttribute on Resources.LoadAll?

Why does Resources class methods that return arrays of Objects aren't marked with TypeInferenceRuleAttribute as it does for methods that return a single Object (e.g. Resources.Load)?

I just want to cast it like that:

 MyComp[] comps = Resources.LoadAll (path, typeof (MyComp)) as MyComp[];
 MyComp[] comps = (MyComp[])Resources.LoadAll (path, typeof (MyComp));

I've tried using TypeInferenceRule myself, writing a wrapper like:

 [TypeInferenceRule (TypeInferenceRules.ArrayOfTypeReferencedByFirstArgument)]
 static Object[] LoadAll (Type type, string path) {
     return Resources.LoadAll (string, type);
 }

But no success. Still invalid cast operation. For performance reasons, I prefer not to use Linq's Cast.

Comment
Add comment · Show 11
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 ArkaneX · Oct 09, 2013 at 02:59 PM 0
Share

It's hard to say why your wrapper doesn't work. $$anonymous$$aybe it's related to some compiler option, which might be required for TypeInferenceRule attribute to do its magic when compiling?

I'm curious how many resources you're loading, and how often do you do it. In my tests, using LINQ's Cast takes about 20ms for 1000 GameObjects for the first call, and about 5ms for subsequent calls (loading different objects). Times for actual cast operation only, without loading.

Tested on PC, so maybe it looks much worse in mobiles, but anyway, I don't think using Cast should cause any real performance issues.

avatar image Jamora · Oct 09, 2013 at 03:17 PM 0
Share

You're also missing a return type from LoadAll(), but that's probly a mishap while copying or something. $$anonymous$$aybe I just don't understand how TypeInferenceRule works.

avatar image frarees · Oct 10, 2013 at 10:24 AM 0
Share

@Jamora yes it's a typo. I'll fix it. @ArkaneX I don't know either why it doesn't work. Hope someone at Unity can provide more details about TypeInferenceRule implementation. About Cast, it seems really fast after all. However, I'd like to measure the difference of it vs direct casting. AND, love to understand how TypeInferenceRule works. Seems really useful.

avatar image ArkaneX · Oct 10, 2013 at 10:29 AM 0
Share

@frarees - this attribute must be taken into account during compilation somehow. Please note, that there's no such attribute in standard .NET - this is implemented in Boo.Lang library and in UnityEngine library (duplicate of the class and enum from Boo).

Unity uses Boo compiler, so that's why it should be able to use this attribute. $$anonymous$$aybe you can spend some time to investigate if you can control the compilation process. If you have time for this :)

avatar image frarees · Oct 10, 2013 at 10:36 AM 0
Share

@ArkaneX so you say Unity uses Boo to compile C#, JS and Boo code? The attribute is defined in UnityEngineInternal, so it's not that much safe to work with it probably. I'll try to give a look at the Boo.Lang library ;). Thanks for the useful info btw!

Show more comments

1 Reply

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

Answer by frarees · Oct 11, 2013 at 01:20 PM

Resources.LoadAll has an overload for generics, so that I can do:

 Resources.LoadAll <MyComp> (path);
Comment
Add comment · Show 2 · 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 ArkaneX · Oct 11, 2013 at 01:31 PM 0
Share

There is no such method in Unity 4.2.1.

Unless there was some addition in 4.2.2, then this must be an extension method defined in any of your other scripts or referenced assemblies. So probably it works by casting, just like in LINQ...

avatar image frarees · Oct 11, 2013 at 01:33 PM 0
Share

Right, it will be available on 4.3+. Didn't noticed that it's not available for 4.2.

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

18 People are following this question.

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

Related Questions

Custom Editors for any Component 0 Answers

Make compiler treat an object variable as if it was of type X 2 Answers

Identify and Use Unknown Types 1 Answer

Problem with type casting? 1 Answer

how to typecast a method holder in Javascript, like Delegate 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