Setup Photon - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey this is burleth with knox game studios and today we are going to be starting our series on implementing multiplayer into unity using photon and playfab so first thing is we need to do is set up photon in our unity project so here if you go to photonengine.com you'll be able to set up an account so let's just go sign in or you'll need to try photon for free so i'm going to go sign in select my accounts remember me i'm not a robot and now let's sign in so this is the dashboard that you'll sign into if you are new to photon and you just created account you won't see any other applications here so all you need to do is go create an app and we will use for the photon type we will do photon pun for the application name we will just do nox i will do you can choose whatever you want game studios tutorial and we'll say this is a tutorial using photon and unity you can put a url but it is optional and all we have to do is now is hit create so this will create a new block here that has an app id this app id is important and you should copy it now because we will use it later on when we install photon into unity but here you can go to analyze manage so under manage you can do a few different things you can set like the custom server um if you want to use steam facebook et cetera but this is the part that i usually make a change to it's this allow regions and here i i might allow a specific region or two like eu or us or whatever you want there's documentation on the different abbreviations for the regions you want to use so now i've created a unity project using 2019.4.8 f1 this is a long term supported version of unity that allows you to have this dark mode so the first thing we're going to do is in this sample scene let's just do save as and under i created a project underscore folder and this is where i'm going to create everything that's for our projects specifically so i'm going to do a scenes and i'm going to do main menu and now i'm going to go to under assets i'm going to go to the scene folder and i'm just going to delete scenes i like to keep all my project specific files under this underscore projects and assets just so i can easily find it because it'll always be at the top i've already gone and imported a whole bunch of art audio and fonts into this project so now that we have our new scene let's go into it and hit right click create empty and we're going to call this network controller under network controller we're going to do another create empty and we're going to do photon and underneath photon i'm going to create another empty and we're going to call this connector so in our underscore projects let's go ahead and right click and create in a new folder and we're going to call that code so this is where all our code is going to exist and inside of code let's create another folder called photon and so in this directory is where all our specific photon code is going to live so now let's right-click create and do our first c-sharp script and we're going to do this photon connector let's go into our scene and click on connector and drag photon connector as a component now that we've attached our photon connector to our connector game object we need to import uh photon 2 into our project before we can start coding so let's click on asset store and if you don't have the asset store tab here you can go to windows asset store so what we're going to do is search for pun to dash free so let's click on it so this is a free option uh it allows you to have 20 concurrent players without uh costing you any money so let's hit import while this imports um oh that was quick so what you can do once uh the import unity package window pops up here is we're just going to hit import but if you want to reduce the amount of code that's imported into yours you can reduce it by unclicking some of these demo scenes but i would include them just because you want to be able to see the example code that was written by exit kim's so let's hit import so this is where we're going to use the app id here in a second once this finishes uh importing it will prompt up a wizard and in that wizard we can paste in the app id that we generated earlier inside of the photonengine.com now that the photon package has finished importing you'll see that this is what pops up the pun wizard and it's this setup and it requests the app id or email so we're going to paste in the app id that we set up earlier we're going to set up project and now we have completed what the setup process so we can hit close here so you'll see that we now have some new files added to our assets folder so by default it takes you to this resources and scriptable object here this is important this will allow you to change and modify the photon server settings you'll see here's the app id that we set up but a couple things we want to do is depending on what region you're from uh i'm from the us so i'm going to type in for fixed region us and for the dev region us the reason why i'm doing this is when i'm doing testing right now i want to make sure that my standalone builds and my unity builds will both be connecting to the same region in photon so then we can connect to the same master server so now that we've imported this successfully let's go back and look at our photon folder and go to photon connector and let's open that script so i'm using visual code for my development and these are the three different extensions that i use i know a lot of people use visual studios this is a just an alternative lightweight version that you can use as well so now that we're back let's click back on the explorer and by default this is our boilerplate unity code so what i'm going to do is i'm just going to delete everything here and start from scratch so i want to do three things just because we're going to have a decent amount of code in here i like to divide my code into different regions so i'm going to have a unity method region i'm going to have a region for my private methods and this is an optional step this is just for me to stay sane and then one for our public methods and lastly one for our photon callbacks and this is the region of our code that is going to be most populated with all these different callbacks we're going to be implementing so let's save that so the first method we will be implementing is the unity start method so in the start method we're going to set a random name for the player connecting to photon for now um this will will change this in the future but for now we're just going to give it a random name so i'm going to have it tester dot grid this is just a way to get a unique name because the grid will be unique always it's got me to use a grid though we need to use using system there we go so after we set our random name i want to connect to photon and i'm going to pass in the random name so the reason why this is giving us issues is because we don't currently have a method called connective photon so let's make one so we're going to do private void connect to photon string and we're going to have the argument name as nickname just because that is the terminology that photon uses when it's connecting so we're going to do a debug statement just so we can keep track of where we are when we're in the console and we're going to do connect to photon as and we're just going to pass say the the nickname so now we can do photon network dot auth values so these are just basic uh author authentication that we're gonna do and we're just gonna pass in the nickname and the values here and because this is giving us an issue here what we need to do is include using photon pawn so now that we've done that we can go photon network dot automatically sync scenes so in this game whenever the master client joins a scene i want other clients that are connected to their same room to also join that room and i think i missed an s there we go authentication oh the reason why this is complaining is because for authentication we need to use using photon real time as well so now we have those two using statements i'm just going to clean this up a little bit and remove the other using statements that aren't currently being used so let's do photon network dot nickname equals the nickname that we passed in and then photon network dot connect using settings and these are the settings that we set up in the scriptable object we were looking at just a minute ago and all that's this is all we have to do now when we run this game we will connect to the master server using the random nickname that's generated but we want to implement some photon calls to actually be able to proceed from here and to do photon callbacks we need to not inherit from mono behavior but actually inherit from mono behavior pun callbacks once we inherit from mono behavior pine callbacks we can start implementing some callbacks here and or overriding some callbacks and the first one we want to do is public override void on connected to master so this is the callback that happens when we connect to the master photon server so we're going to do debug.log just so we can keep sign and we're going to say you have connected to the mat photon master server once we connect to the photon master server what we want to do is check to see if we are in a photon lobby or not so basically once you connect to a server you want to connect to a lobby and then from lobbies you can connect to the different rooms that are inside the lobby so what we're going to do is if not in a photon network dot in lobby so basically this is saying if we are not in a photon lobby let's join a photon lobby so let's go photon network dot join lobby and it's as simple as that but to verify that we joined a photon lobby we now need to implement another photon callback and that photon callback is on joined lobby so let's do photon or public override void on joined lobby so debug.log we don't need a dollar sign here you have connected to a host on lobby you don't need the dollar sign there either all right now that we've connected to the photon lobby we want to actually create a room for ourselves so let's do create photon room and we're just going to give it a name test room for now we're just going to do a hard coding this will all be dynamic in the future so this right here is giving us another underline that's because this is a method that doesn't exist yet and this will be a private void crate photon room this method will also need to take in a string room name once we do that you can see our issues down here has been resolved so to create a photon room we need a couple things we need to set a room options variable and we're just going to call room options ro equals new room options so there's a few different themes you can set and let's look at that so you can do if the room is open if the room is visible the different max players and a few other things as well feel free to look through here so we're just going to set is open to true we're going to do ro dot is visible to true and ro dot max players equals four so what this means is our room will be open for other people to join the room will be visible for others to be able to see it in roomless and the max number of players we're going to allow is four so once we set up our room options we can do photon network dot join or create room to be able to call this method you need to be connected to the photon master server and a photon lobby so what we're going to do is pass in a string name which is room name and then the next expected parameter is a room options which is ro and then it is a typed lobby so for this we're just going to do typed lobby dot default and voila that is all we need to do to actually create and or join a room when we do this it will do another photon call back that photon callback there will be two that actually kick off depending on if we create a room or create a room and join it or we just join a room so the first one we're gonna implement is public override void on created room so if we were the first person to correct to join this room or we will be the ones that are actually creating it so let's do debug.log you have created a photon room named and let's simply put the photon name here so photon network dot local player or actually we can do room i think current room there it is current room dot name so this will just be a nice little print out that we'll be able to verify that we joined it or we created the room so if you were to be the person that created the room you will also have another callback called that we'll need to override which is void on joined room you have joined the room the photon room and we're going to do the same thing i'm just going to copy and paste this there you go the next photon callback method we want to implement is on join room failed so let's do that now public override void on join room failed and so this will pass in a return code and a message so we will print out the message in a debug log and for now that's all we'll do in this one as well so you failed to join a photon room and we'll print out the message colon here all right once we implemented this one let's do the next one which is on player entered room so on player in a room is when another player enters the room you're currently in so let's do public override void on player entered room and this will give you the photon player and this is the photon.realtime.player so what we will do is just a simple debug.log another player has joined the room and we'll do new player dot user id so the next callback we will implement from photon is on player left room so this is when another player leaves the current room you're in so on player left it's not gonna help me public override void on player left room debug.log player has left the room other player dot user id the next callback method we want to implement is public override void on master client switch so what this method is is when the master client in the room leaves the room will automatically switch the master client to another person in that room and that other person that is now the master client is passed in here as the new master client let's do debug.log we'll say new master client is and we'll do new master client dot user id and for the last photon callback we want to implement around joining and leaving rooms is actually the on left room so i'm going to put that up here and this method is when the current player so you has left a photon room so on left room and we'll do a debug statement i'll say you have left a photon so this is all the code that we need to at least get our project started so now let's switch back to unity and go to our scene or game view and just hit play once that's actually playing let's go to our console and now we can actually see everything that's happening so first step was we generated a random tester then we connected to the photon master server once we connected to the photon master server we're connecting to a photon lobby we were creating or joining a photon room so you can see we have two methods now the first one is you have created the photon room test room and then we also got the photon callback method of you have joined the photon room test room so that will wrap us up with getting the project started if this video helped you out please give it a thumbs up and subscribe to the channel also join our discord if you have any questions or requests for additional videos there will be an invite link to the discord below along with some extra resources i will be putting out of the next video shortly on how to implement playfab into your project future videos will include how to build a friend and party invite system recent players list a simple store how to store stats into play fab for a player leaderboards and much more i want to thank you for watching and have a good one
Info
Channel: Knox Game Studios
Views: 3,815
Rating: undefined out of 5
Keywords: photon, unity, playfab, gamedev, unity3d, multiplayer, pun2, networking, crossplatform, build multiplayer game, build unity multiplayer
Id: cIhreFhKs5U
Channel Id: undefined
Length: 23min 21sec (1401 seconds)
Published: Thu Aug 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.