Part 4: Using the State Management Script

This post follows: Part 3: Creating the Game State Management Script

We have the State Management Script, how does it get hooked up into the sample project?    We’ll create a new scene to enable us to build test controls for the script.  We’ll be using the UI Canvas and mostly work with 2D controls.   Our test scene will use UI Text objects to display the resource levels and UI Sliders to control the generation and consumption rates of the resource.

The following video shows the steps needed for creating the scene and controls.  Scrub through the video to jump to specific sections or rewind/pause as needed, the video is setup as an alternative to screen captures.

Video Notes

  • Change the SkyBox on the camera to a solid color since we’ll be effectively working with a 2D display
  • Create UI Text Objects to display the resource levels from the script and adjust its anchor, pivot and position.  The shortcut is to press ALT and SHIFT when defining the anchor setup.
  • Our project was setup for 3D but for the test scene, we need to switch the scene view to 2D.
  • Change the color of the UI Text object to make it easier to read.  The object is then duplicated (CTRL-D) and renamed to properly identify each item.   Since the object are duplicates, they are all in the same position on the screen/canvas — to reposition, change the Y values so they are placed top-down.   The anchor is setup for Top-Left which requires the Y values to be decreasing in value for each following UI Text object.
  • Besides the 4 resource level text objects, we need to have an indicator when we have hit system failure levels.   The alert text object is another duplicate with a different color, font size and position.
  • UI Sliders are created so the generation and consumption rates of each resource can be changed at Runtime.  The slider is setup horizontally by default requiring a change in the properties to make it a bottom-up control.  The size would need some adjustment to fit the screen.  The slider is then duplicated to preserve the property changes we applied for the new sliders.
  • To load the State Management Script in to the scene, an Empty GameObject is created which will host the script.  The public properties of the script are accessible on the Inspector when the GameObject is selected.    The UI Text instances are assigned to the script in order to be accessible during runtime; other properties such as current level and maximum levels are also  defined.
  • The UI Sliders interact with the script through its exposed Unity Event “On Value Changed”, this can be setup through script/code or the Slider Inspector.   The script GameObject can be passed to the Slider which can then access the public properties and methods — the Slider is able to pass its value directly to a property exposed by the script as well as calling other publicly accessible methods. The Air and Water sliders are setup with the script properties.
  • The default value of the slider ranges from 0 to 1 which can be adjusted,   we modify this value to 0 to 10 to make the rate of change faster.
  • The sample script in Part 3 only updated the Air Levels, we effect a change to include updates for the Water Level.

We simulated the Mars Base Modules functionality through UI Sliders, on the next post we’re going back to the Mars Base Scene and create the scripts needed by the modules.

For questions, comments or contact – follow me on Twitter @rlozada

Next: Part 5: Applying Scripts to the Scene