Unity Interfaces vs Abstract Classes - Part 1 - Interfaces

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I find a lot of people have trouble understanding the use / benefit of interfaces. To anyone that's interested, but struggling with this idea, here's an analogy I use when mentoring junior devs:

An interface is the blue print specification for a given class. Take an auto factory - at Toyota they build a slot for the radio to fit. When an after market company wants to build a radio, they need specifications like the height and width of the radio slot, the wiring harness to make it work with the car, and possibly an adapter to make the radio fit in the Toyota slot.

All of these constraints would go into an IRadio interface that would look something like:

interface IRadio { double Width{get;}
double Height{get;} double Length{get;}

//Wire harness Info int[,] WireHarnessLayout{get;} //Typically 2/10 or 2/12

// Position in Matrix Vector2 PowerWire1; Vector2 PowerWire2; Vector2 UpVolume; Vector2 DownVolume; //Etc... }

And so when powering on the radio, Toyota can check the values on your interface implementation, and (hopefully) shut down the power before you blow up your radio. Other things like the physical wire harness and radio spec will obviously not fit if you don't match up to Toyota, or you have to create an adapter ( just like an adapter pattern in the software world).

In software, all of this allows the concept of "loose coupling" - something that's critical for having stable, scallable software. it won't guarantee those things, but makes it much easier to do. It's part of SOLID principals, something that doesn't make it into games that often, but is everywhere in enterprise application development.

Hope that helps!

👍︎︎ 3 👤︎︎ u/solidh2o 📅︎︎ Sep 11 2017 🗫︎ replies
Captions
it what's up Jason near community 3d college in this video I want to talk about interfaces and abstract classes and give a quick rundown of the differences and how you could use them in your unity projects and to do this I think I'm just gonna go through and build out a real quick demonstration of just the code and how I would set all this kind of stuff up so first let's go with something like a maybe a vehicle like a car right so today I want to create a car in my game study a car script I'm gonna create these all in the route because it's just simple thing and I've got a car and maybe this car can take damage so one thing I could do is go to create C sharp script and make an interface and maybe call it like I gave damage right and interfaces in general with C sharp start with the uppercase I before the the name instead of like I think there's kind of the only file where there's really a very specific naming structure for him and it doesn't have to be that way technically but it's just a big common c-sharp standard so what I want to do first just change this instead of being public class I'll make it public interface and I'll get rid of everything else and then what we can do here is just define a contract for any class that wants to have this interface so if if anything has I take damage add into it and I'll show you how to do that in a second it needs to have a method on there let's call it void just call it take damage and that'll take an int for the damage amount and then which set it up just like this we don't have to declare it as public because everything in an interface is public automatically it has to be and we're just giving it a method name and an variable so let's go back over to the car class now I'm gonna fix the formatting there and if I after a remodel behavior add I take damage see what happens you get a red underline here saying the car doesn't implement the interface member I take damage take damage that's because our car now if it's going to be an take damage object it needs to actually implement the interface and have that method so just you control period implement interface and now I have the method and the compiler stops appearing right so here I can do something like you know maybe I have a health field so I do a private health and I've set it to ten no I'm gonna make this public just no I'm gonna make it private I can't do it so private and health equals ten and then I'll just do damage per sorry health minus equals damage and then you do like if health is less than or equal to zero destroy the game object so here we're just doing something really simple where we you know if this thing takes damage its health will be reduced and then it'll destroy itself all right so we've got an interface here we've got a car and you might be wondering well why I add this interface we could just have take damage here without this I take damage thing right it's right now it may seem a little bit pointless but give it just a couple more minutes and hopefully start to make some sense so now let's say we want to have a barrel right maybe barrels or something else that you can do damage to now let's add that same interface the I take damage and we'll implement the interface with control period and for this barrel we're just gonna make it get destroyed as soon as you click on it right or as soon as it takes damage so did that was to destroy game object and then this is one of the kind of key things with interfaces the they have to have the same methods and react to the same contract but how they do it can vary completely they don't ever have to do the same thing we just have to implement those same methods so here we've just got the I take damage on the barrel and let's jump back over to the editor real quick and let's create another C sharp script we'll call it a damage on click all right we've got a damage on quick and here let's do a input mouse button down zero so we're gonna check to see if they left click clean up this formatting real quick give all this extra stuff there we go so if they left click we're gonna do rayray equals camera main dot screen point array so it's just gonna get array into our scene will do camera dot no sorry we're do input mouse position so this just lets us click on things and get let's just do a ray Kasten of the scene if physics dot oops physics that raycast way comma out hit info and add the lines in here I'll just go like this oh that's not what I wanted we're gonna we want to introduce a local here we want to do hit ray cast it sorry ray cast it hidden there we go so if we're if we hit it into something hidden pose gonna have a point yeah that's gonna have a thing that we clicked on so then what we're gonna do is we're gonna look at the hit info dot Collider and we're gonna call it that get component I take damage so we'll call this I take damage damage a bowl equals that so here we're gonna what we're gonna do is anything we click on we're going to check to see if it has a dim and I take damage component and we'll assign it to damageable and if it does have one so if damageable whoops image a bowl is not equal to no star damageable dot take damage and we'll just do one damage there there like get rid of these extra lines on top save this out and then let's go into the editor and see what that does real quick so first we should create a barrel so let's go 3d object and create a cylinder will add the barrel script and then let's create one more object and we call this to damage and we're just gonna take the damage on click object save my scene and then hit play alright let's see what happens now so I click on the barrel and it's destroyed as we would expect click on the car nothing's happening that we can tell let's go to the car though and view it in debug mode and just look at the health there we go so you can see the health is six five four three two one so here we can see that we can have this one script here this one damage on click script and it's able to work with multiple different object types by using the interface so using I take damage instead of checking to see is this a car if this car take damage is it a barrel is it a box if it's a box take damage or do something different on each one of these it gives us a way to have that interaction be specific to the object and not in our damage ability or whatever ability is interacting with it so our damage on click you know imagine this is like a player's spell right you don't want the spell to have to determine you know how does this thing get destroyed how does it interact and take damage or you know like if I click on it what does it do we don't want that all in a single spot we want that spread so that the behavior is kind of specific to the object and that's exactly what setting up an interface like this does now there are some drawbacks some things that you cannot do with an interface for instance say I had a game object or a script that wants to reference something like this so I have like a let's see let's call this like spawn damage taker and say I had an object and I wanted to maybe allow it to spawn things that can take damage okay so in a normal situation you could do something like car a private car car prefab you just like that and do a serialized field and this would work right so I'd be able to go into the editor and let's create a new object call it a spawn damage taker just match the script name I could assign this here let's get out of debug mode and I'd be able to just assign a car right so I could put a car in there but if I put in I take damage this is not gonna work anymore so give it just a second you see it's gone we can't even assign anything there and the reason for that is that I take damage or an interface like this doesn't specifically have a way to tell the editor that oh this is definitely going to be you know an object or a component that can be used by the editor this could be any kind of class right it could be on anything it doesn't have to be on something that is set up to work on a game object so to do it in a way that set up you know so that you can assign these things we want to switch over to abstract classes and because this is already a little long I'm gonna split that into a second video so you can watch and see the difference with abstract classes so if you like this and you want to learn more about interfaces abstract classes or just c-sharp with unity in general don't forget to Like and hit the subscribe button thanks
Info
Channel: Jason Weimann
Views: 49,546
Rating: undefined out of 5
Keywords: Unity3D Interface, Unity Abstract Class, Unity Interface, C# Interface, Unity3D Abstract Class, Abstract Class vs Interface
Id: kYJRIWjoeFA
Channel Id: undefined
Length: 10min 10sec (610 seconds)
Published: Sun Sep 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.