我試圖設置一個遊戲的高分指標,我希望它看起來每個設備都是一樣的,這就是爲什麼我要使用GUILayout。我仍然對此感到陌生,並且發現這個特殊的功能令人困惑。一般的想法是有一個看起來像這樣的佈局: 高分:(分數),並在遊戲畫面中看起來像這樣:(硬幣紋理)×(收集的數量)。任何關於如何正確設置的幫助都會很棒。每個人都會進入屏幕的左上角。GUILayouts Aligning Problems
0
A
回答
1
Unity中的GUILayout更多的是關於自動定位,而不是關於分辨率的獨立性。基本上,GUILayout元素將自動定位到其包含區域的左上角,並且自動調整大小,除非您使用GUILayoutOptions指定尺寸。檢查出GUILayout tutorial。
要支持所有分辨率,您需要使用GUI.Matrix(解釋爲here),或使用Screen.width
和Screen.height
手動縮放事物。
但是不考慮分辨率問題,下面介紹一些代碼(使用GUI.Matrix方法 - 硬編碼像素值將與所選1280x800「原生」佈局分辨率有關)。
public int currentScore = 120;
public int highScore = 1490;
public Texture2D coinTexture;
// Native layout dimensions
public const float LAYOUT_WIDTH = 1280f;
public const float LAYOUT_HEIGHT = 800f;
private void SetGUIMatrix()
{
Vector3 scale = new Vector3(Screen.width/LAYOUT_WIDTH,
Screen.height/LAYOUT_HEIGHT,
1f);
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
}
void OnGUI()
{
SetGUIMatrix();
Rect highScoreRect = new Rect(10, 10, 120, 100); // 120x100px rect near top left corner of the screen
GUILayout.BeginArea(highScoreRect);
// High score
GUILayout.Box("High score: " + highScore, new GUILayoutOption[] { GUILayout.Height(40) });
// Current score
GUILayout.BeginHorizontal();
GUILayout.Box(coinTexture, new GUILayoutOption[] { GUILayout.Width(60), GUILayout.Height(40) });
GUILayout.Box(currentScore.ToString(), new GUILayoutOption[] { GUILayout.Height(40) });
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
請注意,使用這種方法時,元素將被拉伸以適應不同的寬高比,這可能是也可能不是你想要的。希望這可以幫助!
編輯:對不起,錯過了問題中的JavaScript標記。這裏的JS也是一樣:
var currentScore = 120;
var highScore = 1490;
var coinTexture : Texture2D;
// Native layout dimensions
var LAYOUT_WIDTH = 1280.0;
var LAYOUT_HEIGHT = 800.0;
function SetGUIMatrix()
{
var scale = new Vector3(Screen.width/LAYOUT_WIDTH,
Screen.height/LAYOUT_HEIGHT,
1.0);
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
}
function OnGUI()
{
SetGUIMatrix();
var highScoreRect = new Rect(10, 10, 120, 100); // 120x100px rect near top left corner of the screen
GUILayout.BeginArea(highScoreRect);
// High score
GUILayout.Box("High score: " + highScore, GUILayout.Height(40));
// Current score
GUILayout.BeginHorizontal();
GUILayout.Box(coinTexture, GUILayout.Width(60), GUILayout.Height(40));
GUILayout.Box(currentScore.ToString(), GUILayout.Height(40));
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
相關問題
- 1. Aligning UITextFields
- 2. stackpanel not center-aligning
- 3. HTML div-aligning
- 4. mathjax/mchem aligning
- 5. CapacitiveSensor problems
- 6. CSS Responsive comlumns - aligning text
- 7. aligning elements left and right
- 8. Aligning Div with bullet list
- 9. Aligning GridBagLayout單元格
- 10. Bootstrap Grid Columns Not Aligning
- 11. aligning datalabel to column highcharts
- 12. Raphael Rect&Text not aligning
- 13. JavaScript if else problems
- 14. Grails Quartz2 Session Problems
- 15. Sublime Snippet Selection Problems
- 16. Action listener java problems
- 17. MVC2 ViewData Problems
- 18. GUI actionPerformed valueChanged problems
- 19. C - struct problems - writing
- 20. if else conditions problems
- 21. Uploadify updateSettings problems
- 22. J Unit Test problems?
- 23. jQuery TinySort Problems
- 24. git commit problems
- 25. SDL:IDE/Image Problems
- 26. Javascript Countdown Problems
- 27. If else statement problems
- 28. Number Picker problems
- 29. KRL:if then else problems
- 30. Streaming RTP/RTSP:sync/timestamp problems