Refactoring the shooting mechanic into an interface is a challenge I'll leave to you, but we still need to know how to create and adopt interfaces in code. For this example, we'll create an interface that all manager scripts would hypothetically need to implement to share a common structure.
Create a new C# script in the Scripts folder, name it IManager, and update its code, as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 1
public interface IManager
{
// 2
string State { get; set; }
// 3
void Initialize();
}
Let's break down the code:
- First, it declares a public interface called IManager using the interface keyword.
- Then, it adds a string variable to IManager named State with get and set accessors to hold the current state of the adopting class.
All interface properties need at least a get accessor to compile but can have both get...