Unity3d SelectionGrid单元格大小调整

时间:2014-11-19 16:08:21

标签: user-interface unity3d

我尝试创建游戏选项但是调整了单元格大小的问题。
对于选项"首先移动"我使用的是用户名。如果名字太长,我想增加单元格。如果更容易,请减小字体大小。
enter image description here

细胞样式:

    mCellStyle = new GUIStyle();//style for cells
    mCellStyle.normal.background = Resources.Load<Texture2D>("Textures/button_up_9patch");
    mCellStyle.onNormal.background = Resources.Load<Texture2D>("Textures/button_down_9patch");
    mCellStyle.focused.background = mButtonStyle.active.background;
    mCellStyle.fontSize = GUIUtils.GetKegel() - GUIUtils.GetKegel() / 5;
    mCellStyle.border = new RectOffset(7, 7, 7, 7);
    mCellStyle.padding = new RectOffset(20, 20, 20, 20);
    mCellStyle.alignment = TextAnchor.MiddleCenter;
    mCellStyle.wordWrap = true;

我的代码:

    GUIStyle lBackStyle = new GUIStyle(mCellStyle);
    lBackStyle.fontSize = GUIUtils.GetKegel();
    lBackStyle.active.background = null;
    lBackStyle.focused.background = null;

    GUIStyle lStyle = new GUIStyle(lBackStyle);
    lStyle.normal.background = null;

    //create contents and calculate height
    GUIContent lContent1 = new GUIContent(LanguageManager.GetText("FirstMove"));
    float lElemH1 = lStyle.CalcHeight(lContent1, lMaxWidht);
    GUIContent[] lArrayContent2 = new GUIContent[] { new GUIContent(MainScript.Logic.UserName), new GUIContent(MainScript.Logic.NurslingName) };
    float lElemH2 = mCellStyle.CalcHeight(lArrayContent2[0], lMaxWidht);
    GUIContent lContent3 = new GUIContent(LanguageManager.GetText("Difficulty"));
    float lElemH3 = lStyle.CalcHeight(lContent3, lMaxWidht);
    GUIContent[] lArrayContent4 = new GUIContent[] { new GUIContent(LanguageManager.GetText("Easy")), new GUIContent(LanguageManager.GetText("Hard")) };
    float lElemH4 = mCellStyle.CalcHeight(lArrayContent4[0], lMaxWidht);

    float lTotalH = lElemH1 + lElemH2 + lElemH3 /*+ 100*/;//reserve 100 for paddings

    GUILayout.BeginArea(mAreaRect);
    GUILayout.BeginVertical(lBackStyle, GUILayout.Height(lTotalH));
    GUILayout.Label(lContent1, lStyle);
    GamePreferences.setAIMakesFirstMove(GUILayout.SelectionGrid(GamePreferences.getAIMakesFirstMove(), lArrayContent2, 2, mCellStyle));
    GUILayout.Label(lContent3, lStyle);
    GamePreferences.setDifficulty(GUILayout.SelectionGrid(GamePreferences.getDifficulty(), lArrayContent4, 2, mCellStyle));
    GUILayout.EndVertical();
    GUILayout.EndArea();


增加:我只想在选择网格中设置单元格的高度。有可能吗?

1 个答案:

答案 0 :(得分:1)

好的,我发现我可以通过GUILayoutOption作为最后一个参数来设置单元格的高度:

GUILayout.SelectionGrid(GamePreferences.getFirstMove(), lArrayContent2, 2, mCellStyle, GUILayout.Height(lCellHeight))

但我仍然不知道如何将细胞大小与其内容相匹配。

float lElemH2 = mCellStyle.CalcHeight(lArrayContent2[0], lMaxWidht);

mCellStyle.CalcHeight忽略了长度的内容,并在lElemH2我获得了单行文字的高度。

<强>编辑:
我的错,我向CalcHeight发送了错误的参数。正确的版本:

float lElemH2 = mCellStyle.CalcHeight(lArrayContent2[0], lMaxWidht / cellsCount);