美文网首页
Unity接入Google Play Games与Game Ce

Unity接入Google Play Games与Game Ce

作者: Francis_Rose | 来源:发表于2018-01-02 12:44 被阅读3492次

    一、连接Google Play Games账号
    Unity没有直接提供连接Google Play Games账号的接口,但是Google出品了相应的unity包(https://github.com/playgameservices/play-games-plugin-for-unity),可以直接下载后使用current-build目录下的GooglePlayGamesPlugin-x.x.xx.unitypackage包,目前版本是0.9.42。主要步骤可以参考博文http://blog.csdn.net/ad_118/article/details/70139612https://www.cnblogs.com/Colored-Mr/p/6964801.html,以及GitHub上面关于使用该插件的说明,相应点都很明确了。我使用的环境是Unity 5.6.2p3 Android studio 2.2.2。
    需要注意的几点:
    1. 需要在Google Play Console后台配置游戏服务,并且关联到相应的游戏项目;
    2. 将相应的APP ID配置到unity中(Window -> Google Play Games -> Setup -> Android setup)
    3. 在SDK manager设置里面Appearance & Behavior > System Settings > Android SDK的SDK Tools选项卡下面,安装Google Play Services, Android Support Repository以及Google Repository,确保能访问Google后台服务
    4. 在Android工程中的AndroidManifest文件中的application节点下面添加:

    1>. 对应的Google Play Games配置

              <!-- The space in these forces it to be interpreted as a string vs. int -->
              <meta-data android:name="com.google.android.gms.games.APP_ID"
                      android:value="\ xxxxxxxxxxx" />
              <!-- Keep track of which plugin is being used -->
              <meta-data android:name="com.google.android.gms.games.unityVersion"
                      android:value="\ xxxxx" />
               <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
               <activity android:name="com.google.games.bridge.NativeBridgeActivity"
                       android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
    

    2>. 增加权限

              <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
              <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    
              note: xxxxxx处要替换成对应的ID,gms.version的value处会爆红,但是不会出错,有相应的地方会有这个值。
    

    授权代码:

    1. 配置 (如果要存档游戏,需要在游戏服务设置中允许,配置中添加EnableSavedGame()配置)

       static PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
       .RequestEmail()
       .RequestServerAuthCode(false)
       .RequestIdToken()
       .Build();
      
    2. 初始化 (只需要初始化一次,Start()函数执行即可)

      PlayGamesPlatform.InitializeInstance(config);
      PlayGamesPlatform.DebugLogEnabled = Const.UseDebugLogMode;
      PlayGamesPlatform.Activate();
      
    3. 登录授权

       UnityEngine.Social.Active.localUser.Authenticate((bool success) => {
               if (success)
               {
                     CustomDebug.DebugLog("authenticate success");
                     PlayGamesLocalUser user = (PlayGamesLocalUser)UnityEngine.Social.localUser;
                     CustomDebug.DebugLogFormat("UserName: {0} id: {1} Avatar URL: {2} Email: {3} Token: {4}",
                                              ((PlayGamesLocalUser)UnityEngine.Social.localUser).userName,
                                              ((PlayGamesLocalUser)UnityEngine.Social.localUser).id,
                                              ((PlayGamesLocalUser)UnityEngine.Social.localUser).AvatarURL,
                                              ((PlayGamesLocalUser)UnityEngine.Social.localUser).Email,
                                              ((PlayGamesLocalUser)UnityEngine.Social.localUser).GetIdToken());
               }
               else
                {
                       CustomDebug.DebugLog("authenticate failed");
                }
       });
      

    二、接入iOS Game Center账号
    由于unity直接提供了Game center服务的相应的接口,所以接入Game Center账号比较容易:

        private void AccessGameCenter()
        {
              UnityEngine.Social.localUser.Authenticate (AccessGameCenterCallback);
        }
    
        private void AccessGameCenterCallback(bool success)
        {
                if(success) {}
                else{}
        }
    

    在对应的工程设置中要开启Game Center服务

    接入游戏账号后与服务端协商对应登录接口即可

    相关文章

      网友评论

          本文标题:Unity接入Google Play Games与Game Ce

          本文链接:https://www.haomeiwen.com/subject/psyfnxtx.html