Tag Archives: display

Adding a UI to display the score in a 2D game with Unity

This tutorial continues on from the previous tutorial on adding collectables and scoring to a 2D Unity game. In the previous tutorial, we improved the points scoring system and added collectables (coins) of different values).

In this tutorial we will add a UI (user interface) which will display the number of coins collected to the user. The LevelManager script will be responsible for displaying and updating the score text on screen. Watch the video below and then scroll down for the sample code.

Sample code

Here is the sample C# code for the LevelManager script.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class LevelManager : MonoBehaviour {
  public float respawnDelay;
  public PlayerController gamePlayer;
  public int coins;
  public Text coinText;

  // Use this for initialization
  void Start () {
    gamePlayer = FindObjectOfType<PlayerController> ();
    coinText.text = "Coins: " + coins;
  }
  
  // Update is called once per frame
  void Update () {
  
  }

  public void Respawn(){
    StartCoroutine ("RespawnCoroutine");
  }

  public IEnumerator RespawnCoroutine(){
    gamePlayer.gameObject.SetActive (false);
    yield return new WaitForSeconds (respawnDelay);
    gamePlayer.transform.position = gamePlayer.respawnPoint;
    gamePlayer.gameObject.SetActive (true);
  }

  public void AddCoins(int numberOfCoins){
    coins += numberOfCoins;
    coinText.text = "Coins: " + coins;
  }
}

Using Toasts to display messages in Android

A Toast is a small message that is temporarily displayed at the bottom of a screen in Android apps and is usually used to notify the user that something has occurred or changed, or to confirm an action that the user took such as saving a file or sending a message. The example app below shows a Toast that gives a confirmation to the user that they clicked a button. The Toast will appear when the button is clicked and then disappear a few seconds later.

image1

Step 1

For this example, we will display a Toast when a button is clicked. Add a button to your app’s screen. In this example, we are using the Main.axml screen and the default button.

image2

Step 2

When the button is clicked it will display a toast. You need to define the button in your code and create a click event for it. You can use the existing button and click event that usually exists by default when you create a new Android app, if you wish.

The following line needs to be added inside the click event for the button to make the Toast appear:

Toast.MakeText(this, "You clicked the button", ToastLength.Long).Show();

The complete code should look something like this:

image3
Click the image to see a larger version.

The string message inside the quotes could also be replaced with a variable’s value, or you could also display a variable’s value joined to a string. The Toast can be used for any event such as a click event, when an option is selected or changed, after a certain time period, when something is received, or even just when the app loads.

Go and test your app and it should look something like this when the Toast appears after clicking the button.

image1

Date and time in PHP

In this tutorial you will learn how to set the timezone in a PHP program and how to store and display the current date and time like what you see below.

date_time_output

There are a range of different timezones you can use in PHP as well as date/time formats. The links below provide a list of timezones and date/time formats in the PHP manual:

Watch the video below and then scroll down to see the sample code from the video as well as a table containing the most commonly used date/time formats.

Sample PHP code:

<?php
  // set timezone
  date_default_timezone_set('Australia/Sydney');
  
  // display the date and time with a greeting

  function show_date(){
    return date('l, jS F H:i');
  }

  function greeting(){
    $hour = date('H');
    if($hour < 12){
      $greeting = "Good morning!";
    }
    else{
      $greeting = "Good day!";
    }
    return $greeting;
  }

  echo show_date();
  echo "<br/>" . greeting();
?>

List of commonly used date/time formats:

Argument Description Example
Y Year displayed as 4 digits 2016
y Year displayed as 2 digits 16
n Month displayed as 1 or 2 digits 9
m Month displayed as 2 digits 09
F Full name of the month December
M Name of the month as 3 letters Dec
j Day displayed as 1 or 2 digits 3
d Day displayed as 2 digits 03
l Full name of the weekday Thursday
D First three letters of weekday Thu
S Ordinal suffix displayed as 2 letters eg. rd or th or nd
H Hour in 24hr time 17
h Hour in 12hr time 5
i Minutes 30
s Seconds 50
a am/pm lowercase am
A AM/PM uppercase PM