Recording and displaying clicks

Now that we have this foundational button working, let's add some text to display information to the player.

  1. Go to GameObject | UI |Text. From there, use the Anchor Presets tool, hold down Alt + Shift, and click on the top-center option. It may be hard to see, so let's fix that up next. Change the Width to 1920 and Height to 100. You will remember that 1920 is what we used in our Reference Resolution, so this will take up the entire size.
  2. Then, change the Font Size to 75 and then change the Paragraph Alignment to be centered horizontally, change the Text Color to white, change the Text to You have: $0, and rename the Text object to CashText:
    Recording and displaying clicks
  3. Now select the CashText object and duplicate it (Ctrl+D) and give it a name of RateText. Change the Pos Y to -75, the text to per second: 0.0 and change the Font Size to 50.
  4. Next, let's write some code. Go to the Project tab, move to the Assets folder, and create a new folder named Scripts. From there, create a new script by selecting Create | C# Script, and once it's created, name it GameController. Once named, double-click on it to open up your IDE.
  5. Use the following code:
    using UnityEngine;
    using UnityEngine.UI; // Text
    
    public class GameController : MonoBehaviour {
    
        /// <summary>
        /// How much cash the player has
        /// </summary>
        private float _cash;
        public float Cash
        {
            get
            {
                return _cash;
            }
            set
            {
                _cash = value;
                cashText.text = "You have: $" + _cash.ToString("0.00");
            }
        }
    
        /// <summary>
        /// How much cash is automatically earned per second
        /// </summary>
        private float _cashPerSecond;
        public float CashPerSecond
        {
            get
            {
                return _cashPerSecond;
            }
            set
            {
                _cashPerSecond = value;
                rateText.text = "per second: " + _cashPerSecond.ToString("0.0");
            }
        }
    
        [Tooltip("How much cash players will make when they hit the button.")]
        public float cashPerClick = 1;
    
        [Header("Object References")]
        public Text cashText;
        public Text rateText;
    
        // Use this for initialization
        void Start ()
        {
            Cash = 0;
            CashPerSecond = 0;
        }
        
        // Update is called once per frame
        void Update ()
        {
            Cash += CashPerSecond * Time.deltaTime;
        }
    
    public void ClickedButton()
        {
            Cash += cashPerClick;
        }

There is a lot of stuff going on in the previous code, so we will start off talking about the new aspects of code we used and then explain the overall content and meaning.

Working with accessors (get/set functions)

One of the things you may have noted is the addition of the get and set functions for the two properties. This is referred to as accessors and is a way for us to protect data or do something whenever a certain value changes. In this case, anytime the value changes, we will change the text displayed from our text objects.

Note

For more information on that check out https://msdn.microsoft.com/en-sg/library/aa287786(v=vs.71).aspx.

Tooltip and Header attributes

You may have noticed the Header being used briefly in Chapter 1. These allow users to have additional behavior from the Inspector tab in the Unity editor. In this case, Tooltip will display a comment when you highlight the property in the Inspector tab, and Header will display a header before the next variable displayed.

Unity 5.3 has added a whole bunch of new attributes for people to use that still aren't put together easily within Unity's documentation yet, but Lior Tal put together a list of them with links to their documentation at http://www.tallior.com/unity-attributes/.

Explaining GameController's functionality

In the previously mentioned code, we have a number of new variables that we are going to be working with. cashText and rateText are references to the Text components of the two text objects we created earlier and will change their text whenever the value of Cash or CashPerSecond changes due to the set functions. To be as efficient as possible, we only want to change the text when the value changes so that this works perfectly.

In our Update function, we are changing the value of Cash by increasing it by the current value of CashPerSecond. Note that we also use Time.deltaTime as we did in Chapter 1, 2D Twin-stick Shooter to make it so that it will increase by the amount of time that elapsed in seconds since the previous frame.

Finally, we added a function that we will call whenever we click on the button to increase cash by whatever amount we set CashPerClick to.

  1. Next, create an empty game object by going to GameObject | Create Empty. Rename it to GameController. Then, drag and drop the object to the top of the Hierarchy. Next, drag and drop the GameController script onto the object. Then, drag and drop the CashText and RateText objects to the respective properties in the newly added GameController component:
    Explaining GameController's functionality
  2. Now we need to call ClickedButton whenever the button is clicked, so from the Hierarchy tab, click on the Button object. From the On Click () section, press the + button and then drag and drop the GameController game object into the slot (drag it from the Hierarchy tab). Then, from the function bar, call GameController | ClickedButton:
    Explaining GameController's functionality
  3. Save the scene and play the game!
    Explaining GameController's functionality

Now we have a way to increase the amount of cash we have by clicking on the button!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset