Комментарии:
that explanation was really smooth, thank you very much!
ОтветитьUnity thanks!!!
Ответитьthanks
ОтветитьExcellent explanation for someone how already knows what interfaces are ;)
ОтветитьThe exampLe at the end really helped!
Ответитьnicely explaind! The dislikes are from the ones who didn't understand a word :-) seriously! :-D
ОтветитьThis tutorial doesn't tell how to call.
Ответитьcan I use properties inside interfaces? is it a solid principles?
ОтветитьVery well explained with great clarifying examples, Thank you Unity ❤️
Ответить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.
ОтветитьSo simple yet so effective!
Ответить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.
thanks a lot . Awesome explanation
Ответить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.
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
ОтветитьThat explanation in the end makes everything clear. 200+ IQ
Ответить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.
Ответить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.
ОтветитьWarcraft III tutorial narrator
Ответить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?
No words about , how can i call a interface :/
ОтветитьWow. Awsome answer in the end.
ОтветитьStellar explanation
Ответитьhomeschool mom and 16 yo. ✅🍀
Ответить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?
Ответитьwell explained
ОтветитьExplained very well...👍
Ответить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
Thanks a lot for this exaple
Ответитьfirst
Ответить