官术网_书友最值得收藏!

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 theIActorTemplateinterface with a partial overview of our game framework:

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

  1. Create a script in theAssets/Resources/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);
}
  1. 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 inherits the interface. For example, thePlayerscript that we'll create later on in the chapter inherits theIActorTemplate 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 type ofSOActorModel.SOActorModelis a scriptable object that we are going to explain and create in the next section.

主站蜘蛛池模板: 济阳县| 元阳县| 新郑市| 鄂州市| 讷河市| 淳安县| 聊城市| 大连市| 铁岭市| 莱州市| 潮安县| 沙坪坝区| 维西| 福建省| 梁平县| 湖南省| 石渠县| 当雄县| 报价| 伊吾县| 拜城县| 卫辉市| 仪陇县| 鸡西市| 镇宁| 定安县| 隆回县| 梅河口市| 楚雄市| 宁安市| 南郑县| 建阳市| 北辰区| 古蔺县| 门源| 望城县| 濉溪县| 墨玉县| 洮南市| 遂宁市| 阿勒泰市|