使用 json 数据值实例化精灵 (C# Unity)

Instantiate a sprite using json data value (C# Unity)

这是我的代码:

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

我在这里想要实现的是看图

我的 jsonString="[1, 1, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]"; 被转换成 json 格式需要这样做,例如

1 = blue circle
2 = red circle

就像图片中的那样,我的 json 数据上的每个值都需要用相当于 1 和 2 的精灵实例化,这是我有这个条件 o.GetComponent<UISprite>().spriteName = array_big_road[i] == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";

PS:如果我不能很好地解释它,我非常抱歉,因为英语不是我的母语,所以我提供了一张图片。非常抱歉。

编辑:我是这样做的,但问题是它没有得到我想要的所有红色,2 是唯一出现在板上的

 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);
    }

已编辑:更多信息。

它只是给我这个

所有精灵都在一个地方,第二个问题是所有克隆的预制件总是红色 (2)

我解决了所有实例化的游戏预制件都是红色的问题,所以我在这里做的是这样的。

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);
        }

谢谢。