You are previewing Unity Game Development Scripting.
O'Reilly logo
Unity Game Development Scripting

Book Description

Write efficient C# scripts to create modular key game elements that are usable for any kind of Unity project

In Detail

The intuitive and powerful Unity game engine is one of the most widely used and best loved packages for game development. Unity scripting is an essential but challenging skill to master in order to create custom game elements. Learning modular scripting allows you to rewrite as little code as possible as you deploy your scripts to multiple projects and work easier, quicker, and more efficiently than before.

In each chapter of this book, you'll learn how to script new game elements. Beginning with making custom controls for the keyboard and mouse, as well as the Xbox 360 Controller, you'll then get to grips with more complex systems such as inventory, data saving, and artificial intelligence. As you create these elements, you'll also learn how to make your scripts simpler and easy to use. This will allow drag-and-drop deployment, which is to be used by designers and nonprogrammers. Finally, you'll combine all of your newfound skills to create your own complete game project.

What You Will Learn

  • Include controls for both keyboard/mouse and the gamepad
  • Create three different types of expandable items
  • Implement an inventory that includes a hot key system
  • Optimize your game's video and audio options
  • Integrate a GUI HUD with buttons, health bars, and damage reports
  • Make a complex AI system
  • Save game data in multiple styles
  • Track player statistics and scores
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Unity Game Development Scripting
      1. Table of Contents
      2. Unity Game Development Scripting
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Interactive Input
        1. Picking the controls
          1. Mapping the needed controls
          2. Checking the input manager
          3. Checking the Xbox 360 Controller inputs
          4. Adding additional controller inputs
          5. Adding a start button and trigger inputs
          6. Adding directional pad inputs
          7. Adding PC control inputs
        2. Housing our control script
          1. Creating and naming the script
          2. Formatting the script
        3. Creating the device detector
          1. Adding the variables needed
          2. Creating the detection function
          3. Creating the identifier function
        4. Let's get set and show them
          1. Adding variables for each control
          2. Adding a function to display the variables
        5. Let's switch!
          1. Creating control profiles
          2. Adding a profile switching function
          3. Adding the GUI interaction function
        6. Customization is key
          1. Swapping control schemes
          2. Adding a control switch button to the GUI
          3. Cycling control inputs
          4. Adding the controls pop up to the GUI
          5. Resetting the controls
          6. Adding the Reset function
          7. Adding the Reset input
        7. Playtesting
        8. Summary
      9. 2. GUI Time
        1. A traditional 2D UI
          1. Setting up our scene
          2. Housing our 2D UI
          3. Creating GUI buttons
          4. Creating a health bar
          5. Level counter
          6. Creating an experience counter
        2. Housing our 3D UI
          1. Creating a 3D health bar
          2. Creating 3D damage reports
          3. The new Update function
          4. Completing the damage reports
          5. Creating 3D name tags
        3. Summary
      10. 3. Expandable Item Classes
        1. The self item class
          1. Adding our variables
          2. Buff or debuff stats
            1. The health changer
            2. The armor changer
            3. The item activator
        2. The melee item class
          1. Adding our variables
          2. Buff or debuff stats
            1. The health changer
            2. Let's interact with the environment
            3. Detecting triggers
        3. The projectile item class
          1. Adding our variables
            1. Buff or debuff stats
            2. The health changer
            3. Adding movement
            4. Detecting triggers
        4. Playtesting
        5. Summary
      11. 4. Inventory
        1. Features of an inventory
          1. Limits of the inventory
          2. Accessing the inventory
          3. Organizing an inventory
        2. Item count modifications
          1. Item bartering
          2. Dropping and picking up items
          3. Destroying and using items
        3. Displaying the inventory
        4. Creating the inventory script
          1. Creating and naming the script
          2. Adding the necessary variables
          3. Initializing our inventory
          4. Creating the initializer
        5. Adding items
          1. Let's figure this out
          2. Creating the adding function
        6. Removing items
          1. Let's figure this out
          2. Creating the removing function
        7. Setting the quick-select items
          1. Setting the quick-select items quickly
          2. Let's display the inventory
          3. Using our custom inputs
          4. Displaying the GUI
          5. Running the GUI
        8. Playtesting
          1. Creating a test scene
          2. Let's add an item
          3. Let's remove some items
        9. Other things to try out
        10. Summary
      12. 5. Enemy and Friendly AIs
        1. AI techniques
          1. Finite state machines
          2. The behavior tree form of the AI system
            1. Combining the techniques
            2. Let's start scripting!
        2. Internal and external actions
          1. External actions
          2. Internal actions
          3. Scripting the actions
        3. Pathfinding
          1. Creating paths using the waypoint system
          2. Unity's NavMesh system
          3. Setting up the environment
            1. Creating the NavMesh
            2. Adding our variables
            3. Scripting the navigation functions
        4. Character animations
          1. Importing the model mesh
          2. Scripting the animations
        5. Putting it all together
          1. Final coding touches
          2. Filling out the inspector
        6. Playtesting
        7. Summary
      13. 6. Keeping Score
        1. Prototype stats
          1. Assigning the stats to the player
        2. The stat tracker
          1. Setting the stats
          2. Resetting the stats
          3. Resetting all of our prefs
          4. Saving all of our prefs
          5. Setting a specific pref
          6. Resetting a specific pref
          7. Showing our stats on the screen
        3. The achievement system
          1. Prototyping the achievements
          2. Adding the required achievement variables
          3. Resetting the achievements
        4. Achievement trackers
          1. Tracking the kills
          2. Tracking the gold total
          3. Tracking the gold spent
          4. Tracking the player's level
          5. Tracking the rounds won
          6. Tracking the time played
        5. Let's check the achievements
          1. Checking a specific achievement
          2. Checking all of the achievements
        6. Displaying the achievements on screen
          1. Adding the GUI functions
        7. Playtesting
        8. Summary
      14. 7. Creating Save and Load Systems
        1. Saving data with flat files
          1. Adding the required variables
          2. Time to save our file
        2. Deleting our flat files
        3. Loading our flat files
          1. Time to load our file
        4. The XML save system
          1. Creating our XML files
        5. Saving data with XML
          1. Adding the required variables
          2. Saving the player data
          3. Saving the enemy data
        6. Loading data with XML
          1. Loading the player data
          2. Loading the enemy data
        7. Creating the SaveHandler script
          1. The checkpoint system
          2. The save anywhere-anytime system
        8. Playtesting
        9. Summary
      15. 8. Aural Integration
        1. Background music
          1. Creating a random system
          2. Adding a playlist system
          3. Implementing the audio systems
        2. Atmospheric sounds
          1. Creating the script and variables
          2. Initializing the variables
          3. Playing the atmospheric sounds
        3. Sound effects
          1. Creating the script and variables
        4. Playtesting
        5. Summary
      16. 9. Game Settings
        1. Figuring out what to optimize
        2. Making video configurations
          1. Setting the values
          2. Toggling the shadows
          3. Setting the field of view
          4. Setting the resolution
          5. Toggling the anti-aliasing property
          6. Setting vsync
          7. Changing the quality settings
          8. Loading the settings
        3. Making audio configurations
          1. Setting the values
          2. Configuring the volumes
          3. Setting the speaker mode
        4. Creating the settings menu
          1. Preparing the code
          2. Creating the GUI
          3. Saving all the values
          4. Loading all the values
        5. Playtesting
        6. Summary
      17. 10. Putting It All Together
        1. Creating levels
          1. The main menu
          2. The playable level
        2. Creating player interactions
          1. Shooting and pausing
          2. Collecting potions
        3. Adding all the sounds
          1. Playing the background music
          2. Adding the atmospheric sounds
        4. Implementing the GUI
          1. Adding the script
        5. Tracking stats
          1. Adding the script
        6. Saving and loading
          1. Adding the script
        7. Final preparations
          1. Adding win conditions
          2. Affecting the AI
          3. Finalizing the items
          4. Creating more levels
        8. Playtesting
        9. Summary
      18. Index