首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Unity3d SelectionGrid小区大小调整

Unity3d SelectionGrid小区大小调整
EN

Stack Overflow用户
提问于 2014-11-19 16:08:21
回答 1查看 2K关注 0票数 1

我尝试创建游戏选项,但在调整单元大小时遇到了问题。

对于选项“第一步”,我使用用户名。如果名字太长,我想增加单元格。或缩小字体大小,如果更容易。

单元格的样式:

代码语言:javascript
运行
复制
    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;

我的代码:

代码语言:javascript
运行
复制
    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();

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-24 09:35:49

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

代码语言:javascript
运行
复制
GUILayout.SelectionGrid(GamePreferences.getFirstMove(), lArrayContent2, 2, mCellStyle, GUILayout.Height(lCellHeight))

但我仍然不知道如何使单元格大小与它的内容相匹配。

代码语言:javascript
运行
复制
float lElemH2 = mCellStyle.CalcHeight(lArrayContent2[0], lMaxWidht);

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

编辑:

我的错是,我向CalcHeight发送了错误的参数。正确版本:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27021486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档