Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity Certified Programmer Exam Guide

You're reading from   Unity Certified Programmer Exam Guide Pass the Unity certification exam with the help of expert tips and techniques

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781803246215
Length 766 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Philip Walker Philip Walker
Author Profile Icon Philip Walker
Philip Walker
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Chapter 1: Setting Up and Structuring Our Project 2. Chapter 2: Adding and Manipulating Objects FREE CHAPTER 3. Chapter 3: Managing Scripts and Taking a Mock Test 4. Chapter 4: Applying Art, Animation, and Particles 5. Chapter 5: Creating a Shop Scene for Our Game 6. Chapter 6: Purchasing In-Game Items and Advertisements 7. Chapter 7: Creating a Game Loop and Mock Test 8. Chapter 8: Adding Custom Fonts and UI 9. Chapter 9: Creating a 2D Shop Interface and In-Game HUD 10. Chapter 10: Pausing the Game, Altering Sound, and a Mock Test 11. Chapter 11: Storing Data and Audio Mixer 12. Chapter 12: NavMesh, Timeline, and a Mock Test 13. Chapter 13: Effects, Testing, Performance, and Alt Controls 14. Chapter 14: Full Unity Programmer Mock Exam 15. Other Books You May Enjoy Appendix

Introducing our interface – IActorTemplate

The IActorTemplate interface is what we are using to prompt damage control, death, and scriptable object assets. The reason for using an interface such as this is that it ties general uses together between classes that inherit it.

A total of six classes will be using the IActorTemplate interface, which is as follows:

  • Player
  • PlayerBullet
  • PlayerSpawner
  • Enemy
  • EnemyBullet
  • EnemySpawner

The following diagram shows the IActorTemplate interface with a partial overview of our game framework:

Figure 2.31 – IActorTemplate UML

Figure 2.30 – IActorTemplate UML

Let's create our interface and explain its content along the way:

  1. Create a script in the Assets/Scripts folder with the filename IActorTemplate.
  2. Open the script and enter the following code:
    public interface IActorTemplate
          {
          int SendDamage();
          void TakeDamage(int incomingDamage);
          void Die();
          void ActorStats(SOActorModel actorModel);
          }
  3. Make sure to save the script.

The code we just entered looks like we have declared a class, but it acts fundamentally differently. Instead of using the class keyword, we enter interface followed by the name of the interface, IActorTemplate. It's not a requirement to start any interface name with an I, but it makes the script easily identifiable.

Within the interface, we make a list of methods that act like contracts to whichever class implements them. For example, the Player script that we'll create later on in the chapter inherits the IActorTemplate interface. The Player script must declare the function names from IActorTemplate or the Player script will throw an error.

Inside the scope of the interface, we declare methods without accessors (it doesn't require private or public at the beginning of each method). Methods also don't require any content in them (that is, they are empty bodies).

For more information about interfaces, check out https://learn.unity.com/tutorial/interfaces.

The last method in our interface is ActorStats, which takes a SOActorModel type. SOActorModel is a scriptable object that we are going to explain and create in the next section.

You have been reading a chapter from
Unity Certified Programmer Exam Guide - Second Edition
Published in: May 2022
Publisher: Packt
ISBN-13: 9781803246215
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image