字符串数据数组不同时增加位置C#(Unity)

时间:2018-05-02 03:11:37

标签: c# android unity3d

我需要有关我正在制作的记分牌的帮助。这是我想要实现的记分牌:

Enter image description here

我实际上有四张桌子,每张桌子都有自己的记分牌,但对于这个问题只针对表格1,我需要这样就是我如何创建这种记分牌。

现在,到目前为止,我已经这样做了:

if (gametable_no == 1)
{
    for (int i = 0; i < tzPlayInfo.Instance.bc_gametable_history_list.Count; i++)
    {
        newString[0] += tzPlayInfo.Instance.bc_gametable_history_list[i].r;
        newString[0] += ",";
    }
    string[] groupedString = newString[0].Split(',');

    foreach (string allchars in groupedString)
    {
        int x = 0;
        int y = 0;

        GameObject o = Instantiate(prefab_big_road[0]) as GameObject;
        o.transform.SetParent(pos_big_road[0]);
        o.transform.localScale = Vector3.one;
        o.transform.localPosition = new Vector3(2.0f, -5.0f, 0f);
        o.transform.localPosition = new Vector3(o.transform.localPosition.x + x,
            o.transform.localPosition.y + y, o.transform.localPosition.z);

        if (allchars.Contains(playerwinnopairboth))
        {
            o.GetComponent<UISprite>().spriteName = "layout_player_bigline-01";
            NGUITools.SetActive(o, true);
        }

        if (allchars.Contains(bankerwinnopairboth))
        {
            o.GetComponent<UISprite>().spriteName = "layout_banker_bigline-01";
            NGUITools.SetActive(o, true);
        }

        if (allchars.Contains(tienopairboth))
        {
            o.GetComponent<UISprite>().spriteName = "layout_tie_bigline-01";
            NGUITools.SetActive(o, true);
        }
    }
}

此代码只会像这样实现:

Enter image description here

输出是随机生成的,例如我有这个输出数据:

Gametable 1 history : P  ,P  ,P  ,B   ,B P,P P,TBP

我需要知道gametable 1 history data中的角色正在改变

例如:输出必须像这样

Enter image description here

如果值不像P ,P ,P那样改变,那么位置将仅在y轴上递增,然后如果下一个值与B ,不同,则它将移动到x轴。< / p>

有没有办法检测字符串数组中的字符是否与其他值不同?

EDITED

string[] newString = new string[4];

string[,] table = new string[70, 70];

string previousValue = "placeholder";
int xIndex = -1;
int yIndex = 0;

// First table
if (gametable_no == 1)
{
    for (int i = 0; i < tzPlayInfo.Instance.bc_gametable_history_list.Count; i++)
    {
        newString[0] += tzPlayInfo.Instance.bc_gametable_history_list[i].r;
        newString[0] += ",";
    }

    //string[] groupedString = GroupString(newString[0]);
    string[] groupedString = newString[0].Split(',');
    foreach (var allchars in groupedString)
    {
        GameObject o = Instantiate(prefab_big_road[0]) as GameObject;
        o.transform.SetParent(pos_big_road[0]);
        o.transform.localScale = Vector3.one;

        if (table.GetLength(0) < xIndex)
        {
            break;
        }

        if (allchars.Equals(previousValue) && table.GetLength(1) < yIndex)
        {
            yIndex += 15;
            table[xIndex, yIndex] = allchars;
        }
        else
        {
            xIndex += 15;
            yIndex = 0;
            table[xIndex, yIndex] = allchars;
        }
        previousValue = allchars;

        o.transform.localPosition = new Vector3(xIndex, yIndex, 0f);

        // Match the sprites
        if (previousValue.Contains(playerwinnopairboth))
        {
            o.GetComponent<UISprite>().spriteName = "layout_player_bigline-01";
            NGUITools.SetActive(o, true);
        }

        if (previousValue.Contains(bankerwinnopairboth))
        {
            o.GetComponent<UISprite>().spriteName = "layout_banker_bigline-01";
            NGUITools.SetActive(o, true);
        }

        if (previousValue.Contains(tienopairboth))
        {
            o.GetComponent<UISprite>().spriteName = "layout_tie_bigline-01";
            NGUITools.SetActive(o, true);
        }
    }

它只生成很少的预制件:

Enter image description here

虽然我已经生成了这些数据:

Enter image description here

在我的界面中,它只显示两个:

Enter image description here

2 个答案:

答案 0 :(得分:1)

我建议使用多维数组作为表格。 也许是这样的?

string[,] table = new string[30, 6];

string previousValue = "placeholder";
int xIndex = -1;
int yIndex = 0;

foreach (var value in groupedString)
{
    if (table.GetLength(0) < xIndex)
    {
         break;
    }

    if (value.Equals(previousValue) && yIndex < table.GetLength(1))
    {
        yIndex += 1;
        table[xIndex, yIndex] = value;
    }
    else
    {
        xIndex += 1;
        yIndex = 0;
        table[xIndex, yIndex] = value;
    }
    previousValue = value;
}

现在您应该有一个包含所有值的表格,然后您可以在UI上显示这些值并相应地匹配正确的精灵。

答案 1 :(得分:1)

感谢Immorality for showing me his answer,它实际上是正确的,但错过了条件:

if (table.GetLength(0) < xIndex)
            {
                break;
            }

            if (previousValue.Equals(newPreviousValue) && yIndex < table.GetLength(1))
            {
                yIndex += 1;
                table[xIndex, yIndex] = previousValue;
            }
            else
            {
                xIndex += 1;
                yIndex = 0;
                table[xIndex, yIndex] = previousValue;
            }
            newPreviousValue = previousValue;

所以代替table.GetLength(1) < yIndex,它必须是yIndex < table.GetLength(1)