- Home /
pre generated static Object in C#
i need static property, which will be more or less complicated object, which propertis might be array, float, int and string
in flash/as3 i would do it by json:
static Object o = {property:1, property2:{subproperty:"text", subproperty:[{subP:1, subP:2}]}};
is there way in c# ?
Do you mean it can contain any properties? Isn't it possible to define a class with all possible properties and fill only part of them when initializing? If it's possible, then you can initialize an instance by deserializing JSON.
Answer by Fornoreason1000 · Dec 04, 2013 at 01:42 PM
you can use constructors in your Object to achieve this
public class MyObject {
string text;
float num;
int num2;
int[] sub;
public MyObject(string text, float num, int num2, int[] subprop) {
this.text = text;
this.num = num;
this.num2 = num2;
this.sub = subprop;
//code goes here
}}
to create the static object, you can now use this constructor
static MyObject obj = new MyObject("blurzdfasdf", 69f, 69, new int[] {6,9});
is there way to do it without particular Class but only with Object ?
i mean, this works, but is there way to do it as in AS3 with JSON?
don't confuse Object with object...Object is unity specific, object is basically everything. object is the base class for absolutely everything inclusig UnityEngine.Object
yes you can, but its slightly different, object can't have properties, because the properties are object. everything is cast able to object, even Arrays!
this code will work straight out the box
static object obj = new object[] {"blurzdfasdf", 69f, 69, new int[] {6,9}};
however the problem with this is that the compiler no longer understands that there are parameter there so you ight want an object[] ins$$anonymous$$d of object.
No there is no way to create an object like that in C#. see the thing is, that is basically a "class" in terms of true JS(which is pretty much AS3) your defining an object with properties. when Javascipt programmers (not Unity Script!!) want a "class" they use that code. so the only way is to create a class or struct with the properties you want.
too bad
well the 1st answer works well for my case, i just wanted to know if it's possible ... i miss JSON the native way it works in AS3
thx a lot guys!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Do you have to set a object reference for every script? 0 Answers
Distribute terrain in zones 3 Answers
Creating instance at the beginning of the game 1 Answer
Rotation with multiple objects 1 Answer