Free Prize functionality is a great way of keeping users coming back to your games by offering them a reward after a given period of time. This is something that is used to great effect in a number of popular games such as Crossy Road and Air Hockey Dreams.

Setting this functionality up in Unity is simple. First you want to create a new Unity project and have the Game Framework downloaded and added as described here.

FreePrizeManager

The FreePrizeManager component controls the status of the free prize so we need to start by adding this to the scene.

Create a new gameobject and rename it _GameScope, add the FreePrizeManager component to this and set the values as below which will give a prize of between 10 and 20 (coins) after a wait of between 10 and 30 minutes. Note that the FreePrizeManager is persistant and will remain active across scene changes.

Showing the Status

We now have a countdown in place that will be automatically saved across restarts. Next we need to add functionality to show the current status. You can create your own code based upon specific needs, but for this example we will use the build in functionality and:

Show a counter if there is no free prize available.

Show a button if there is a prize available.

Select the menu GameObject->UI->Panel to add a new user interface to your scene. Do similar to add Button and Text gameobjects as children of this so you end up with the following setup

Next add the EnableIfPrizeAvailable component to the Panel gameobject. Drag the Button gameobject that you added to the Prize Available Game Object field, the Text gameobject to the Prize Countdown Game Object field and set Run to On Update as shown below.

Finally add the TimeToFreePrizeDisplay component to the Text gameobject so that we get a counter, and set the Run value to On Update.

We now have full free prize functionality. Run the scene and and test by either waiting, or using the Make Prize Available and Reset Counter buttons on the FreePrizeManager component to check that the correct gameobjects are shown under the correct conditions.

Showing a button however isn’t much of a reward, we need to do a bit more. You can link the button up to your own custom code, or use the build in components if you want to reward the user with coins.

Built in Rewards

The built in rewards system rewards the user with extra coins. This relies on setting up and including some other parts of the Game Framework.

Add a GameManager component to _GameScope. GameManager is used for handling global game state.

Next add a new UI->Text gameobject underneath Panel as we did previously. Rename it Coins and then modify its Rect Transform as shown below to anchor it to the top right of the screen. You can also right align the text as shown. (click for a larger version)

Add the ShowCoins component to the new Coins gameobject. Now try running the scene and you will see the new Text component shows the number of coins that the player has (currently 0).

Hint: Add the CheatFunctions component to _GameScope to give you an easy way of modifying scores and other values.

Finally create a new empty gameobject and rename it _SceneScope. To this add a DialogManager component (used for showing and managing dialogs and popups).

We are now ready to add connect the custom popup. Simpy add the OnButtonClickShowFreePrizeDialog component to the Button we created earlier and you are done.

Run the scene and check everything works and that the coin score is updated. Use the test buttons to help with testing and verify if you stop and start the scene the coin score is retained.

Custom Code

If the build in functionality isn’t sufficient then you can also trigger your own code. Simply create a new (or existing component) and hook that up to the button we setup in the first step by adding it as an onlick event.

You can either role your own prize, or access the calculated prize value by using:

FreePrizeManager.Instance.CurrentPrizeAmount

Once the user has collected the prize, be sure to use the following call to reset for the next prize:

FreePrizeManager.Instance.StartNewCountdown();

Next Steps

We have created a full Free Prize sample in a very short period of time with a minimum amount of coding. There are a lot more options that you can explore, in particular making the whole UI more visually appealing. We will come back to this in part 2, but in the meantime check out the below games for how the FreePrize and other functionality from the framework can work.