 All right welcome back to the top-down camera tutorial brought to you by IndiePixel All right, so in the last video we set up our editor tool, which is fun made it nice and usable So in this one, I want to make it so that it when we go and need a new camera We can actually set this guy up properly. So Or we can have a menu that allows us to set up. So let's jump over into Unity All right, so here we are inside of Unity. So what we want to do I want to actually make another script here and we're just going to call this the camera Let's say IP camera menus our menu All right Something like that All right, so we're going to pop this open here And again, I'll put it under the proper namespace. I'm going to say namespace And this is going to be IndiePixel Dot cameras like so And we do need to be using the Unity editor namespace for this and what I'm going to do is Create some public static. So first I want to make a menu item. So we're going to say menu item Like so and I'm going to say IndiePixel All right forward slash we'll say cameras and this one's going to be top-down top-down camera Okay, and then we want a public static void create top-down camera like so And because we have this menu item above it, it'll fire off the function below it. So to prove that we're going to do a debug.log All right, and we're going to say creating Top-down camera Like so and there we go. So we get the menu item up here cameras Boom creating top-down camera. So the first thing that we want to do when we when we go and fire off that function, right? I want to check to see if I have anything selected, right? Because I mean the script itself isn't going to care what that thing is I just want to know that I have a game object selected so I can attach This script to that object and assign that selected object to the target property of this particular Component all right So we can say or maybe what if we did something like this where we say, okay I want to attach there's my camera There's my target and what it does is it uses that information to set up the whole system. So Maybe we do it like that Okay, let's actually try that out Alright, so in order to do that what we want to do is we want to say game objects when we create an array a game object So we're going to we're going to be we're going to say selected Geos that's what I like to just say I've selected Geo. How about that equals selection dot objects? Game objects. There you go This returns the array of all the selection. So let's Do that All right, cool, and then let's actually do a for loop here. So we're gonna say for each Hold on there we go for each bar selected and selected Geo We could always Just use selection, but I want to store all these guys. So let's do a debug log Selected dot name. I just want to kind of do a quick Peak and to see what we what kind of information we get we should get to game objects when we select things, right? So let's fire off that function There we go. Thank Cameron tank so This is what we want to do. We want to tell the the user here. Okay? All right, so I don't necessarily need that just yet, but I'm gonna just comment it out So we want to tell the user like okay You have two object selected, but imagine a situation where a user might be like it might have selected three objects Okay, by accident. So we want to give them some information, right about that selection process. So What we're gonna do is we're gonna say, okay, if you only have one object selected Then I'll just attach the script to it and there won't be any target setup But if I have two objects, right and then maybe after that process will pop up a little display dialog and say hey You know, we set up the script, but there was no target attached But if I have two object selected, I'll take the last one and say that that's the target the last in the array, right? so length minus one and I'll say hey everything went well we set up the script in the target and if there's three objects selected I Might just make it so that it just bails completely and say hey, please only select two objects You want to create those types of rules, right for your tools? Okay in your menus? All right, so let's do that. So we're gonna say if Select geo dot length is Less less than two All right I'm gonna make another function here that just does the attachments process So we're gonna say attach Top-down script like so And I don't need it either and I'm gonna pass in a game object So game object camera and Game object target I do like to do this to a target a Camera, so if it's less than two then we we have an object, right? Now first we need to check to make sure it's greater than zero if Selected geo dot length is greater than zero then we're cool so we can proceed All right Else what we're gonna do is we're gonna say Editor utility dot display dialogue and we're gonna say Camera tools for the title and for the description. It's gonna be You need to select a Game object in the scene to assign to And I just do it. Okay. Okay There we go All right, so we can actually test that out already. So let's go make sure that that works So now if I try to do this You need to select a game object in the scene to assign to all right, and then if I were to select the main camera Perfect. We didn't get anything. So so for lesson two we're gonna attach attach top-down script Attach That's oh it needs to be static. Sorry. All right, because it's inside of a static function. So There we go attach top-down script and we are going to pass in In this case No, yeah, we're good Yeah, we're good. So what we're gonna do is we're gonna pass in Selected geo and We're gonna do the first one for Target because we know we have that Because if we're less than two we know we have an object in the zero index of this array But we don't have anything in the one index. So That means that I don't I didn't select the target. So I just want to attach the script to the camera that I have selected Okay Perfect, and then I'm just gonna pass in null for that one And then I'm gonna say else if I Know there's tons of ways you can set this all up. I'm just Showing you guys a way to do this you could also even say like check to make sure that the first object in the list here Has a camera component on it already else. We're totally gonna bail kind of thing How about we do that so I just thought of that so Zero dot get component Camera if that's true, then we could go through it, right? I forgot one part Then we can go through this whole process else if the length is equal to two Oh Sorry, and yeah, what I was doing I set it up. So now it's checking to see if there's camera component on the first selection else It's It's gonna say you need to select a game object in the scene to assign to So now we need a little bit more information, right? So now we need to say you need to select a game object in The scene that has a camera component assigned to it There we go, I don't think it too long you can always just hit plus and it will add it on there automatically like so All right, so now that should work. So let's try that out. Let's debug that before you go any further and make sure that Oh, sorry That game object There we go So I'll fix the game object error there Nope. Oh, it's right here. Oh Well, there we go My bad. It's getting late There we go, okay So let's say we select something like the tank radical that doesn't have a camera script on it So if I were to go here, hey, we need to find the sign that so if I were to assign just Or select the camera because it has a camera component on it It should assign another top-down camera script to this now And it doesn't just yet. Oh That's because I Haven't filled out this thing yet. So Let's actually make that work. Sorry. I'm getting ahead of myself over here Okay, so now that we we're gonna check to say we're gonna say if a camera Right because it's game object that it derives from I know a ball meaning that you can check to see if it's true or false as by just saying a camera or Game object, right? Basically. So if a camera then what we're gonna do is we're gonna say I want to create a variable first and this is gonna be a type of Transform. No, it's gonna be the script itself. So yeah IP top-down camera. Yeah, and we're gonna say camera script And we'll just leave it null for now, right? Okay So then what we'll do is we'll say camera script equals a camera dot add component IP top-down camera boom Okay, so then What we'll do is we'll make a little note for our sign ourselves. So we'll sign the top-down Script to the camera cool And then what we want to do is we want to check to see if we have a target and we Have a script reference Okay, so if we have if this succeeded and this is not null then what we could do is assign the target to that script Boom, and that basically completes the auto setup right Okay, so now we're gonna say if camera script All right and a target So we have both those things we're gonna say camera script Dot and target is equal to a target Dot transform because it's of type transform. So this is of type transform And because this came in as a game object. So basically I could just Do transform and We'll just do that and we'll do the conversion up here. So Now I can use the same function to say attach top-down script. We could send in that selected go zero dot game object and We can take the second object and we can say that's the target. So I'm gonna say selected go one Dot transform There we go. So then what we want to do is we want to say Actually, we'll just do the check for three We'll say and we'll do it in here. We'll say selected go dot length is Less than three Then we're cool else Actually, no, you know, that's why I like to put it on the inside here because it won't give you enough information right because the The display dialogue for this particular condition tells you that you need to have something selected But if I have three things selected I have a ton of things selected So I need to do another else if It's less or it's greater than three Or is equal to three how about that? Yep Then let's say Something like You can only select two objects or game objects in the scene for this to work and The first Selection needs to be a camera Okay, and again, I'll just Concatenate that there. All right Perfect. So let's check all this out now No air is very cool. So let's go and talk down camera boom That's just because I had just the main camera setup. So now let's remove that and Check it. So I'm gonna select the camera tank and that will select the camera here. Boom. Perfect setup. Oh Good to go. So let's get rid of some. I'll just clean this up here. So Oh, I don't have any more debug logs. That's good Perfect so you could do a lot of other things here you can check to see if the top-down script already exists on that particular component I Think the last thing to clean this up just to make it a little easier I don't need this for each loop anymore. I think Okay, so if we are successful what I want to do is just select The camera so I'm gonna come down here in this attach top-down script and we're going to say Selection.active game object Active game object is equal to a camera And let's just do this There we go Perfect. And that is really what I wanted to cover in this particular course So now we have an automated way to set up the camera. So if I just remove all this and Go cameras top-down camera boom So now we can select the camera adjust the height Like so And the distance There we go, and we are functioning Perfect. All right, so I'll leave you guys with that. Thanks so much for watching. We'll talk to you later