con este texto, tienes que escribir el codigo y listar los pasos que comenta: "" so for…
- User
con este texto, tienes que escribir el codigo y listar los pasos que comenta: "" so for this tutorial we're going to look at making a simple game in unity but using some quite clever tricks um and we're going to make a color match game so basically we could match game if you have a bunch of colors scattered around and the player had to click the one you tell it to with some tricks and little bits along the way to make that life a little bit more difficult um so this tutorial is just gonna go over the basics of arrays and dictionaries um switch case statements and also remind you about functions and return values okay um so we've got a new blank music project um it's a 2d setup and we've got a new scene so we're going to add to this new series if we're going to add uh a button okay we're going to add the button um you'll notice that i've got my game set up to be a 1024x768 resolution and that's just standard resolution i have set up um you can set it wherever you want and i'm going to set my canvas scaler to get with green size essence that resolution just so that whatever i set it to i know they're going to be about right and i'm going to make my button bigger square i'm just going to go with this text um and i'm going to put it down about here um and then the button what we're going to do for the button we're going to change every time we want to create a new value we're going to change this color value okay we'll change it by code okay so don't forget that now we're going to add two more text boxes okay one we're gonna call color text um and what i'm gonna call score text um don't worry too much about those now that should be there um oops if i ignore that all right um and there we go we're just gonna we've got two let's just make them a bit bigger what we remember um set things to best fit and 120 and there we go there we go so we now have a button and two text values so we're going to create a new script on yeah all right we're gonna call it game manager okay and this game manager here okay we're going to have on our canvas we're going to open it up so we'll open it up in visual studio and we're going to have some very simple logic to start off with okay so we need to break this down so the first thing we obviously want to do is we want to make it so that when the game starts it picks and sets our buttons and we could have made if we want into random colors so there's lots of ways we can do this and say we're going to use some clever little tricks to actually get that done okay so as i said we're going to change this color um section in unity we're going to use it to change the buttons color and what about an image of color um which means that what we want is we want to start by importing in the ui tools okay who want the ui and we're going to start with the array of images so these will be all the images on all different buttons we add okay and we can start by saying images equals get component in children get components with the s in it so that will get all the components in the children of the transform this is attached to um that have an image component on it so your next step is okay we've got our images the next step is actually doing something slightly different and so what we could do we could generate random numbers um and we could use the random numbers to generate the images and to match the images to a color but actually that gets a little bit complicated because you have independent factor in repetition you don't have to do long conversions um what we're actually gonna do we're just gonna instead use another array and we're gonna use a function of arrays to actually have a array of um numbers and i've missed one right um so we've got an array there naught one two three four five six seven okay and those are going to be the numbers that we can generate okay so there's our random numbers but what we're going to do is we're going to sequence them randomly but first let's get to actually setting the colors okay so we weren't going to use update we're going to create an entire new function called set color okay so set color is going to do a little bit of uh clever thinking so what he could do he'd be gonna get given a number in that array and then define what number is um it's going to return a color okay um so to return switch me to send it back so for instance if it were given 0 it might be blue 6 might be um red or yellow okay um and we're going for four so it's that we're going to use something called a switch case so i'm going to switch back to the numbering array okay so what this says is that i we're going to depending on what that number in array that passed it is okay if it's zero we'll return color blue if it's one we'll return cyan gray green uh trying to follow this nicely and so on and so forth so let's do magenta all right [Music] yellow white i know what we need if we need a default so that what we know will be just in case for c sharps own peace of mind if the number that get given it isn't naught one two three four five six or seven um we just need a default and that'll be clear okay um we could make a default yellow but let's keep it simple okay um so this is here is going to be used to set the color we can use that to set the color for all sorts of stuff okay and setting up this as a separate function will make things a lot easier in life later because later that means that we we don't actually have to worry excuse me about um what was using the color for um whether we're using images producing for text okay um and then what we need if we need something that actually uses it so we're now going to use setup colors not technically needed okay but hey um it will use that in case okay um what we're gonna if we use link link allows us to manipulate lists and arrays um uh system.link okay um so you might not come across link for link allows you to manipulate lists and collections of objects like this um so this probably looks a bit weird but will make sense in a second um so this says take the array of numbers and order it by i where i is a random number between the range of norton which is dot length and convert that back to an array okay effectively what all that does is shuffles do you a okay so instead of not one two three four five six seven it might be six four two one etc etc um so now we have a set array a set order and way which we're gonna sequence numbers we can actually do some quite clever so we can say for each image in images so if each one of the images that we've collected image.color so that color i showed you earlier and then we can use this function here so this function that we chose we wrote here okay and this shuffled list to say set the color to be whatever number you picked we picked out your way okay um and we had it round so for each color we should pick a next number in the array nice and easy nice and straightforward so if we go back to unity and we have a canvas remember our images are empty and when we start not much happens at the moment okay really not much happens it because you can see that the images are being grabbed however none of this code so this code down here that's we've written is ever actually being used so we say go back to unity blue set each time and duration a different color okay um so we know it works so we now have um a button that we can add as many buttons if you like and they'll all change color okay what we need to do is have it so that it sets a and gets a a kind of target for us to color um and this is where it gets a little bit more complicated um it's quite difficult to change from a color to a text and back and forth so we're going to use a another c sharp function obviously somebody called dictionaries so dictionaries are like arrays in that they're lists but these are lists that are in key value pairs okay so we have a dictionary of colors okay we have the string that represents the color's name and then the actual color itself okay um and what we're going to do we're going to fill that in with creating a new dictionary and then we should add if i mine my colors the equivalent colors okay so we're gonna i'm just gonna be lazy and copy paste a couple so cyan uh uh so we have next green and then magenta and red and then why to get finished so you have now a dictionary of colors that we can match up okay and then we can go down and we can say public void setup text so we'll use this one to actually set up the text that we're going to use okay a couple of bits and pieces we probably want to do okay um we probably want to add a so we have a score we have a pick text so in a word the text says what color you're going to pick and then some text it says to score um and just bottom here i'll also add this ink we're going to be using it a second the color to pick so we'll set up our text we'll say um correct random number all right this one becomes important now what we say that the color we want to pick equals uh where do we populate colors element at okay so we pick a random element in the dictionary and get its case that's its name and then we say color to pick equals and then we get the values that's the color so that's not the color we want to pick and we set there to be a random number of colors so a random color from the list of colors and then we update score text okay so do you know a random number use that random number to pick a random color from the dictionary and assign its key so it's its name to the text of the pick text and the color of it will such value to the color pick and then set the text just to confuse our player a little bit set the text to be something um a little bit random okay i want more we've got to use a go to our script and if we color texture go in there score text is going there uh oops i forgot once again to call the code so get called there okay um so we want to click blue okay um but at the moment our button don't do anything so what we're going to do is we're going to make it set up button do something and that will be a final bit of a bit of tidying up okay so the next thing to do is to make sure that button do something and i button doing something is a fairly easy thing to check because we want to do check to see the color we've clicked on colored image we've clicked on matches that color to pick that we selected earlier if it does let's pick some more colors set and do text update the score yep nice straightforward well what's going to do here if where i put the tears is i'm just going to add this to say click that just so we've got a bit more clarity okay and then back of duty um i'm gonna click off my play i go to my own click okay drag my canvas in go to game manager okay check color and drag my button in so that now when i'm checking color so when this button pressed i'll check the color of the button that pressed it so this image here against the color that i want and what we're doing we're going to duplicate this a few times so set it here um and put some up here and i'm going to also change the background color just so i can make it a little bit nicer okay so click magenta click cyan click white okay click cyan click green and you can keep playing forever okay and that is a very very simple color match game but using some slightly more advanced programming tricks nothing too advanced to actually get you to think about setting things up a bit easier using functions using arrays using these built-in things means that actually we don't have to worry about checking every value all the time repeating code and makes what would be quite a long program substantially shorter okay um so over to you this is now your game you can do whatever you want to it you could add a time limit you could make the the colors change repeatedly over time you could add negative scores you could have a win condition or lose condition windscreens menus all sorts of different things different game modes and really built it up okay um hopefully you'll get on great um yeah please feel free to show me what you've done and hopefully this helped thank you for watching"
- Assistant