Unity如何多人联机游戏开发

努力加载中,稍等...
暂无新消息
努力加载中,稍等...
已无更多消息...
这些人最近关注了你
努力加载中,稍等...
已无更多消息
努力加载中,稍等...
已无更多消息
& 在Unity中创建跨平台多人联网游戏(一)
Creating a Cross-Platform Multiplayer Game in Unity — Part 1
征集热心朋友翻译文章,奖励规则:每100汉字奖励10QB,30天内互动奖励0 - 50QB.
翻译请求:专家推荐
该文章来自用户转载
What? You want to make a multiplayer game? Are you nuts!? These things are hard! Seriously, when it comes to game development, few things drive a developer crazier than multiplayer: it’s a pain to test, difficult to debug, can harbor bugs that are nearly impossible to reproduce — until, of course, you demo your game for the press — and, even when all your code is working perfectly, can still break thanks to flaky network connections.In spite of the challenge of creating multiplayer games, they are totally worth it. No AI, no matter how clever, will have the same ingenuity as a human opponent, nor will trash talking an AI be as fun as trash talking your friends. And I haven’t heard of any player starting a relationship with an AI that they met online…movies starring Joaquin Phoenix notwithstanding. :]In this tutorial, you’ll create a networked multiplayer game that works across different brands of devices. When you show off an iOS and Android game playing together, your co-workers are going to think you’re some kind of crazy wizard!But try not to let it go to your head.In the first part of this tutorial series, you will learn the following:How to configure and install Google Play Games ServicesHow to setup an iOS project to work with Google Play Games Services.How to sign in a user.How to sign out a user.You should have the following prerequisites before you move ahead with this tutorial:A basic understanding of Unity and C#. All of your coding is going to be in C# — no Objective-C, Swift, or Java here!Some experience running a Unity game on an actual device.At least two devices to develop on: either iOS, Android, or one of each. Testing a multiplayer game with just one device tends to be rather difficult. :]A Google Play Developer account, which you can sign up for at . Even if you’re going to test this on iOS only, you’ll still need one of these accounts. Obtaining a Play Developer account will set you back $25 — but you’ll consider it worthwhile once you experience your game in action!Note: For those of you considering trying this tutorial using just the simulators, you will find yourself tearing out your hair. The simulators will not work. You will need two devices. Ideally, you should have one iOS device and one Android device, but in a pinch, two of the same devices will work fine.If you are just starting out with Unity, it would be well worth your time to work through Christopher LaPollo’s excellent
on this site before you tackle an advanced-level Unity tutorial like this one.Getting StartedIt’s time to get familiar with the game you’re going to turn into a multiplayer showcase of awesomeness. , then unzip the file to extract the project.In Unity, select File\Open Project. Select Open Other… from the dialog box that appears, and then select the Circuit Racer folder you just unzipped:Start by loading up the Main Menu scene. From the Assets panel, select MenuStuff and then double-click MainMenu to open up the main menu.Hit the Play button to try out the game within U select the single player experience, and watch as your car performs death-defying feats as it circles around the track before time runs out!Oh, by the way — watch out for those crates. :]Running on a Real DeviceIt’s time to try this on an actual device. Since you’re reading this on RayWenderlich.com, I’ll assume you’ll want to run this on your favorite iOS device. Readers on RoyWenderlich.com, the Android tutorial site run by Ray’s evil twin, probably have a different experience in mind:A rare photo of Ray and Roy together, before what has only been referred to as “the incident”.You’ll first need to change the Bundle Identifier of your project to something a little less generic. Go to File\Build Settings, then select iOS. The iOS platform should already be selected as your target platform, but if it isn’t, click the Switch Platform button with iOS selected.Click Player Settings and the PlayerSettings should appear in your Inspector panel. In the Other Settings area, look for the Bundle Identifier and change it from com.example.CircuitRacerUnity to something that’s unique to you and appropriate for your provisioning profile, like com...CircuitRacerUnity:Now connect your physical device to your Mac. If you haven’t run an Xcode project on a device before, then read up on what’s required
first.Head back to the Build Settings dialog box, which should still be open, and select Build and Run. Unity will ask you to save the generated X give it a fun and original name such as CircuitRaceriOS.Once Unity has finished exporting the project, it will open up the newly created project in Xcode. Xcode will take a few moments to process the file, and it will then run the game on your actual device! However, sometimes Xcode forgets to run the project for you. If this happens, just click the Run button to remind Xcode why it opened in the first place. :]Note: If you’re running an older version of Unity and a newer version of Xcode, Unity might have trouble opening up Xcode after it exports the file to an iOS project. If that happens to you, either upgrade your version of Unity to the latest version, or simply open Xcode, open your project manually, and then continue running your project from there.In addition to the on-screen gamepad support, the game also supports tilt controls — something you probably wouldn’t have noticed when you were playing the game within Unity.Understanding Circuit RacerThe Circuit Racer game in this tutorial is a mashup of two different tutorials:The Circuit Racer SpriteKit game, as presented in the book .The
on this site by Christopher LaPollo.This tutorial won’t go into too much detail about how the controls work, how the game knows you’ve completed a circuit, or how the car turns, since that’s been covered by the two aforementioned tutorials and it’s not directly relevant to your goal of turning this into a multiplayer game. Still, feel free to browse through the code, in particular the GameController, CarController, and AnalogControl classes, which will give you a general idea of how things work.Note: At the time of this writing, Unity 4.6 was still in beta, so this project is still using the older GUI system supported by Unity 4.5 and earlier.As long as you’re trying out the game on your device, you might as well see what the multiplayer option has to offer. Head back to the main menu and click on the Multiplayer button:Well, that was underwhelming, to say the least. But the good news is that you’re going to fix it all up!Introducing Google Play Game ServicesAlthough negotiating network connections between devices behind various firewalls might sound like fun to some, most developers choose to use third-party libraries to handle many of the “complicated but not fun” tasks of initiating multiplayer games. There are lots of choices for third-party frameworks out there, but this tutorial uses Google Play game services for the following reasons:It’s free.There’s a plug-in available for Unity.The plug-in is also free.Yes, skeptical cereal-eating man, it’s true. Game Center does provide some really good support for real-time multiplayer, but it’s only available for iOS devices. In this day and age, there’s probably a very good chance that even the most hardcore iOS fan has an Android-using friend or two that they’d like to play games with.One of the advantages of using Unity to make your 2D game is that you can easily export your game to both Android and iOS, and it would be awfully nice if your multiplayer solution followed suit. In the interest of full disclosure, I work for Google and I occasionally play Towerfall with the people who made Google Play game services, so I’m just a teeny bit biased.Google Play game services includes a number of features for game developers including achievements, leaderboards, saving your game in the cloud, creating quests, and, most importantly for this tutorial, support for turn-based and real-time multiplayer. You won’t implement any of these features right now except for real-time multiplayer. However, once you go through the work to integrate Play services into your game, it’s probably worth checking out some of the other features to see what they can offer you.Setting Up Google Play Game ServicesOkay, I’ll level with you here: there’s a bit of setup required to get things going. But stick with me — once you get through this part, you can go back to writing glorious code!Registering Your GameBefore you do anything else, you’ll need to register your application with the Google Play Developer Console and get yourself a Client ID. This identifies your app with the service and lets you perform important actions like signing in the user.Open
in your browser. If you don’t have a Google Play developer account, you can set one up at this point:If you have an account and log in, you will see a screen that either looks like this:…or one that shows your existing applications:Either way, get started by clicking the little gamepad icon on the left:Next, click the Set Up Google Play game services button like so:If you’ve set up Play game services before, click on the Add a new game button at the top of the screen:A dialog box will appear with two tabs: the first tab is relevant if you aren’t yet using any Google APIs in your game, and the second is for the case where you’re working with an existing game that already uses Google APIs. You’re definitely in the first situation, so leave the dialog on the first tab.Name your p you can call it whatever you like, as it doesn’t need to be unique. Choose a g “Racing” is probably a good choice, but hey, it’s a free country. When you’re done, your dialog should look like the following:Click Continue, and you’ll find yourself on the Game Details page, where you can add a name, a description and some graphics to your game. This are things you’ll have to handle before you publish your game in real life, but for the purposes of this tutorial you can skip filling out these items for now.Instead, you’ll set up your Client ID. Click on the Linked Apps tab on the left side as shown below:Next, select iOS from the given options like so:In the next page that appears, leave Name set to whatever is prefilled for you and leave both iPad and iPhone set to YES. Next, enter the Bundle ID you assigned to your game back in the Build Settings step.Note: Changing your bundle identifier later can be a huge pain. To make your life easy, ensure you have a Bundle ID that matches what you entered in Unity and is unique to you and your organization.Leave the iTunes App ID you don’t need this for now. Your page should now look like the following:Below that, you have two separate options in the Multiplayer Settings section to enable turn-based and real-time multiplayer support for your game. But which one do you want?Turn-Based vs. Real-Time: A Quick PrimerGoogle Play game services, along with many other game frameworks, supports turn-based and real-time games. Here’s a quick introduction to what each of these mean:A real-time game requires that everybody be present and play it generally applies to games where all players play at once and see each other’s moves as they happen. Examples of this include first-person shooters, multiplayer online battle arenas, and most sports games.Turn-based games, on the other hand, are generally played by one person at a time, usually while the other players aren’t active and — gasp! — might not even have their phones with them. With these types of games it might take a day or two for players to
as a result, this option is most useful for games that are slower, more contemplative, or involve situations where a single player’s turn can take a long time. Words With Friends and Draw Something are prototypical examples of turn-based games.Pop quiz, hotshot: you’re making a Texas Hold’em poker game. Should this be turn-based, or real-time? Check your answer below.Solution Inside: AnswerIn your racing game, you’ll have two players racing against each other on the same playing field, so turn the Real-time multiplayer setting to ON.Click the Save and continue button at the top of the screen. Next, you’ll see a screen like the one below where you’re asked to authorize your app:This is where you create your client ID, which is essentially an identifier that identifies your app on Google’s servers. Click the Authorize your app now button.The first dialog that appears lets you add your company’s name, logo, these settings determine how your game appears in the various screens that prompt the user to sign in to your application:For this tutorial, just click Continue.The next dialog asks for your bundle identifier and App Store ID. It’s been pre-populated with the same information you added previously in Step 1 of the dialog, so you shouldn’t have to do anything here. Simply confirm everything is correct, then click Create client:Once the page finishes processing, you will see a giant string ending with apps.googleusercontent.com — this is your Client ID:Copy and paste this string into a text file somewhere, as you’re going to need it later.Adding TestersThere’s just one last step before you can get coding. Click the Testing tab on the left side of the screen to go the Testing panel. In the Testing Access section you should see your email address, and possibly those of your other team members like so:When Play game services are in an unpublished state, they can only by used by accounts that are listed here. Since you’re going to be testing with a few other Google accounts — for your multiplayer game, you’re going to need at least two — click on the Add testers button and add the addresses you’d like to whitelist:Give yourself a pat on the back — you’re all done with the Google Play setup! :]Installing the Unity PluginNow you need to install the plugin that lets Unity talk to Play Games. Go to
and click the “Download ZIP” button on the right:This button either means you’re downloading something from the cloud, or it’s raining deadly arrows. Either way, you should probably stay indoors.Once that’s done, unzip the package you downloaded and head back to Unity. Hello, Circuit Racer! Did you miss us? :] To install the plugin, select Assets\Import Package\Custom Package as follows:Browse to the folder you created, then navigate to the contained current-build folder. Select the file that ends with unity package.Note: At the time of this writing, the package is named GooglePlayGamesPlugin-0.9.10.unitypackage, but the version number will likely have changed by the time you’re reading this.You should now see an Importing package all options should be already selected, but if not, simply click the All button. Finally, click Import:You now have a couple of exciting new folders, and a new “Google Play Games” menu item. If you don’t see the menu item, click on the menu bar to force Unity to refresh its contents. Select Google Play Games\iOS Setup from the menu and you’ll see a dialog box where you need to provide two pieces of information:The “Client ID” is the big string you copied earlier in this tutorial.The “Bundle ID” is the Bundle ID you’ve been using all along.Enter the two pieces of information into the dialog and click Setup. You should receive an alert that all is well, at which point you can close the dialog.Oh shoot — did you forget to copy down the Client ID earlier? N here’s how to find your Client ID again:Go back to the Play Developer console at .Click on Game Services (the little controller icon) on the left.Click on your game.Click on “Linked Apps”.Click on the entry for the iOS version.At the bottom, you should see your Client ID next to a label that says, OAuth2 Client ID.Copy this value to the clipboard and continue on with the tutorial!Build and run your project in Xcode (Command-B is a handy keyboard shortcut). If everything goes well, Unity will export your application to Xcode, which will then compile and run your application on your device, and..whoops. What happened?There are a few modifications to make to your Xcode project before you can run your game on a physical device. Fortunately, the post-build script that Unity runs brings up a dialog box that tells you exactly what you need to do to get things working:That doesn’ you’ll tackle them in order.Adding Missing FrameworksIn Xcode, select the Unity-iPhone project on the left, and then the Unity-iPhone target in the main panel. Go to Build Phases, expand the Link Binary With Libraries section on the right, and then click the + button to bring up the dialog where you can add all the frameworks you need for this project to run:The dialog box should list the frameworks you need to include, but at the time of this writing, you’ll need to add the following frameworks:AddressBookAssetsLibraryCoreDataCoreTelephonyCoreTextlibc++.dyliblibz.dylibSecurityTo make your life easier, you can hold down the Command button to select several frameworks at once.Adding More FrameworksNow you’ll need to download the missing SDKs. Head to . Download and unzip both the the Games C++ SDK and the Google+ iOS SDK as shown below:Note: Be careful: there is an iOS Games SDK listed on the same page. You don’t want that one, even though you’re building an iOS game. You need to download and use the C++ SDK instead.Also note that clicking the link to download the Google+ iOS SDK takes you to a separate download page where you need to click the Download zip… link at the bottom of the screen, which in turn takes you to another page where you need to click on the Download the iOS+ SDK button to finally download the package. They, uh… really like having you click on things, apparently! :]Navigate into the gpg-cpp-sdk folder you just unzipped, go into the iOS subfolder and drag the gpg.framework and GooglePlayGames.bundle files directly into Xcode. In the resulting dialog box, ensure the Unity-iPhone target is checked. It’s up to you whether or not to select the Copy items if needed personally, I like doing it, but if you’re the type of person that likes to have all your third-party libraries in one location, you can leave it unchecked.Next, navigate into the google-plus-ios-sdk folder and drag the GoogleOpenSource.framework, GooglePlus.bundle and GooglePlus.framework files into Xcode where you should see them added to the list of frameworks:Adding the -ObjC FlagFinally, head back to XCode and select the Build Settings tab for your target. Find the Other Linker Flags option — it’s in the Linking section. Double click the part that reads: -weak_framework CoreMotion -weak-ISystem, then click the + button at the bottom of the dialog to add the -ObjC flag. Your project should look like this once you’re done:Note: Don’t skip the -ObjC it’s a particularly insidious error because your code will compile and build just fine without it. But when you try to make a call, your project will crash with a mysterious unrecognized selector message.Now you can finally go back and run your game again! Click the Run button, and if all has gone according to plan, you should see your Circuit Racer running on your device.True, the game looks the same as before — but there’s a whole slew of code and libraries underneath just waiting for you to make use of them! :]Signing in the PlayerYour first task will be to sign in the user when they click the multiplayer button. If you can get that piece working, you know you’ve set up the game services correctly and you can move on to the more interesting parts of developing a multiplayer game.Create a new class to handle some of your multiplayer code. Double-click the Scripts folder in the Unity Assets panel. Then right-click the assets panel, select Create\C# Script and name the new script MultiplayerController. Double-click to edit the script in your code editor of choice.Add the two following imports to the beginning of the file:using GooglePlayGames; using GooglePlayGames.BasicApi.Multiplayer;Next, find the following line:public class MultiplayerController : MonoBehaviour {…and replace it with the following:public class MultiplayerController {Then delete the two boilerplate Start() and Update() methods.If you’re fairly new to Unity, you might not recognize what’s happening here. You’re creating a piece of code that won’t be attached as a component to a Game O instead, you’re creating the object purely in code. With this approach you can’t use MonoBehavior you can either use ScriptableObject as your base class, or, as in this situation, nothing at all.Next, add the following content your class so that it’s created as a singleton:private static MultiplayerController _instance = null;
private MultiplayerController() { // Code to initialize this object would go here }
public static MultiplayerController Instance { get { if (_instance == null) { _instance =
MultiplayerController(); } return _instance; } }Not sure what a singleton is? It’s a common coding pattern where you create one (and only one) instance of an object that can be easily referenced by other classes in your project.In Unity, creating a class as a singleton can be useful when you need to use it across multiple scenes. The UserPreferences object is a good example, because it’s an object you need to access from the main menu, the game, and many other sub-menu scenes. Similarly, you’ll need to access MultiplayerController from both the main menu as well as in the game, so it helps to implement this as a singleton as well.In order to access the object in the future, you won’t look for specific game objects or create new objects. Instead, you’ll call the static MultiplayerController.Instance method, which either creates the singleton if it doesn’t yet exist, or returns the one that’s already been instantiated.Note: Want to find out more about singletons? Check out Eli Ganem’s excellent
tutorial on this site for more detail.Some coding aficionados aren’t terribly fond of singletons, and claim that singletons suffer from overuse. That’s a debate for another time — right now, you’ve got a game to make! :]Next, modify the code inside MultiplayerController() as follows:private MultiplayerController() { PlayGamesPlatform.DebugLogEnabled = true; PlayGamesPlatform.Activate (); }The first line sets your logs to be nice and verbose, and the second line tells the PlayGamesPlatform to initialize itself.Next, add the following method to your class:public void SignInAndStartMPGame() { if (! PlayGamesPlatform.Instance.localUser.authenticated) { PlayGamesPlatform.Instance.localUser.Authenticate((bool success) =& { if (success) { Debug.Log ("We're signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName); // We could start our game now } else { Debug.Log ("Oh... we're not signed in."); } }); } else { Debug.Log ("You're already signed in."); // We could also start our game now } }This is your basic sign-in logic. Calling PlayGamesPlatform.Instance brings up Google Play’s own singleton instance. The first if statement checks to see if the local player is signed in. If not, then you call the platform’s Authenticate method, which attempts to sign the user in.Also notice that the argument you’re passing to Authenticate is, itself, a function. Since Authenticate might take a while to execute, you pass in a callback function to execute when Authenticate this is similar to blocks in Objective-C.Now that you have a method to sign the user in, you need a place from which to call it.Open up MainMenuScript.cs, found in the MenuStuff folder. Find the following line in OnGUI():Debug.Log("We would normally load a multiplayer game here");…and replace it with the following:MultiplayerController.Instance.SignInAndStartMPGame();This calls SignInAndStartMPGame() which you created just a moment ago.Build your project:Note: When you rebuild your Unity game, you’re often given the option to Replace or Append your project. Make sure to select Append:Don’t select Replace, unless you feel like re-doing all those setup steps in the Fixing Your Xcode Project section all over again. I know I don’t!Once you have built your project, run the app. Click the Multiplayer button once your
you’ll be taken to a screen where you’re asked to sign in. Use one of the accounts you listed as a “Tester” account earlier in the tutorial. Click Accept and you’ll be taken back to Circuit Racer where you should see a “Welcome back!” graphic appear at the top of the screen and a “We’re signed in! Welcome ” message in your console log as follows:You’ve signed in — looks like everything is hooked up and authenticating correctly!Improving the Sign-In ProcessStop the application and start it again in Xcode. Notice that you’re no longer signed in, but when you click the Multiplayer button this time around, you’ll see the “welcome back” message without having to go through the sign-in process. What’s going on?If you’ve signed in previously, haven’t explicitly signed out, and the game isn’t asking for any new permissions, then Google Play Games assumes it’s okay to sign you in again without showing you another sign-in dialog, which would get pretty annoying over time.You can take advantage of this fact by “silently” signing the user in when the application starts. Go back to your MultiplayerController.cs script and add the following new method:public void TrySilentSignIn() { if (! PlayGamesPlatform.Instance.localUser.authenticated) { PlayGamesPlatform.Instance.Authenticate ((bool success) =& { if (success) { Debug.Log ("Silently signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName); } else { Debug.Log ("Oh... we're not signed in."); } }, true); } else { Debug.Log("We're already signed in"); } }This looks awfully similar to the code in SignInAndStartMPGame; the difference is that you have a trueargument at the end of your Authenticate call, which instructs the Play service to try to sign the user in silently. That is, Google Play will check to see, “Is this a case where I can sign the user in without having to show them a dialog?” If so, it will sign the user in silently.You’ll need to call this from somewhere. Add the following line to the end of Start() in MainMenuScript:MultiplayerController.Instance.TrySilentSignIn();Build and run your project from within U this time, the application should start up and, after a moment or two, you’ll see the welcome back notification and the console text indicating that you’re signed in. Tap the Multiplayer button and you should see a line in your console log noting that you’re already signed in.Adding a Sign-Out ButtonSince you’re knee-deep in the authentication code, you may as well add a sign-out button too. You wouldn’t typically put such a button on the main screen, as people tend to sign out infrequently enough that you could put this in a settings screen somewhere. However, I find it useful to quickly sign in and out of the app while testing during development.Add the following public variable to the top of your MainMenuScript class:public Texture2D signOutButton;This public variable will hold a reference to your button image.Next, add the following code to the end of OnGUI(), outside of the for loop:if (MultiplayerController.Instance.IsAuthenticated()) { if (GUI.Button( Rect(Screen.width - (buttonWidth * 0.75f),
Screen.height - (buttonHeight * 0.75f),
buttonWidth * 0.75f,
buttonHeight * 0.75f), signOutButton)) { MultiplayerController.Instance.SignOut(); } }This code checks if there is currently a signed- if so, it displays a button to sign the user out. If the player taps that button, the app calls SignOut() on your MultiplayerController.Next, you need to implement those two new methods that you’re calling in your MultiplayerController; fortunately, they’re pretty straightforward. Go back to MultiplayerController.cs and add the following two methods:public void SignOut() { PlayGamesPlatform.Instance.SignOut (); }
public bool IsAuthenticated() { return PlayGamesPlatform.Instance.localUser.authenticated; }Note: Are you wondering why you’re going through the trouble of calling SignOut() in MultiplayerController, which then calls SignOut() on the Play Games platform — instead of simply calling PlayGamesPlatform.Instance.SignOut() directly in your MainMenuScript?This is because you’re trying to follow the general principle of encapsulation (or “information hiding”) and keep all related logic together in one class, rather than spread it out over many classes. If you wanted to swap out Play Games for a completely different multiplayer service, you’d only have to modify MultiplayerController; you wouldn’t have to change any of your other code.Go back to Unity and open up the MainM click on MainMenuGameObject in the hierarchy panel, and you’ll see that the Main Menu Script now has a Sign Out Button entry that is currently undefined. Click on the circle next to the input text field and select btn-signout from the game’s assets as shown below:Bu test the sign-in and sign-out process a few times to make sure it works consistently. The sign-out option will come in quite handy as you test your app with multiple players down the road.Where to Go From Here?The end result of all your hard work is that a lot of the setup work is done and you can finally get around to the fun parts of building a multiplayer game.In
of this tutorial series, you’ll tackle the actual multiplayer aspects of the game. You’ll add matchmaking capabilities so you can connect your games client with other players across the room — or across the world. You’ll add the opponent’s car to the racetrack, and you’ll begin to send and receive gameplay messages so that you can get these cars racing!You can download the completed project for this part . Just make sure add your own Google API Client ID.In the meantime, if you have any questions or comments, please feel free to join in the discussion below!
在Unity中创建跨平台多人联网游戏(一)
版权所有,禁止匿名转载;禁止商业使用;禁止个人使用。
译者:王磊(未来的未来)
审校:崔嘉艺(milan21) - 即使你的所有代码都能正常工作,可是游戏仍然可以因为网络情况太差而出现问题。尽管开发多人联网游戏会面临这么大的挑战,但是这是完全值得的。无论多么聪明的AI,都不可能拥有像人类的对手一样的聪明才智,你与AI的交谈也不会像与你的朋友的交谈一样有趣。 而且我还没有听说任何玩家会与网上的AI开始交往。。。虽然杰昆·菲尼克斯主演的电影中有发生这种情况:]在这个教程中,你将创建一个可以在不同的设备上运行的联网多人联网游戏。当你炫耀iOS和Android的设备可以一起玩你的游戏的时候,你的同事会认为这非常的酷!但是尽量不要让这种想法占据你的思想。在这个系列教程的第一部分中,你将学习以下内容:如何配置和安装Google Play的游戏服务。如何设置iOS项目以使用Google Play游戏服务。如何登录用户。如何登出用户。在你继续阅读本教程之前,你应该具备以下先决条件:对Unity和C#的基本了解。 所有的编码都将通过C#进行–这里没有Objective-C、Swift或是Java代码!有一些在实际设备上运行Unity游戏的经验。有至少两个可以进行游戏开发的设备:iOS,Android或是每种一台。
只用一个设备来测试多人联网游戏往往是相当困难的。 :]Google
Play开发者帐户,你可以通过进行注册。 即使你只在iOS上测试,你仍然需要这样一个帐户。获取Play开发者帐户将使你付出25美金 - 但是,一旦你在实际中体验到你的游戏,你会认为这笔投资是值得的!需要注意的是:对于那些考虑使用模拟器来尝试本教程的人,你会发现使用模拟器会让你很狂躁。模拟器这种方法不行。你将需要两个设备。在理想情况下,你应该拥有一个iOS设备和一个Android设备,但是两个相同的设备也可以正常工作。如果你是刚刚开始使用Unity,那么在学习像这样的高阶Unity教程之前,先学习本网站上ChristopherLaPollo的一本优秀教程——《Unity 2D系列教程》将对你有很大帮助。起步现在,是时候来熟悉一下这个你将完成的令人惊叹的多人游戏了。下载Circuit Racer启动项目(Unity版本),然后解压缩文件以提取工程。在Unity中,选择File \ Open Project。从出现的对话框中选择Open Other… ,然后选择你刚刚解压好的Circuit Racer文件夹:首先加载主菜单场景。从资源面板中,选择MenuStuff,然后双击MainMenu打开主菜单。点击播放按钮,尝试在Unity中运行游戏。选择单人游戏,并在时间耗尽之前尽量让你的赛车多跑几圈!哦,顺便说一下 - 小心这些箱子。 :]在真实的设备上运行现在是时候在实际的设备上尝试这个游戏了。由于你是在RayWenderlich.com上阅读此文档,所以我们假设你会希望在iOS设备上运行这个游戏。 RoyWenderlich.com上的读者-这是Ray的邪恶双胞胎,是一个Android教程网站-可能会有不同的想法:Ray和Roy很罕见地出现在同一张照片中。你首先需要将项目的Bundle标识符更改为一个不太通用的标识符。转到File \ BuildSettings,然后选择iOS。iOS平台应该已经被选为目标平台,但如果不是的话,请点击切换平台按钮来选择iOS平台。单击播放器设置,播放器设置应该显示在“检视器”面板中。 在“其他设置”区域中,查找“Bundle标识符”,将其从com.example.CircuitRacerUnity更改为唯一的、适用于你的配置文件,如com.&你的公司名称&.&你的用户名& .CircuitRacerUnity:现在将你的物理设备连接到你的Mac电脑上。如果你以前没有在这个设备上运行过Xcode项目,请先阅读本教程中的前置要求再做这个事情。回到“构建设置”对话框,这个对话框应该仍然是打开状态,然后选择构建和运行。Unity将要求你保存生成的Xcode项目。给它一个有趣的初始名字,比如像是CircuitRaceriOS。一旦Unity完成了项目的导出,它将在Xcode中打开新创建的项目。 Xcode将需要一些时间来处理该文件,然后它将在你的实际设备上运行游戏! 但是,有时Xcode会忘记为你运行项目。如果发生这种情况,只需单击运行按钮提醒Xcode为什么它会被开启。 :]需要注意的是:如果你运行的是较旧版本的Unity和较新版本的Xcode,Unity可能会在将文件导出到iOS项目后打开Xcode。如果发生这种情况,请将你的Unity版本升级到最新版本,或者简单地打开Xcode,手动打开项目,然后从那里继续运行你的项目。除了支持屏幕上的手柄之外,游戏还支持重力感应,可以通过倾斜手机来控制- 当你在Unity中玩游戏的时候,你可能不会注意到这一点。了解Circuit Racer这个教程中的Circuit Racer游戏是两个不同教程的混搭:Circuit
RacerSpriteKit游戏,在书籍《iOS游戏开发指南》中讲述过。本网站上的Christopher LaPollo的《Unity 2D简介教程》。这个系列教程将不会详细介绍控件的工作原理、游戏是如何知道你已经完成了一圈、或是汽车如何进行转向,因为上述两个教程已经涵盖了这些内容,并且与你把游戏变成一个多人联网游戏的目标没有直接的关系。不过,请随时浏览代码,特别是GameController、CarController和AnalogControl类,这将为你提供一些有关事情的总体思路。需要注意的是:在撰写本文的时候,Unity 4.6仍处于测试阶段,因此该项目仍在使用由Unity 4.5及更早版本支持的旧版GUI系统。只要你在设备上尝试这个游戏,你也可以看看多人联网游戏选项所提供的内容。回到主菜单,然后点击多人联网游戏按钮:呃,好吧,看到的内容是非常糟糕的,但好消息是,你要解决这个问题!对Google Play游戏服务的介绍尽管探讨各种防火墙背后设备之间的网络连接对某些人来说可能听起来很有趣,但大多数开发人员选择使用第三方库来处理初始化多人联网游戏的许多“复杂但不好玩”的任务。第三方框架有很多选择,本教程选择使用Google Play游戏服务的原因如下:它是免费的。Unity中有对应的插件可用。这个插件也是免费的。是的, 正在吃谷物的哥们,这是真的。游戏中心确实为实时多人联网游戏提供了一些非常好的支持,但它只适用于iOS设备。 在这个时代,即使是最彻底的iOS粉丝也有一两个使用Android想一起玩游戏的朋友。使用Unity来制作2D游戏的优点之一是你可以轻松地将游戏导出到Android和iOS上,如果你的多人联网游戏解决方案也是如此,这将是非常好的。坦白说,我为Google工作,而且我偶尔会与制作Google Play游戏服务的人一起玩Towerfall,所以我多少会有点偏袒。GooglePlay游戏服务包括了游戏开发人员需要的许多功能,包括成就、排行榜、将游戏状态保存在云中,创建任务,而对于本教程最重要的是它支持基于回合制和实时多人联网游戏。除了实时多人联网游戏,你将不需要实现任何其他功能。但是,一旦你将Play服务整合到你的游戏中,你可能需要检查一些其他功能,以查看他们能为你提供什么。设置Google Play游戏服务好的,我在这里要跟你说一下:这里有一些设置需要处理一下。但是坚持下去 - 一旦你完成这个部分,你就可以继续写代码了!注册你的游戏在你做任何其他事情之前,你需要通过Google Play开发者控制中心注册你的应用程序,并获得自己的客户端ID。这样就可以通过该服务识别你的应用程序,并允许你执行重要操作,比如说是用户登陆。在浏览器中打开https://play.google.com/apps/publish/。如果你没有GooglePlay开发者帐户,你可以在这申请一个:如果你有帐户并登录,你将看到如下所示的屏幕:。。。或是显示你现有的应用程序:无论采用哪种方式,都是通过点击左侧的小游戏手柄图标开始接下来,像这样点击”设置Google Play游戏服务”按钮:如果你以前设置过Play游戏服务,请点击屏幕顶部的“添加新游戏”按钮:将出现一个对话框,其中包含两个选项卡:第一个选项卡对应的是,你尚未在游戏中使用任何Google API,第二个选项卡是你正在开发的游戏中已使用的Google API的情况。你肯定是第一种情况,所以选择第一个选项卡来离开对话框。适当地命名你的项目。你可以用任何你喜欢的名字来称呼它,因为这个名字不需要是唯一的。 选择一个游戏类别,“赛车”可能是一个很好的选择,但嘿,这是一个自由的国度,你可以随意选择。完成后,你的对话框应如下所示:点击”继续“,你会发现自己现在看到的是游戏详细信息页面,你可以在其中添加名称、描述和一些图像到你的游戏中去。这是你在实际发布游戏之前必须处理的事情,但出于本教程的目的,你可以跳过填写这些项目。相反,你将设置你的客户端ID。 点击左侧的“链接应用程序“选项卡,如下所示:接下来,从给定的选项中选择iOS,如下所示:在出现的下一页中,将“名称”设置你想设置的任何内容,并将iPad和iPhone设置为“是”。接下来,在”构建设置“步骤中输入你分配给游戏的Bundle ID。ITunes App ID不需要填写;你现在还不需要用到它。现在你的页面看起来应该如下所示:在这个页面的下方,在“多人联网游戏设置”部分中有两个单独的选项卡,以便为你的游戏启用回合制和实时多人联网游戏。但是你想要哪一个?回合制多人联网游戏 vs. 实时多人联网游戏:快速入门GooglePlay游戏服务以及许多其他游戏框架支持回合制和实时游戏。这是一个快速介绍,介绍一下二者的含义:实时游戏要求每个玩家同时在场和进行游戏,它通常适用于那些所有玩家一起玩的游戏,并能看到对方的动作发生。这其中的例子包括第一人称射击游戏,多人在线竞技场以及大多数运动游戏。另一方面,回合制的游戏通常一次只有一个人在玩,而其他玩家则是不活跃的 - 甚至可能没有在他们的手机旁边。对于这些类型的游戏,玩家可能需要一两天的时间来进行下一回合。因此,这个选项对于速度更慢、更需要长考的游戏或涉及到单个玩家需要很长时间来完成一个回合的场合最有用。与朋友说话和是绘画有关的东西是回合制游戏的典型例子。流行测验,热点:你正在做德州扑克游戏。这应该是回合制还是实时的? 在下面检查你的答案。答案:虽然扑克在技术上是一个基于回合制的游戏,但人们期望快速连续进行回合,因此您希望这是一个实时游戏。你能想象一个扑克游戏,每个玩家花费48个小时来决定是否投注? 第一轮投注可能需要一个月的时间! 这太无聊了! :]GooglePlay游戏服务包括了游戏开发人员需要的许多功能,包括成就、排行榜、将游戏状态保存在云中,创建任务,而对于本教程最重要的是它支持基于回合制和实时多人联网游戏。除了实时多人联网游戏,你将不需要实现任何其他功能。在你的赛车游戏中,你将有两名玩家在同一场比赛中相互竞争,因此将“实时多人联网游戏”设置设为”开“。点击屏幕顶部的“保存并继续“按钮。接下来,你会看到下面这个截图,你被要求授权你的应用程序:你可以在这里创建客户端ID,这个ID基本上是标识你的应用在Google服务器上的标识符。点击“授权你的应用”按钮。出现的第一个对话框可以添加公司的名称、标志和主页。这些设置决定了你的游戏如何显示在提示用户登录你的应用程序的各种截图中:对于这个教程而言,只需单击“继续”。下一个对话框要求你的bundle标识符和App Store ID。它已经预先填充了你在对话框的第1步中添加的相同信息,因此你不必在此处执行任何操作。 只需确认一切正确,然后单击“创建客户端”:页面处理完成以后,你将看到一个以apps.googleusercontent.com结尾的巨型字符串 - 这是你的客户端ID:将此字符串复制并粘贴到某个文本文件中,稍后你将需要它。添加测试者在你开始编码之前还需要最后一步。单击屏幕左侧的“测试”选项卡进入“测试”面板。在“测试访问”部分,你应该会看到你的电子邮件地址,也可能会看到你的其他团队成员的电子邮件地址,如下所示:当Play游戏服务处于未发布状态的时候,这个服务只能由此处列出的帐户来使用。由于你将要使用其他几个Google帐户进行测试 - 对于你的多人联网游戏,你至少需要两个账户 - 点击添加测试者按钮,然后添加要列入白名单的地址:赞赏下自己 - 你完成了Google Play的设置! :]安装Unity插件现在你需要安装插件,从而在Unity中使用Play游戏的服务。打开页面https://github.com/playgameservices/play-games-plugin-for-unity,然后点击右侧的“下载ZIP”按钮:这个按钮意味着你正在从云端下载某些东西,或者外面在下很大的雨。不管怎样,你应该留在室内。一旦完成,请解压缩你下载的软件包并返回到Unity。你好,Circuit Racer!
你想念我们吗 :]要安装插件,请选择Assets \ Import Package \ CustomPackage,如下所示:浏览你创建的文件夹,然后导航到包含的 current-build 文件夹。选择以unity package结尾的文件。需要注意的是:在撰写本文的时候,该软件包名为GooglePlayGamesPlugin-0.9.10.unitypackage,但是当你阅读这篇文章的时候,版本号可能已更改。你现在应该看到一个“导入包”对话框,所有选项都应该已经被选中了,但是如果没有选中的话,只需点击“全部”按钮。最后点击“导入”:你现在有几个令人兴奋的新文件夹和一个新的“Google Play游戏”菜单项。 如果你没有看到菜单项,请单击菜单栏以强制Unity刷新其内容。 从菜单中选择Google Play Games\iOSSetup,你将看到一个对话框,你需要提供两条信息:“客户端ID”是这个教程前面你复制过的那个巨型字符串。“Bundle ID”是你一直在使用的Bundle ID。在对话框中输入两条信息,然后单击设置。 你应该收到一个警报,一切都很好,现在你可以关闭对话框了。哦,等等 - 你在前面的步骤里面忘记复制客户端ID了吗? 没问题; 以下是如何找到你的客户端ID的方法: 返回Play的开发者控制台,网址为:https://play.google.com/apps/publish/。点击左侧的游戏服务(小控制器图标)。点击你的游戏。点击“链接应用程序”。点击iOS版本的入口。在底部,你应该看到你的客户端ID旁边有一个标签,上面写着OAuth2 Client ID。将这个值复制到剪贴板,并继续这个教程!在Xcode中构建并运行你的项目(Command-B是一个方便的键盘快捷方式)。如果一切顺利的话,Unity会将你的应用程序导出到Xcode,然后编译并在你的设备上运行应用程序,然后。。。。。发生了什么?在你可以在物理设备上运行游戏之前,需要对Xcode项目进行一些修改。幸运的是,Unity运行的后期构建脚本会弹出一个对话框,告诉你需要做些什么来使得它能够正常工作:那看起来不太严重,按照顺序处理即可。添加缺失的框架在Xcode中,选择左侧的“Unity-iPhone”项目,然后选择主面板中的“Unity-iPhone”目标。 转到构建阶段,在右侧展开“链接二进制库”部分,然后单击“+”按钮,弹出对话框,你可以在其中添加要运行的项目所需的所有框架:这个对话框应该列出了你需要包含的框架,但在撰写本文的时候,你需要添加以下框架:·
AddressBook·
AssetsLibrary·
CoreData·
CoreTelephony·
CoreText·
libc++.dylib·
libz.dylib·
Security为了使你更轻松一点,你可以按住Command按钮一次选择几个框架。添加更多的框架现在你需要下载缺少的SDK。 前往页面。 下载并解压缩GamesC ++ SDK和Google+ iOS SDK,如下所示:需要注意的是:请小心:在此页面上还有一个Ios游戏 SDK。 即使你正在建立一个iOS游戏,你也不想要用那个。你需要下载并使用的是C ++ SDK。另请注意,点击链接以下载Google+ iOS SDK可以转到单独的下载页面,你需要点击屏幕底部的“下载zip 。。。”链接,然后再转到另一个页面,你需要点击的” Download the iOS+ SDK ”来最后下载所需的包。 他们,呃,显然真的很喜欢你多点几下! :]浏览你刚刚解压缩的gpg-cpp-sdk文件夹,进入iOS子文件夹,然后将gpg.framework和GooglePlayGames.bundle文件直接拖动到Xcode中。 在生成的对话框中,确保选中Unity-iPhone目标。 是否选择“Copy items ifneeded “由你决定。就个人而言,我喜欢这样做,但如果你是喜欢将所有第三方库置于一个位置,则可以对其取消选中。接下来,浏览到google-plus-ios-sdk文件夹,并将GoogleOpenSource.framework,GooglePlus.bundle和GooglePlus.framework文件拖动到Xcode中,你将看到它们被添加到框架列表中:添加-ObjC标志最后,返回XCode并为你的目标选择“构建设置”选项卡。找到“其他链接器标志位”选项 - 它在”链接“部分。 双击写着”:-weak_frameworkCoreMotion -weak-ISystem“的部分,然后单击对话框底部的”+“按钮添加”-ObjC“标志。 完成后,你的项目应该看起来是这样的:需要注意的是:不要跳过上面添加“-ObjC“的步骤,否则的话这会是一个特别阴险的错误,因为你的代码将可以正常编译和构建。但是当你运行的时候,你的项目将会遇到一个神秘的”无法识别选择器“的错误。现在你终于可以回去再运行你的游戏了!单击“运行“按钮,如果 前面都按照按计划进行,那么你应该看到你的CircuitRacer在你的设备上运行起来。真的,游戏看起来和以前一样 - 但是下面有一大堆代码和库,只是等着你使用它们! :]玩家登陆你的第一个任务是在用户点击“多人联网游戏“按钮的时候进行用户登陆。如果你可以让这部分正确运行,你就知道你已经正确地设置了游戏服务,你可以继续开发一个多人联网游戏中更有趣的部分。创建一个新类来处理一些多人代码。 双击Unity资源面板中的”脚本“文件夹。 然后右键单击资源面板,选择”创建\ C#脚本“,并命名新脚本为MultiplayerController。双击以便在代码编辑器中编辑你选中的脚本。将以下两个导入添加到文件的开头:12usingGooglePlayGusingGooglePlayGames.BasicApi.M接下来,找到下面这行:1publicclassMultiplayerController:MonoBehaviour{。。。。并将其替换为以下内容: 1publicclassMultiplayerController{然后删除样板中的Start()和Update()方法。如果你刚接触Unity,你可能不会认识到这里发生了什么。你正在创建一段不会作为组件附加到游戏对象上的代码, 而是在代码中纯粹创建对象。使用这种方法,你不能使用MonoBehavior作为你的基类,你可以使用ScriptableObject作为你的基类,或者,在这种情况下,根本没有基类。接下来,在你的类中添加以下内容,使其创建为单例:1234567891011121314privatestaticMultiplayerController _instance =null;
privateMultiplayerController(){}
publicstaticMultiplayerController Instance {get{if(_instance ==null){
_instance =newMultiplayerController();}return _}}不知道单例是什么?? 这是一个常见的代码设计模式,你可以在其中创建一个(而且只有一个)实例的对象,可以轻松地被项目中的其他类所引用。在Unity中,当你需要在多个场景中使用它的时候,将一个类创建为一个单例是有用的。UserPreferences对象是一个很好的例子,因为它是一个需要从主菜单、游戏和许多其他子菜单场景访问的对象。同样,你需要从主菜单和游戏中访问MultiplayerController,因此将其作为单例实现有助于你的开发。为了在以后访问这个对象,你不需要查找特定的游戏对象或创建新的对象。相反,你可以调用静态方法MultiplayerController.Instance,如果这个实例还不存在则此方法将创建此单例,如果已经被实例化就直接返回。需要注意的是:想了解更多有关单例的内容?查看EliGanem在这个网站上给出的一个很棒的iOS设计模式教程来了解更多详情。一些编码爱好者不是非常喜欢单例,并声称单例被过度使用。而这是另一个主题的辩论 - 现在,你还有一个游戏要做! :]接下来,修改MultiplayerController()中的代码,如下所示:1234privateMultiplayerController(){PlayGamesPlatform.DebugLogEnabled=true;PlayGamesPlatform.Activate();}第一行代码是设置好的日志,让内容比较详细,第二行代码是告诉PlayGamesPlatform自己进行初始化。接下来,将以下方法添加到你的类中:123456789101112131415public void SignInAndStartMPGame() {if (! PlayGamesPlatform.Instance.localUser.authenticated) {PlayGamesPlatform.Instance.localUser.Authenticate((bool success) =& {
if (success) {Debug.Log ("We're signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName);
} else {Debug.Log ("Oh... we're not signed in.");
} else {Debug.Log ("You're already signed in.");
}}这是你的基本登录逻辑。调用PlayGamesPlatform.Instance会得到 GooglePlay自己的单例实例。第一个if语句会检查本地玩家是否已登录,如果没有,则调用平台的Authenticate方法,该方法尝试对用户进行登陆处理。还要注意,你传递给Authenticate方法的参数本身就是一个函数。 由于Authenticate方法可能需要一段时间才能执行完毕,因此当Authenticate完成的时候,你传入的回调函数将执行, 这与Objective-C中的块类似。 现在你有一个方法来登陆用户,你需要找到一个地方来调用它。打开MainMenuScript.cs,这个文件可以在MenuStuff文件夹中找到。在OnGUI()方法中找到以下行:1Debug.Log("We would normally load a multiplayer game here");。。。并将其替换为以下内容:1MultiplayerController.Instance.SignInAndStartMPGame();这会调用你刚刚创建的SignInAndStartMPGame()方法。构建你的项目:需要注意的是:当你重新构建你的Unity游戏的时候,你通常需要选择是替换或是追加你的项目。确保你选择的是追加:不要选择替换,除非你想要把刚才部分的所有设置步骤都重新再做一遍。我知道我不愿意!构建项目后,运行应用程序。应用程序启动后,单击“多人联网游戏“按钮。你的面前将出现一个页面,要求你登陆。请使用在本教程前面部分被你列为“测试人员”帐户的一个。 点击”接受“,你将被带回Circuit Racer游戏中,将在屏幕顶部看到“欢迎回来!”,并在你的控制台日志中显示“我们已登录!欢迎“字样的消息,如下所示:你已经登录了 - 看起来像一切都正常并进行了正确的认证!改善登录过程停止应用程序并在Xcode中重新启动它。 请注意,你不再是登录状态,但是当你单击”多人联网游戏”按钮的时候,你将看到“欢迎回来”这条消息,而无需经过登录过程。这是怎么回事?如果你以前登录过,没有主动退出,游戏并不要求任何新的许可,那么Google Play游戏会认为你可以再次登录,而无需再次使用另一个登录对话框进行登陆,你可以利用“静默“登陆用户这一特点。。你可以利用“静默“登陆用户这一特点。。返回到你的MultiplayerController.cs脚本并添加以下新方法:12345678910111213public void TrySilentSignIn() {if (! PlayGamesPlatform.Instance.localUser.authenticated) {PlayGamesPlatform.Instance.Authenticate ((bool success) =& {
if (success) {Debug.Log ("Silently signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName);
} else {Debug.Log ("Oh... we're not signed in.");
} else {Debug.Log("We're already signed in");
}}这看起来非常类似于SignInAndStartMPGame中的代码。区别在于,你的Authenticate调用结束的时候有一个true参数,指示Play服务尝试以静默方式来登录用户。也就是说,Google Play会检查一下,“这是否可以进行用户登录而不必显示对话框?”如果是这样,它将以静默的方式进行用户登录。你需要在某个地方调用它。在MainMenuScript文件中将以下代码添加到Start()方法的末尾:1MultiplayerController.Instance.TrySilentSignIn();从Unity内部构建并运行你的项目。 这一次,应用程序应该启动,一到两秒后,你将看到欢迎回来的通知和控制台文本,表明你已登录。点击“多人用户“按钮,你应该在控制台日志中看到一行信息,表明你已经登录。添加退出按钮由于你深陷于验证部分的代码,你还可以添加一个退出按钮。你通常不会在主屏幕上放置这样一个按钮,因为人们往往不经常登出,可以把它放在设置中的某处。但是,在开发过程中进行测试的时候,我发现可以快速登录和退出应用程序是非常有用的。将以下公共变量添加到MainMenuScript类的顶部:1public Texture2D signOutB这个公共变量将会引用你的按钮图片。接下来,在for循环之外的OnGUI()的末尾添加以下代码:12345678if(MultiplayerController.Instance.IsAuthenticated()){if(GUI.Button(newRect(Screen.width-(buttonWidth* 0.75f),Screen.height-(buttonHeight* 0.75f),buttonWidth* 0.75f,buttonHeight* 0.75f), signOutButton)){MultiplayerController.Instance.SignOut();}}代码会检查当前是否有用户登陆,如果有用户登陆的话,它会显示一个按钮来登录用户。如果玩家点击该按钮,则应用程序将在你的MultiplayerController上调用SignOut()方法。接下来,你需要实现你在MultiplayerController中调用的两种新方法,幸运的是,他们很简单。返回到MultiplayerController.cs并添加以下两种方法:1234567publicvoidSignOut(){PlayGamesPlatform.Instance.SignOut();}
publicboolIsAuthenticated(){returnPlayGamesPlatform.Instance.localUser.}需要注意的是:你是否想知道为什么要很费劲的在MultiplayerController中调用SignOut(),然后在Play游戏平台上调用SignOut(),而不是直接在MainMenuScript中调用PlayGamesPlatform.Instance.SignOut()?这是因为你试图遵循封装(或“信息隐藏”)的一般原则,并将所有相关逻辑保持在一个类中,而不是将其分散在许多类中。如果你想把Play游戏换成一个完全不同的多人联网游戏服务,你只需要修改MultiplayerController就可以,你不必更改任何其他代码。回到Unity并打开MainMenu场景,单击层次结构面板中的MainMenuGameObject,你将看到主菜单脚本现在具有当前未定义的“注销按钮“条目。 点击输入文本字段旁边的圆圈,然后从游戏的资源中选择btn-signout,如下所示: 构建并运行你的应用程序。测试登录和注销过程几次,确保其表现一致。确保其表现一致,有退出选项将会非常方便。接下来将做什么?想要更快学习吗?通过我们的视频课程来节省时间。我们这所么多辛苦工作的最终结果是,大量的设置工作已经完成,你终于可以开始构建多人联网游戏的有趣部分了。在这个系列教程的第2部分中,你将解决游戏的实际的多人联网游戏方面的问题。你将添加配对功能,以便你可以将游戏客户端与房间内的其他玩家连接在一起,这个配对也可以在全球范围内进行。你会把对手的车添加到赛车场,你会开始发送和接收游戏消息,让你可以得到这些赛车的状态信息!你可以在这里下载这个部分的一个完成状态的项目。 只需确保添加你自己的Google API客户端ID就行了。在此期间,如果你有任何问题或意见,请随时加入下面的讨论! 【版权声明】原文作者未做权利声明,视为共享知识产权进入公共领域,自动获得授权。
分类:程序新手圈
请勿发表无意义的内容请勿发表重复内容请勿发表交易类内容禁止发表广告宣传贴请使用文明用语其它
淫秽色情政治倾向人身攻击抄袭剽窃广告刷屏恶意挖坟冒充他人其它
登录后参与讨论。点击

我要回帖

更多关于 手机联机游戏 的文章

 

随机推荐