- Unity Game Development Blueprints
- John P. Doran
- 480字
- 2021-08-05 17:29:05
Customizing the GUI
While it is great that Unity provides us with all of this functionality to save us time in creating elements, the actual aesthetics leaves a little to be desired. Thankfully, UnityGUI allows us to customize the appearance of our controls by making use of GUIStyle
, which is an optional third parameter to our control functions. If we do not specify a GUIStyle
parameter, Unity's default will be used, which is what we experienced last time. This can work fine while testing something out. However, since we're trying to create a polished and complete project, we're going to create one of our own by performing the following steps:
- Open up the
MainMenuGUI
script file and modify the function to accommodate the changes in bold:using UnityEngine; using System.Collections; public class MainMenuGUI : MonoBehaviour { public int buttonWidth = 100; public int buttonHeight = 30; public GUIStyle titleStyle; public GUIStyle buttonStyle; void OnGUI() { //Get the center of our screen float buttonX = ( Screen.width - buttonWidth ) / 2.0f; float buttonY = ( Screen.height - buttonHeight ) / 2.0f; //Show button on the screen and check if clicked if ( GUI.Button( new Rect( buttonX, buttonY, buttonWidth, buttonHeight ), "Start Game", buttonStyle ) ) { // If button clicked, load the game level Application.LoadLevel("Chapter_1"); } // Add game's title to the screen, above our button GUI.Label( new Rect(buttonX + 2.5f , buttonY - 50, 110.0f, 20.0f), "Twinstick Shooter", titleStyle ); } }
- Once finished, save the file into your
Scripts
folder and then move back into Unity. You should see the two variables showing up in the Inspector section: - For the Title Style option, navigate to Normal | Text Color and put in
white
. Change the Font option to the OSP-DIN font we added in the previous chapter, the Font Size option to42
, and the Alignment option to Middle Center: - Now, for the
buttonStyle
variable (Button Style in the inspector), we're first going to need some additional images for our buttons. So, go into yourChapter 2/Assets
location, which you should have obtained from Packt Publishing's website, and drag-and-drop the new images into ourSprites
folder. - Once that's finished, set the Normal, Hover, and Active Background properties to the blue, yellow, and green buttons, respectively. Next, set the Border property to
10
in each of the four directions. Setting the border will make sure that the background images do not exceed those many pixels in each of those directions. This is great for objects that can be different sizes, such as buttons and text fields. Finally, set the Font option to OSP-DIN and the Alignment option to Middle Center: - With all of that done, save your scene and run the game as shown in the following screenshot:
We now have something that already looks a lot better than what we had before, and you learned how to work with different styles!
- 我們都是數(shù)據(jù)控:用大數(shù)據(jù)改變商業(yè)、生活和思維方式
- 企業(yè)數(shù)字化創(chuàng)新引擎:企業(yè)級PaaS平臺HZERO
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)
- 信息系統(tǒng)與數(shù)據(jù)科學(xué)
- Architects of Intelligence
- Learning Spring Boot
- Python廣告數(shù)據(jù)挖掘與分析實戰(zhàn)
- 數(shù)據(jù)驅(qū)動設(shè)計:A/B測試提升用戶體驗
- OracleDBA實戰(zhàn)攻略:運維管理、診斷優(yōu)化、高可用與最佳實踐
- 基于OPAC日志的高校圖書館用戶信息需求與檢索行為研究
- Apache Kylin權(quán)威指南
- 重復(fù)數(shù)據(jù)刪除技術(shù):面向大數(shù)據(jù)管理的縮減技術(shù)
- SIEMENS數(shù)控技術(shù)應(yīng)用工程師:SINUMERIK 840D-810D數(shù)控系統(tǒng)功能應(yīng)用與維修調(diào)整教程
- 數(shù)據(jù)指標體系:構(gòu)建方法與應(yīng)用實踐
- Scratch 2.0 Game Development HOTSHOT