Skip to content

Conversation

@shayded-exe
Copy link

Now uses the non-generic version of GetComponents then casts back to the
proper type.

Now uses the non-generic version of GetComponents then casts back to the
proper type.
@shayded-exe
Copy link
Author

Actually this doesn't quite work right. The GetComponents are returning null. Will update with a real fix.

@shayded-exe
Copy link
Author

Aaaand there we go. The cast is now done per-element using Array.ConvertAll. The old method would just cast the whole thing.

@shayded-exe
Copy link
Author

I've extracted out the extra code around GetComponents to an extension method.

I wont put it in the pull request, but you can use it if you wish :)

public static T[] GetInterfaceComponents<T>(this Component parent) where T : class
{
    return System.Array.ConvertAll(parent.GetComponents(typeof(T)), c => c as T);
}

@prime31
Copy link
Owner

prime31 commented Aug 31, 2015

I am not sure I am following this. GetComponents works fine with interfaces as of Unity 4.6

From the release notes:
Added smart-allocating GetComponents method which fetches components of type T and grows the list as needed. Non-generic version that supports interfaces has also been added.

@shayded-exe
Copy link
Author

Strange, your normal code didn't compile for me because of GetComponents, so that's why I did this. I'm using 4.6.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants