Unity 2021 Cookbook
eBook - ePub

Unity 2021 Cookbook

Over 140 recipes to take your Unity game development skills to the next level, 4th Edition

Matt Smith, Shaun Ferns

Share book
  1. 816 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Unity 2021 Cookbook

Over 140 recipes to take your Unity game development skills to the next level, 4th Edition

Matt Smith, Shaun Ferns

Book details
Book preview
Table of contents
Citations

About This Book

Discover the latest features of Unity 2021 and dive deeper into the nuances of professional game development with Unity

Key Features

  • Discover the latest features of Unity 2021 including coverage of AR/VR development
  • Follow practical recipes for better 2D and 2D character development with Unity GameKits
  • Learn powerful techniques and expert best practices in building 3D objects, textures, and materials

Book Description

If you are a Unity developer looking to explore the newest features of Unity 2021 and recipes for advanced challenges, then this fourth edition of Unity Cookbook is here to help you.

With this cookbook, you'll work through a wide variety of recipes that will help you use the essential features of the Unity game engine to their fullest potential. You familiarize yourself with shaders and Shader Graph before exploring animation features to enhance your skills in building games.

As you progress, you will gain insights into Unity's latest editor, which will help you in laying out scenes, tweaking existing apps, and building custom tools for augmented reality and virtual reality (AR/VR) experiences. The book will also guide you through many Unity C# gameplay scripting techniques, teaching you how to communicate with database-driven websites and process XML and JSON data files.

By the end of this Unity book, you will have gained a comprehensive understanding of Unity game development and built your development skills. The easy-to-follow recipes will earn a permanent place on your bookshelf for reference and help you build better games that stay true to your vision.

What you will learn

  • Discover how to add core game features to your projects with C# scripting
  • Create powerful and stylish UI with Unity's UI system, including power bars, radars, and button-driven scene changes
  • Work with essential audio features, including background music and sound effects
  • Discover Cinemachine in Unity to intelligently control camera movements
  • Add visual effects such as smoke and explosions by creating and customizing particle systems
  • Understand how to build your own Shaders with the Shader Graph tool

Who this book is for

If you're a Unity developer looking for better ways to resolve common recurring problems with recipes, then this book is for you. Programmers dipping their toes into multimedia features for the first time will also find this book useful. Before you get started with this Unity engine book, you'll need a solid understanding of Unity's functionality and experience with programming in C#.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Unity 2021 Cookbook an online PDF/ePUB?
Yes, you can access Unity 2021 Cookbook by Matt Smith, Shaun Ferns in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in C#. We have over one million books available in our catalogue for you to explore.

Information

Year
2021
ISBN
9781839219276
Edition
4
Advanced Topics - Gizmos, Automated Testing, and More
This chapter will put together four sets of advanced recipes:
  • Gizmos
  • Saving and loading game data
  • Automated testing
  • An introduction to Unity Python
Let's take a look at each in this introduction!
Gizmos
Gizmos are another kind of Unity editor customization. Gizmos are visual aids for game designers that are provided in the Scene window. They can be useful as setup aids (to help us know what we are doing) or for debugging (understanding why objects aren't behaving as expected).
Gizmos are not drawn through Editor scripts, but as part of the Monobehaviour class, so they only work for GameObjects in the current scene. Gizmo drawing is usually performed in two methods:
  • OnDrawGizmos(): This is executed every frame or editor window repaint, for every GameObject in the Hierarchy window.
  • OnDrawGizmosSelect(): This is executed every frame, for just the/those GameObject(s) that are currently selected in the Hierarchy window.
Gizmo graphical drawing makes it simple to draw lines, cubes, and spheres. More complex shapes can also be drawn with meshes, and you can also display 2D image icons (located in the Project folder: Assets | Gizmos).
Several recipes in this chapter will illustrate how Gizmos can be useful. Often, new GameObjects created from editor extensions will have helpful Gizmos associated with them.
Saving/loading data at runtime
When loading/saving data, it is important to keep in mind the data types that can be used. When writing C# code, our variables can be of any type permitted by the language, but when communicated by the web interface, or to local storage using Unity's PlayerPrefs class, we are restricted in the types of data that we can work with. When using the PlayerPrefs class, we are limited to saving and loading integers, floats, and strings. We will provide several recipes illustrating ways to save and load data at runtime, including the use of static variables, PlayerPrefs, and a public class called TextAsset containing text-format data for a 2D game level.
PlayerPrefs offers a great, cross-platform way to store persistent data locally for Unity games. However, it is also very easy to hack into, so sensitive or confidential data should not be stored using this technique. For securely storing data online in hashed/encrypted format, data storage methods should be used. But for data such as lives left, checkpoints reached, scores and time remaining, and so on, it is an easy and simple way to implement memory after a scene has been exited.
Automated testing
For a very simple computer program, we can write code, run it, enter a variety of valid and invalid data, and see whether the program behaves as we expect it to. This is known as a code-then-test approach. However, this approach has several significant weaknesses:
  • Each time we change the code, as well as run new tests relating to the code we are improving, we have to run all the old tests to ensure that no unexpected modified behaviors have been introduced (in other words, our new code has not broken another part of our program)
  • Running tests manually is time-consuming.
  • We are relying on a human to rerun the test each time. However, this test may be run using different data, some data may be omitted, or different team members may take a different approach to run tests.
Therefore, even for simple programs (and most are not simple), some kind of fast, automated testing system makes a lot of sense.
There is an approach to software development called test-driven development (TDD), whereby code is only written until all tests pass. So, if we want to add or improve the behavior of our game program, we must specify what we want in terms of tests, and then the programmers write code to pass the tests. This avoids a situation whereby programmers write code and features that are not needed, spend time over-optimizing things that would have been fine, and so on. This means that the game development team directs its work toward agreed goals understood by all since they have been specified as tests.
The following diagram illustrates the basic TDD in that we only write code until all tests pass. Then, it's time to write more tests:
Figure 13.1 – Test then code to the test
Another way that TDD is often summarized is as red-green-refactor:
  • Red: We write a new test for which code is needed, so initially, our test fails (in other words, we write a test for the new feature/improved behavior we wish to add to our system).
  • Green: We write code that passes the new test (and all the existing ones).
  • Refactor: We (may...

Table of contents