0

这是我的代码:

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

常量值.cs

public const int _HISTORY_COUNT_ = 70;
public const int _HISTORY_HEIGHT_ = 6;

播放信息.cs

public int[] _BIG_ROAD_ = new int[Const._HISTORY_COUNT_ * Const._HISTORY_HEIGHT_ ];

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

在此处输入图像描述

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

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

在此处输入图像描述

4

1 回答 1

0

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

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

谢谢。

于 2018-04-23T06:57:10.910 回答