Book Image

OUYA Game Development by Example

By : John Donovan
Book Image

OUYA Game Development by Example

By: John Donovan

Overview of this book

The OUYA console and development kit gives you the power to publish video games for the players, creating a console marketplace of the gamers, for the gamers, and by the gamers. Using the OUYA developer kit and the Unity3D game engine, even beginners with a captivating game idea can bring it to life with a hint of imagination. OUYA Game Development by Example uses a series of feature-based, step-by-step tutorials that teach beginners how to integrate essential elements into a game engine and then combine them to form a polished gaming experience.
Table of Contents (18 chapters)
OUYA Game Development by Example Beginner's Guide
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – saving data with PlayerPrefs


We're finally ready to save and load data to the player's collection class. For this, we'll be using Unity's PlayerPrefs class in code.

  1. Open the PlayerCollection.cs file in your code editor and add the following lines to your CollectCoin function:

    public void CollectCoin()
    {
      totalCoins++;
      print(totalCoins);
      PlayerPrefs.SetInt("TotalCoins", totalCoins);
    }

    It's as simple as that! Let's examine what this new line actually does. We're calling the SetInt function from the PlayerPrefs class because the data we're saving is an integer value. However, PlayerPrefs also contains functions to save floating-point values and strings.

    No matter what kind of data you're saving, the parameters for the saving function are generally the same. The first value is a text string that represents a key or a way to label the data you're saving so that you can access it later.

    The second parameter is the data you're saving with that key, be it an integer, string, or...