You are previewing Unity 4.x Cookbook.
O'Reilly logo
Unity 4.x Cookbook

Book Description

Maximize the potential of Unity 4 with this wide-ranging book. Your games will look better, play more realistically, and communicate with more devices through applying these fully explanatory recipes.

  • A wide range of topics are covered, ranging in complexity, offering something for every Unity 4 game developer

  • Every recipe provides step-by-step instructions, followed by an explanation of how it all works, and alternative approaches or refinements

  • Book developed with the latest version of Unity (4.x)

  • In Detail

    Covering the latest version (Unity 4) of this established game engine, the Unity 4.x Cookbook explores a wide range of 3D, animation, multimedia, and scripting game features.

    The power of Unity 4 is demonstrated through the 10 chapters covering many of this game engine’s features.

    "Unity 4.x Cookbook" helps you learn how to make the most of the powerful but easy-to-use Unity 4 game engine.

    Every Unity game developer is different. Some come from a multimedia background, some are new to game development, and some are transferring from other engines. Whatever your background, with the breadth and depth of topics covered you should find new features and techniques to enhance your next game.

    This book offers detailed, easy-to-follow, step-by-step recipes helping you master a wide range of Unity game features.

    Chapters are provided focusing on the basics; audio, cameras, images, materials and animations, GUIs, external resources and devices, directional and artificial intelligence character control, and performance optimization.

    "Unity 4.x Cookbook" provides a collection of ideas and resources that should offer new and useful techniques for every Unity game developer to enhance their next game.

    Table of Contents

    1. Unity 4.x Cookbook
      1. Table of Contents
      2. Unity 4.x Cookbook
      3. Credits
      4. About the Authors
      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. Getting Started with Unity 4.x
        1. Introduction
        2. Installing Unity 4.x
          1. Getting ready...
          2. How to do it...
          3. There's more...
            1. Acquiring new licenses
        3. Setting your preferences
          1. How to do it...
          2. There's more...
            1. Changing the editor's player quality settings
          3. See also
        4. Understanding and optimizing the User Interface
          1. How to do it...
          2. There's more...
          3. See also
        5. Saving assets created in Unity as Prefabs
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Adding external files
            2. Taking your Prefabs to another project
            3. Creating other kinds of game objects
          4. See also
        6. Discovering Unity's content
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Studying the sample project
            2. Downloading more resources
          4. See also
        7. Importing your own content
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Organizing it with the Project view
            2. Exporting your assets to Unity 4.x
          4. See also
        8. Importing Unity packages into your project
          1. How to do it...
          2. How it works...
          3. There's more...
          4. See also
        9. Importing custom packages into your project
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        10. Exporting custom packages from your project
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        11. Adding custom packages to Unity's quick list
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        12. Using the Project browser
          1. Getting ready
          2. How to do it…
          3. How it works...
          4. See also
      9. 2. Using Cameras
        1. Introduction
        2. Creating a picture-in-picture effect
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Making the picture-in-picture proportional to the screen's size
            2. Changing the position of the picture-in-picture
            3. Preventing the picture-in-picture from updating on every frame
          5. See also
        3. Switching between multiple cameras
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using a single-enabled camera
            2. Triggering the switch from other events
          5. See also
        4. Customizing the lens flare effect
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Making textures from screen content
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Applying your texture to a material
            2. Using your texture as a screenshot
        6. Zooming a telescopic camera
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Making an inspect camera
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Creating particle effects using Shuriken
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Scrubbing back and forward through the particle animation
            2. Learning more from the Unity documentation
            3. Adding effects through Particle Effect
        9. Displaying a mini-map
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using Render Texture (Pro only)
            2. Adapting your mini-map to other styles
          5. See also
      10. 3. Creating Maps and Materials
        1. Introduction
        2. Creating a reflective material
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using six different texture maps
            2. Experimenting with mapping and shader options
            3. Reflecting actual scene geometry
        3. Creating a self-illuminated material
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using independent maps
            2. Emitting light over other objects
          5. See also
        4. Creating specular texture maps
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Changing the specular color of our material
            2. Adjusting shininess to focus the highlight
          5. See also
        5. Creating transparency texture maps
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using Cutouts
            2. Applying Bumped Diffuse
          5. See also
        6. Using cookie textures to simulate a cloudy outdoor
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Creating Spot Light cookies
            2. Creating Point Light cookies
        7. Creating a color selection dialog
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Combining textures in real time through the GUI
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        9. Highlighting materials at mouse over
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Highlighting with self-illuminated shaders
            2. Using transparent shaders
        10. Animating textures by looping through array of materials (for example, simulated video)
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using materials rather than Texture2D images
          5. See also
        11. Disabling culling for a material
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Learning more about shaders
            2. Solving the problem by editing the object's geometry
          5. See also
      11. 4. Creating GUIs
        1. Introduction
        2. Displaying a digital clock
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Converting to a 12-hour clock
          4. See also
        3. Displaying an analogue clock
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Configuring a screen corner clock camera
          4. See also
        4. Displaying a compass to show player direction
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Displaying a radar to indicate relative locations of objects
          1. Getting ready
          2. How to do it...
          3. How it works...
            1. Displaying radar in the OnGUI() method
            2. Displaying radar object icons in the DrawBlip()method
          4. There's more...
            1. Different colored "blips" for different objects
          5. See also
        6. Displaying images for corresponding integers
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Choosing between GUI layout alternatives: GUILayout versus GUI
          5. See also
        7. Displaying images for corresponding floats and ranges
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Cleaning up code with an array of textures
          5. See also
        8. Displaying a digital countdown timer
          1. How to do it...
          2. How it works...
          3. See also
        9. Displaying a countdown timer graphically (5, 4, 3, 2, 1 – blast off)
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        10. Displaying a countdown timer graphically as a pie-chart style clock
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Creating your own images with a spreadsheet
          5. See also
        11. Creating a message that fades away
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Facilitating several messages by using a prefab
            2. Improving efficiency by re-using a single GUIText message
        12. Displaying inventory texts for single object pickups
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Comparing the gameObject tag with a string special method
          5. See also...
        13. Displaying inventory icons for single object pickups
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        14. Managing inventories with a general purpose PickUp class
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Responding to non-display pickups
            2. Removing items from the List<> collection
          5. See also
        15. Controlling the scrollbar with the mouse wheel
          1. How to do it...
          2. How it works...
          3. There's more...
        16. Implementing custom mouse cursor icons
          1. Getting ready
          2. How to do it...
          3. How it works...
      12. 5. Controlling Animations
        1. Introduction
        2. Configuring a character's Avatar and Idle animation
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        3. Moving your character with Root Motion and Blend Trees
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        4. Mixing animations with Layers and Masks
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Overriding Root Motion via script
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        6. Adding rigid props to animated characters
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        7. Making an animated character throw an object
          1. Getting ready
          2. How to do it...
          3. How it works...
        8. Applying ragdoll physics to a character
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        9. Rotating the character's torso to aim
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
      13. 6. Playing and Manipulating Sounds
        1. Introduction
        2. Matching audio pitch to animation speed
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Changing the Animation / Sound Ratio parameter
        3. Adding customizable volume controls
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using a single volume bar
            2. Bypassing the Esc key
          5. See also
        4. Simulating a tunnel environment with Reverb Zones
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Attaching the Audio Reverb Zone component to audio sources
            2. Making your own Reverb settings
        5. Preventing the AudioClip from restarting if already playing
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Waiting for audio to finish before auto-destructing an object
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Making a dynamic soundtrack
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Hiding the triggers
            2. Dealing with audio file formats and compression rates
            3. Using 2D sound
      14. 7. Working with External Resource Files and Devices
        1. Introduction
        2. Loading external resource files – by Unity Default Resources
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Loading text files with this method
            2. Loading and playing audio files with this method
          5. See also
        3. Loading external resource files – by manually storing files in Unity's Resources folder
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Avoiding cross-platform problems with Path.Combine() rather than "/" or "\"
            2. Loading a text file using this approach
            3. WWW and resource contents
          5. See also
        4. Loading external resource files – by downloading files from the Internet
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        5. Saving and loading player data – using static properties
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Saving and loading player data – using PlayerPrefs
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        7. Saving screenshots from the game
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        8. Control characters in Unity with the Microsoft Kinect using the Zigfu samples
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        9. Animating your own characters with the Microsoft Kinect controller
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Completing your block character
          5. See also
        10. Homemade mocap by storing movements from the Microsoft Kinect controller
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Recording movements of multiple body parts
          5. See also
        11. Setting up a leaderboard using PHP/MySQL
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Extract the full leaderboard data as XML for display within Unity
            2. Using secret game codes to secure your leaderboard scripts
          5. See also
      15. 8. Working with External Text Files and XML Data
        1. Introduction
        2. Loading external text files using the TextAsset public variable
          1. Getting ready
          2. How to do it...
          3. How it works...
        3. Loading external text files using C# file streams
          1. Getting ready
          2. How to do it...
          3. How it works...
        4. Saving external text files with C# file streams
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Choosing the Data or the Resources folder
        5. Loading and parsing external XML files
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Retrieving XML data files from the web
        6. Creating XML text data manually using XMLWriter
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Adding newlines to make XML strings more human readable
            2. Making data class responsible for creating XML from list
        7. Creating XML text data automatically through serialization
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Protecting member properties with accessor methods
        8. Creating XML text files – saving XML directly to text files with XMLDocument.Save()
          1. How to do it...
          2. How it works...
      16. 9. Managing Object States and Controlling Their Movements
        1. Introduction
        2. Controlling cube movement through player controls
          1. How to do it...
          2. How it works...
          3. See also
        3. Controlling object look-at behavior
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        4. Controlling object-to-object movements (seek, flee, follow at a distance)
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using the simpler (and less sophisticated) MoveTowards() method
            2. Improving arrival behavior through deceleration when near target
            3. Implementing flee target and follow at a distance
          5. See also
        5. Controlling object group movement through flocking
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        6. Firing objects by instantiation with forward velocity
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Addressing efficiency issues with FindGameObjectsWithTag()
          5. See also
        7. Finding a random spawn point
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        8. Finding the nearest spawn point
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        9. Following waypoints in a sequence
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. See also
        10. Managing object behavior with states
          1. How to do it...
          2. How it works...
          3. See also
        11. Managing complex object behavior with the state pattern
          1. How to do it...
          2. How it works...
          3. See also
      17. 10. Improving Games with Extra Features and Optimization
        1. Introduction
        2. Pausing the game
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Learning more about quality settings
          5. See also
        3. Implementing slow motion
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Adding Motion Blur (Unity Pro only)
            2. Creating sonic ambience
          5. See also
        4. Implementing 3D stereography with polarized projection
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Using Render to Texture (Unity Pro only)
            2. Using anaglyph resources
            3. Using NVidia 3D Vision
            4. Acquiring active stereo third-party software
        5. Preventing your game from running on unknown servers
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Improving security by using full URLs in your Domain List
            2. Allowing redistribution with more domains
        6. Identifying performance "bottlenecks" with code profiling
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Unity Pro only – code profiling
        7. Reducing the number of objects by destroying objects at a "death" time
          1. How to do it...
          2. How it works...
          3. See also
        8. Reducing the number of enabled objects by disabling objects whenever possible
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Another common case – only enable after OnTrigger()
            2. Going one step further – make the parent GameObject inactive
          4. See also
        9. Improving efficiency with delegates and events (and avoiding SendMessage!)
          1. How to do it...
          2. How it works...
        10. Executing methods regularly but independent of frame rate with coroutines
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Have different actions happening at different intervals
          4. See also
        11. Spreading long computations over several frames with coroutines
          1. How to do it...
          2. How it works...
          3. There's more...
            1. Retrieving the complete Unity log text files from your system
          4. See also
        12. Caching, rather than component lookups and "reflection" over objects
          1. Getting ready
          2. How to do it...
          3. How it works...
      18. 11. Taking Advantage of Unity Pro
        1. Introduction
        2. Dynamically focusing objects with Depth of Field
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
          5. See also
        3. Creating a rearview mirror
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Adding details
            2. Using Render Texture as a TV screen
        4. Playing videos inside a scene
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
        5. Simulating underwater ambience with audio filters
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Keeping the Cutoff Frequency unchanged
            2. Adding more filters
          5. See also
        6. Loading and playing external movie files
          1. Getting ready
          2. How to do it...
          3. How it works...
          4. There's more...
            1. Loading videos from the Internet
      19. Index