- Home /
Instantiate a sprite using json data value (C# Unity)
I'm making a scoreboard like this
Now here is my script
BetBoard_Test.cs
//Scoreboard
[SerializeField] protected GameObject prefab_big_road = null;
[SerializeField] Transform pos_big_road = null;
string jsonString = "[1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]"; //sample data
private void Start()
{
ExampleClass dataParser = new ExampleClass();
dataParser.dataToParse = jsonString;
//Convert to Json
string exampleClassToJson = JsonUtility.ToJson(dataParser);
Debug.Log(exampleClassToJson);
ExampleClass obj = JsonUtility.FromJson<ExampleClass>(exampleClassToJson);
//Loop over it
for (int i = 1; i < obj.dataToParse.Length - 1; i += 3)
{
char indivisualChar = obj.dataToParse[i];
int j = 0;
if(j < rh.Const._HISTORY_COUNT_ * rh.Const._HISTORY_HEIGHT_)
{
//lets increment it
j++;
//instantiate the sprite
GameObject o = Instantiate(prefab_big_road) as GameObject;
o.transform.SetParent(pos_big_road);
o.transform.localScale = Vector3.one; //(1,1,1)
int x = j % rh.Const._HISTORY_COUNT_;
int y = j / rh.Const._HISTORY_COUNT_;
float xl = 2.3f;
float yl = -26.69f;
o.transform.localPosition = new Vector3(x * xl, y * yl, 0f);
o.GetComponent<UISprite>().spriteName = indivisualChar == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";
NGUITools.SetActive(o, true);
}
}
}
void DeleteChildrens(Transform t)
{
NGUITools.DestroyChildren(t);
}
}
[Serializable]
public class ExampleClass
{
public string dataToParse;
}
ConstantValue.cs
public const int _HISTORY_COUNT_ = 70;
public const int _HISTORY_HEIGHT_ = 6;
PlayInfo.cs
public int[] _BIG_ROAD_ = new int[Const._HISTORY_COUNT_ * Const._HISTORY_HEIGHT_ ];
Those code above give me this ouput
And what most bothering me here is that the prefab that is clone are all red there's no blue one even though i provided this string jsonString = "[1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]" red = 2 , blue = 1
Answer by Ginxx009 · Apr 23, 2018 at 03:17 AM
What i am missing is like this
char indivisualChar = obj.dataToParse[i].toString();
then call it like this
o.GetComponent<UISprite>().spriteName = indivisualChar == "1" ? "layout_player_bigline-01" : "layout_banker_bigline-01";
Answer by Bunny83 · Apr 19, 2018 at 04:53 AM
Your "ExampleClass" does not reflect your JSON data. Your JSON data is an array of integer values while your ExampleClass only has a single string value. Furthermore Unity's JsonUtility can not handle arrays as top element. So you may want to declare your ExampleClass class like this:
[Serializable]
public class ExampleClass
{
public int[] data;
}
And the corresponding JSON would look like this:
"{\"data\":[1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]}"
Or when written in an actual file just
{
"data" : [1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]
}
Sorry for being noob but i'm declaring my sample data just like this
string jsonString = "[1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]";
How can convert my string data to int @Bunny83
Have you read my answer carefully? Unity's JsonUtility can not deserialize a JSON array as top level element. Even though your string does contain valid JSON you can't use JsonUtility to read it. You either need to wrap it in an object (like i did in my example) or use a different third party JSON parser (like my SimpleJSON for example ^^).
If you just hardcode your values you may just want to store them as an int array in the first place and not as JSON string?
int[] data = new int[]{1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1};
Anyways if you use my SimpleJSON parser you can simply do:
// at the top of your script
using SimpleJSON;
// [ ... ]
JSONNode data = JSON.Parse( jsonString );
foreach(JSONNode n in data)
{
int val = n;
// use "val"
}
Your answer
Follow this Question
Related Questions
How to gather data and stop gathering after collision 2 Answers
instantiate problem 1 Answer
Saving JSON file in Android,How to save json file in android 0 Answers
LitJSON android problem 1 Answer
Receipt JSON Split 0 Answers