- Home /
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.
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.
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.
@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.
@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 :)
@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!
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);
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...
Right, it will be available on 4.3+. Didn't noticed that it's not available for 4.2.