C# Interfaces in Unity! - Intermediate Scripting Tutorial

C# Interfaces in Unity! - Intermediate Scripting Tutorial

Unity

4 года назад

93,787 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

BrunoRM
BrunoRM - 01.01.2023 13:53

that explanation was really smooth, thank you very much!

Ответить
Fly_PoPa
Fly_PoPa - 26.08.2022 14:26

Unity thanks!!!

Ответить
Miller Klitsner
Miller Klitsner - 28.01.2022 21:35

thanks

Ответить
Psychologia & Gaming
Psychologia & Gaming - 22.01.2022 13:46

Excellent explanation for someone how already knows what interfaces are ;)

Ответить
John W
John W - 07.12.2021 14:10

The exampLe at the end really helped!

Ответить
diliup gabadamudalige
diliup gabadamudalige - 06.11.2021 10:20

nicely explaind! The dislikes are from the ones who didn't understand a word :-) seriously! :-D

Ответить
ColdOneK
ColdOneK - 31.08.2021 10:14

This tutorial doesn't tell how to call.

Ответить
Jacques Oliveira
Jacques Oliveira - 12.06.2021 15:59

can I use properties inside interfaces? is it a solid principles?

Ответить
mehrdad
mehrdad - 14.05.2021 20:18

Very well explained with great clarifying examples, Thank you Unity ❤️

Ответить
nazxuul
nazxuul - 02.02.2021 06:14

Good explanation, but I'm not sure how this ties in with Unity. For example, can I use GetComponent to find the component that implements a particular interface? I assume not, which kind of limits the usefulness of interfaces in Unity.

Ответить
ploopploop
ploopploop - 11.12.2020 19:18

So simple yet so effective!

Ответить
Aaron Rumley
Aaron Rumley - 09.12.2020 06:49

excellent video! If someone could clarify something for me, that would be great! relatively beginner programmer here, but not a total rookie.

So essentially, if an ICollectable interface has a Collect() method, then any class that implements ICollectable has to define something for the Collect() method.

Lets say the Coin class implements ICollectable.
So my question is, the body for Collect() could really be anything at all? If thats the case, my mind immediately thinks - "how helpful can the methods in an interface be, if each class defines the methods differently?" To put it another way, whats the point of an interface having all these methods, if each class that implements the interface has it's own unique definition for that method?

@DanBrian, sounds like you may have some insight here.

Ответить
Osman AKTAS
Osman AKTAS - 12.11.2020 17:58

thanks a lot . Awesome explanation

Ответить
Subliminal Castillo
Subliminal Castillo - 05.10.2020 01:35

A good practical example for why interfaces are important:

Say I have a 2D platformer game.

Every single creature/entity in the game (player, enemy) all have a "generic" Movement class that controls their horizontal movement. (since jump is handled by applying a sudden force vertically, using a (-1, 0, +1) axis base is going to be ignored for the purposes of this.

The movement class has a function inside of it called "MoveHorizontally"

"MoveHorizontally" takes in a parameter of the type "float" called "HorizontalMovementDirection"
and inside of the function, it uses HorizontalMovementDirection to define the x-axis velocity of the Rigidbody2D.

I now create an interface called IHorizontalMovementInput, that has 1 field (variable) inside of it: float HorizontalMovementDirection { get; }

I can now create 2 seperate classes that "implement" that interface.

HorizontalMoveInput_Keyboard : Monobehaviour, IHorizontalMovementInput
& HorizontalMoveInput_EnemyAI : Monobehaviour, IHorizontalMovementInput

Inside of HorizontalMoveInput_Keyboard, I create the horizontalmovementdirection float and inside of the update method set it to Input.GetAxisRaw("Horizontal");
Inside of the HorizontalMoveInput_EnemyAI, I create an enemy AI that can tell how close the player is, using Vector2.distance, and set it to Vector2.MoveTowards if it is within a certain range.

Now, back to the original Movement class, I create a reference to that interface:
IHorizontalMovementInput horizontalMovementInput;
and in the update method, I call the MoveHorizontally function & pass in the horizontalMovementDirection from the horizontalMovementInput interface.

This allows you to set up one common functionality (Moving the game object horizontally) and provide different implementations on HOW that thing moves horizontally.

This is a very powerful tool, that allows you to keep your code very modular, and reusable, instead of copying and pasting the same logic class-to-class, and it also makes it very easy to implement new features without having to change the code that you have already written.

Ответить
DanBrian
DanBrian - 12.07.2020 20:01

One of the most underrated functions of an interface is, that you can find it with GetComponent<IyourInterface>(); This is very powerful. For example, your Cat, Dog, Horse classes inherit the interface Ifeedable. Your Bowl class' OnTriggerEnter then looks if the entered gameObject inherits from Ifeedable with "Ifeedable feedableObject = other.GetComponent<Ifeedable>();" and "if (feedableObject != null)" then you can start your feeding logic. The Bowl does NOT need to know which animals exists and you therefore do not need to extend anything in the Bowl code logic, if you add more animals. This is effectively a better Tagging system

Ответить
ImSamhel
ImSamhel - 18.06.2020 15:03

That explanation in the end makes everything clear. 200+ IQ

Ответить
LeatherAndPoems CharmyMan
LeatherAndPoems CharmyMan - 24.05.2020 04:06

how to use it! ? because I can not drag the interface to the hierachy? because is not a class and has not monobehaviour? I undesrtand the example...but... I can not see anything , not errors, I got only 2 cubes (car and wall) and the scripts for interface.cs... thx btw.

Ответить
Abul Amad
Abul Amad - 19.05.2020 02:35

Let's imagine the IKillable interface, this interface implements some functions that tell that this object can be killed, however, you don't know how will they die. Does this object die normally? Is it a boss that when killed, a new section of the map is opened? Is it even a box that when destroyed it gives some items? You really don't care about the object that implements this interface, you only care that this object can be killed/destroyed.

Ответить
Jovan Novakovic
Jovan Novakovic - 30.04.2020 22:41

Warcraft III tutorial narrator

Ответить
BurritoInTheMist
BurritoInTheMist - 25.03.2020 23:47

So, as I understand this, Interfaces allow you to create your own public voids, just as Monobehaviour automatically has void Start() and void Update() when you start a new script?


MUST you utilize every function in the interface wherever it is implemented? If I have (spit-balling here) Interface ITalkable with functions void Dialog(), void CameraCuts(), and void CharacterResponses(), would every single object I have that uses the ITalkable interface (see for instance, a wall that you can examine), require all those functions?

Ответить
Hermetes
Hermetes - 02.03.2020 14:03

No words about , how can i call a interface :/

Ответить
RAPHAEL SC
RAPHAEL SC - 06.02.2020 04:27

Wow. Awsome answer in the end.

Ответить
ohaRega
ohaRega - 03.02.2020 20:22

Stellar explanation

Ответить
anaїs lake
anaїs lake - 17.01.2020 21:41

homeschool mom and 16 yo. ✅🍀

Ответить
Sasuke Uchiha
Sasuke Uchiha - 19.11.2019 19:27

How do you make a reference to the interface from another object in order to call these methods? Do you use a GameObject variable and call the method on that?

Ответить
theroom theroom
theroom theroom - 06.11.2019 16:01

well explained

Ответить
UGuruz
UGuruz - 19.07.2019 05:54

Explained very well...👍

Ответить
Bes2TheFort
Bes2TheFort - 18.07.2019 23:48

Hi Unity

Can you show my game on your channel. I made it with Unity and its called Pandea Stones which is available for ios and android

Ответить
GamesCreator
GamesCreator - 18.07.2019 19:59

Thanks a lot for this exaple

Ответить
Itai Dhaliwal
Itai Dhaliwal - 18.07.2019 18:56

first

Ответить