XDracam / unity-corelibrary

Collection of classes and extension methods that make life with Unity3D more comfortable
MIT License
15 stars 3 forks source link

Add a method which recieves a list of components and returns a GameObject with these components attached #34

Open n3roxis opened 5 years ago

n3roxis commented 5 years ago

Something like: var obj = CreateGameObject(RectTransform, LineRenderer, Button);

XDracam commented 5 years ago

C# has no 'variadic templates' like C++ does. This request would result in reflection-heavy code, like:

var obj = MakeGameObject(typeof(RectTransform), typeof(LineRenderer), typeof(Button));

compared to the old style, this looks rather messy:

var obj = new GameObject();
obj.AddComponent<RectTransform>();
obj.AddComponent<LineRenderer>();
obj.AddComponent<Button>();

Sure, it's more bloated, but also more readable.

I think we don't need this. Better solutions include:

XDracam commented 5 years ago

A discussion with @Eregerog and @n3roxis revealed following idea:

Generate code for a number of generic (A, B, C, ...) AddComponents<A, B, C ...>() methods, which work exactly like Unitys AddComponent.

XDracam commented 5 years ago

Marked for C#7 only because of the use of tuples that make this convenient at all.