我尝试创建游戏选项,但在调整单元大小时遇到了问题。
对于选项“第一步”,我使用用户名。如果名字太长,我想增加单元格。或缩小字体大小,如果更容易。
单元格的样式:
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();
我只想在选择网格中设置单元格的高度。有可能吗?
发布于 2014-11-24 09:35:49
好的,我发现我可以传递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);
https://stackoverflow.com/questions/27021486
复制相似问题