0

Here is my code :

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];

        Debug.Log(indivisualChar);
    }

    WinLog();
}

IEnumerator WinLog_big_road()
{
    DeleteChildrens(pos_big_road);

    yield return new WaitForEndOfFrame();

    int[] array_big_road = tzPlayInfo.Instance._BIG_ROAD_;

    for (int i = 0; i < rh.Const._HISTORY_COUNT_ * rh.Const._HISTORY_HEIGHT_; i++)
    {
        if (array_big_road[i] == 0) continue;

        int x = i % rh.Const._HISTORY_COUNT_;
        int y = i / rh.Const._HISTORY_COUNT_;
        float xl = 9.0f;
        float yl = -8.0f;

        GameObject o = Instantiate(prefab_big_road) as GameObject;
        o.transform.SetParent(pos_big_road);
        o.transform.localScale = Vector3.one; //(1,1,1)

        o.transform.localPosition = new Vector3(x * xl, y * yl, 0f);
        o.GetComponent<UISprite>().spriteName = array_big_road[i] == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";

        NGUITools.SetActive(o, true);
        yield return new WaitForEndOfFrame();
    }
    yield break;
}

    void DeleteChildrens(Transform t)
    {
        NGUITools.DestroyChildren(t);
    }

    public void WinLog()
    {
        StopCoroutine("WinLog_big_road");
        StartCoroutine("WinLog_big_road");
    }
}

[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_ ];

What i am trying to achieve here is that see image

enter image description here

my jsonString="[1, 1, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]"; that is converted into json format needs to do it something like this for example

1 = blue circle
2 = red circle

enter image description here

just like that in the picture every value on my json data needs to be instantiated with the sprite that is equivalent to 1 and 2 which is i have this condition o.GetComponent<UISprite>().spriteName = array_big_road[i] == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";

PS: I am very sorry if i couldn't explain it very well because english is not my native so i provided a picture. I am very sorry.

EDIT: I did it like this but the problem is that it's not getting what i want all the red which is 2 is the only appearing on the board

 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 = 9.0f;
            float yl = -8.0f;
            o.transform.localPosition = new Vector3(x * xl, y * yl, 0f);
            //o.GetComponent<UISprite>().spriteName = indivisualChar == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";
            if (indivisualChar == 1)
            {
                o.GetComponent<UISprite>().spriteName = "layout_player_bigline-01";
                NGUITools.SetActive(o, true);
            }
            else
            {
                o.GetComponent<UISprite>().spriteName = "layout_banker_bigline-01";
                NGUITools.SetActive(o, true);
            }
        }
       //Debug.Log(indivisualChar);
    }

EDITED: More information.

It just give me this

enter image description here

All of the sprites are in one place and the second problem of that is all the prefab that is cloned is always red (2)

enter image description here

Ginxxx
  • 1,602
  • 2
  • 25
  • 54
  • As far as I have understood, you only set the sprite name on the above code, but didn't set a sprite actually. You need to set the sprite from a prefab may be – ZayedUpal Apr 18 '18 at 06:16
  • In that `for` loop, if `indivisualChar` is `1` instantiate blue. If it is `2` instantiate red. Not sure where you are lost but this is all you need to do. – Programmer Apr 18 '18 at 06:17
  • @Programmer I edit my question sir and added some information . I try to code this but its not getting what i want – Ginxxx Apr 18 '18 at 06:27
  • @ZayedUpal Sorry i updated my question because i tried something – Ginxxx Apr 18 '18 at 06:30
  • 1
    Sorry I don't use NGUI. I use Unity's UI system but your edit is looking good. If you are not getting what you expected I think you should post a screenshot of what you are getting now maybe someone that uses NGUI will help you out – Programmer Apr 18 '18 at 06:39
  • @Programmer i guess its not about the NGUI. Could you help me please – Ginxxx Apr 18 '18 at 09:59
  • 1
    IT mattes which UI system you are using. If you are using Unity UI, you can do this with the *Grid Layout Group* component. You may want to start using Unity UI or ask the question about NGUI on their site – Programmer Apr 18 '18 at 10:17
  • @Programmer hi can u please help me. Here is a more concrete question . https://stackoverflow.com/questions/49913711/get-json-data-and-parse-the-data-individually-c-unity – Ginxxx Apr 19 '18 at 08:17

1 Answers1

0

I solve the issue about all game prefab that are instantiated is all red so what at i did here was like this.

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.0f;
  float yl = -22.0f;
  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);
        }

Thanks.

Ginxxx
  • 1,602
  • 2
  • 25
  • 54