public:t-vien-15-3:lab6_the_interface
Table of Contents
Lab 6: The Interface
Goal
The goal of this lab is to get familiar with GUI basics. You will create a intro- or title screen that you can dismiss with the press of a button. Then you will add a HUD that shows your current power as you walk through your environment.
Documentation
Procedure
- Create a Scene Create a relatively simple scene (such as a Plane) with a
FPSController
that allows you to walk around and enjoy the sights. Delete the defaultMainCamera
. - Create Intro Screen Create an empty
GameObject
at position(0,0,0)
to serve as the root node for the intro screen and rename itInroScreen
. Now create anUI→Image
object. Notice that along with it you also get aCanvas
object, which becomes the parent of theImage
. You also get anEventSystem
object that supports GUI events. Drag theCanvas
under theIntroScreen
(creating the hierarchyIntroScreen→Canvas→Image
). Select theCanvas
and set itsRender Mode
property (in theCanvas
component) toScreen Space - Overlay
. This will render the canvas on top of whatever scene you have created, and does not even require a camera. theUi Scale Mode
toScale With Screen Size
andScreen Match Mode
toExpand
(both under theCanvas Scaler
component/script). Finally, pick aReference Resolution
that matches your computer's screen resolution (e.g.1920
by1080
). Double-click on theCanvas
object to show it in the Scene View and press the 2D button in the upper bar of the Scene View window (this is a nice mode for working with UI objects, but remember to de-select it when going back to working on the 3D scene). Now select theImage
object and select the 2D rect transform manipulation tool from the toolbox at the top left of the Unity screen (you see a rectangle with a little circle inside it). The corners of theImage
should now show up blue. Drag the corners of theImage
into the corners of theCanvas
so that they are the same size. When you press play, the screen should now fill with the image. - Populate the Intro Screen Bring a texture into your project that you want to use as the background image for the intro screen. Try finding something with the same aspect ratio as the
Reference Resolution
you picked above. One idea is to simply take a screenshot from a different scene you have previously created. Select the texture in the Project browser and change itsTexture Type
property toSprite (2D and UI)
. Then drag that texture into theSource Image
property of theImage
game object. Create a newUI→Text
object under theCanvas
object and with the 2D manipulation tool drag it into a good place on top of the image to place a caption. Anchor it to the underlying canvas by pressing the rectangle icon in itsRect Transform
(e.g. anchor it to the upper right corner). You should see the white target cross move to the corresponding corner of the canvas. Change theText
property of theText
object to say something interesting (e.g. the name of the virtual environment). Create a newUI→Button
object under theCanvas
and drag it into a good place. Anchor it to a corner of the canvas. Notice that aText
object gets created along with the button as its child. Change the text inside theButton
to say something like “Start”. Play with the fonts and colors of theText
andButton
elements you have just created. Notice that you can add a special 2D effect component to them (Add Component→UI→Effects..
). If you want to try different fonts, you can drag fonts from your windows folder into your project and they will appear as new font options in Unity. - Give the Button a Function We want to make the intro screen disappear when the button is pressed. We can do that by de-activating the
IntroScreen
parent node. With theButton
selected, look for the On Click() panel in the Inspector. Click on the target icon to select a game object that this event will involve. From the list of game objects in your hierarchy pick yourIntroScreen
. You can now browse all its public functions next to theRuntime Only
option. PickGameObject.SetActive
. A check bock appears. Leave it un-checked (which meansfalse
). Now test your application. Pressing the button should make the whole canvas disappear. Note however that even while the canvas is covering the whole scene, the FPS Controller is still active in the background and that any player control movements in the intro screen will change the position and orientation of the player. To avoid this, you can keep theFPSController
de-activated until the button gets pressed (without the application running, just un-check the checkbox next to the nameFPSController
at the top of the Inspector window). Then add another button event right underneath the de-activation of the intro screen event. Select theButton
and in the On Click() panel press the + symbol to add another event. MakeFPSController
the target object and selectGameObject.SetActive
as the function to call. This time check the check box that appears (which meanstrue
). When you run the application this time, the FPS Controller will remain dormant until the button gets pressed. NOTE: When you de-activate theFPSController
in your hierarchy, you lose your only camera in the scene. This seems to affect the rendering of the intro screen (even though overlays don't require cameras). To keep the rendering unchanged, you can add a dummy camera underIntroScreen
. - Add a HUD Element Note that you can temporarily de-activate the
IntroScreen
while you are editing other parts of the scene, including the HUD, so that it is not in your way, just remember to activate it again when running the final application. Create an empty game object and call itHUD
. Inside it create a newCanvas
object and set itsRender Mode
toScreen Space - Camera
. Drag theFirstPersonCharacter
object (child ofFPSController
) into itsRender Camera
property and set itsPlane Distance
to 1 (this is the distance from the chosen camera). Create anImage
,Text
and aScrollbar
on this canvas and anchor them all to the upper right corner. Use theUISprite
sprite as theSource Image
for theImage
and set theText
to sayPower
. Rename theScrollbar
toPowerbar
. - Rigging up the Powerbar Look at the
Scrollbar
component in thePowerbar
and set itsValue
to 0 andSize
to 0. De-select theInteractable
property (e.g. make it false). The color of the bar will be theDisabled Color
because it is not interactive. Play with its color and also the color of its child objectHandle
. - Updating the Powerbar Add the following script to the
HUD
object:using UnityEngine; using UnityEngine.UI; using System.Collections; public class HUDScript : MonoBehaviour { private GameObject powerbar; private Scrollbar powerscroller; // Use this for initialization void Start() { powerbar = GameObject.Find("Powerbar"); powerscroller = powerbar.GetComponent<Scrollbar>(); } public void SetPower(float value) { powerscroller.size = value; } // Update is called once per frame void Update () { } }
And add the following scrip to the
FPSController
:using UnityEngine; using System.Collections; public class PlayerScript : MonoBehaviour { public GameObject hud; private float pickupvalue = 0; // Use this for initialization void Start () { } public void Pickup() { pickupvalue = pickupvalue + 0.1f; hud.SendMessage("SetPower", pickupvalue); } // Update is called once per frame void Update () { } }
- Add Pickable Powerups Now you can use pickable objects from previous labs in your scene that will increase the HUD powerbar instead of just increasing a textual counter.
- Hide Mouse Cursor in FPS Mode When you are in FPS mode walking around, you probably don't want to see the mouse cursor on the screen as it serves not useful function. You can disable/enable by assigning either
true
orfalse
to theCursor.visible
variable in any script. So, for example, in order to hide the mouse when theFPSController
becomes activated, you can add this to thePlayerScript
(it overrides the methodOnEnable()
which automatically gets called on activation):void OnEnable() { Cursor.visible = false; }
Just remember to show the mouse cursor again if you chose to leave the FPS mode, for example when you press a button to get back into the intro screen (try implementing that functionality if you like).
/var/www/cadia.ru.is/wiki/data/pages/public/t-vien-15-3/lab6_the_interface.txt · Last modified: 2024/04/29 13:33 by 127.0.0.1