unity3d - Template for component construction in Unity with C# -


programming in c# in unity requires tremendous amounts of boiler-plate. partly due c#, due design decisions in engine itself.

the fact cannot construct monobehaviours myself means need write code following enforce correct initialization:

using system; using system.collections.generic; using unityengine;   public class myclass : monobehaviour {     private int a;     private float b;     private string c;     private list<int> lst;      public static myclass construct (int a, float b, string c,list<int> lst)     {         //boiler-plate stuff         var go = new gameobject ();         var mc = go.addcomponent<myclass> ();          /*          * part fugly. static method setting          * fields on object. responsibility          * initialized after construction should          * lie on object.           * forget set fields          * outside.          */         mc.a = a;         mc.b = b;         mc.c = c;         mc.lst = lst;          return mc;     }      //same above, time without constructing new gameobject.     public static myclass addto (gameobject go,int a, float b, string c,list<int> lst)     {         var mc = go.addcomponent<myclass> ();          mc.a = a;         mc.b = b;         mc.c = c;         mc.lst = lst;          return mc;     }  } 

is there way create code-snippet in xamarin studio reduce boiler-plate, or other fancy tricks automate process of writing these functions?

my unfinished attempt looks xamarin editor-templates:

public static $classname$ construct(){ } 

where $classname$ calls function name of current class. don't think it's possible list identifiers of class in function header.

just pre-empt people: know possible create classes don't derive monobehaviour, let's assume class needs functionality of gameobject.

i guess looking factory pattern.

depending on context, have 3 suggestions:

1 prefabs

avoid boiler plate code, that's why common use prefabs , instantiate them. it's prototype pattern. declare set of serialized properties , instantiate clone of current prototype (that's trying code).

2 initialize in awake

if objects initialized defined set of value, initialize them in awake rather in constructors. if they're not , every component should in case provide relevant constructors overloads(as init method, since cannot rely on constructor), or wrapped inside factory.

3 use lambda

if still prefer, or reasons need to, stick procedural object creation, can avoid declare methods every class , pass responsible initializing object. like:

public class test : monobehaviour {     public int foobar; }  public static t createandattach<t>(action<t> init)      t : monobehaviour {     gameobject go = new gameobject();     t t = go.addcomponent<t>();      init(t);     return t; }  //usage createandattach<test>(t => { t.foobar = 10; });    

the fact cannot construct monobehaviours myself means need write code following enforce correct initialization

the fact monobehaviours c# wrappers component resides in c++ side of engine, , it's the engine owns life time. de facto resources ask , possibly release (like other resources such textures, meshes,..).


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -