Skip to content

Commit 40e689a

Browse files
committed
- add: Game Service Authentication
- Apple signIn - Google Play Games Service signIn
1 parent b2221e3 commit 40e689a

27 files changed

+692
-16
lines changed

Module/GameService.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Module/GameService/Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#if UNITY_EDITOR && UNITY_IOS && VIRTUESKY_APPLE_AUTH
2+
using AppleAuth.Editor;
3+
using UnityEditor;
4+
using UnityEditor.Callbacks;
5+
using UnityEditor.iOS.Xcode;
6+
7+
8+
namespace VirtueSky.GameServiceEditor
9+
{
10+
public static class SignInWithApplePostprocessor
11+
{
12+
[PostProcessBuild(1)]
13+
public static void OnPostProcessBuild(BuildTarget target, string path)
14+
{
15+
if (target == BuildTarget.iOS || target == BuildTarget.tvOS)
16+
{
17+
var projectPath = PBXProject.GetPBXProjectPath(path);
18+
#if UNITY_2019_3_OR_NEWER
19+
var project = new PBXProject();
20+
project.ReadFromString(System.IO.File.ReadAllText(projectPath));
21+
var manager = new ProjectCapabilityManager(projectPath, "Entitlements.entitlements", null,
22+
project.GetUnityMainTargetGuid());
23+
manager.AddSignInWithAppleWithCompatibility(project.GetUnityFrameworkTargetGuid());
24+
manager.WriteToFile();
25+
#else
26+
var manager =
27+
new ProjectCapabilityManager(projectPath, "Entitlements.entitlements", PBXProject.GetUnityTargetName());
28+
manager.AddSignInWithAppleWithCompatibility();
29+
manager.WriteToFile();
30+
#endif
31+
}
32+
else if (target == BuildTarget.StandaloneOSX)
33+
{
34+
AppleAuthMacosPostprocessorHelper.FixManagerBundleIdentifier(target, path);
35+
}
36+
}
37+
}
38+
}
39+
#endif

Module/GameService/Editor/SignInWithApplePostprocessor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "Wolf.UnityCommon.GameService.Editor",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:09ef5922e2d77498aa246abd9073d05f",
6+
""
7+
],
8+
"includePlatforms": [
9+
"Editor"
10+
],
11+
"excludePlatforms": [],
12+
"allowUnsafeCode": false,
13+
"overrideReferences": false,
14+
"precompiledReferences": [],
15+
"autoReferenced": true,
16+
"defineConstraints": [],
17+
"versionDefines": [],
18+
"noEngineReferences": false
19+
}

Module/GameService/Editor/Wolf.UnityCommon.GameService.Editor.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Module/GameService/Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
using System;
2+
using System.Text;
3+
4+
#if UNITY_IOS && VIRTUESKY_APPLE_AUTH
5+
using AppleAuth;
6+
using AppleAuth.Enums;
7+
using AppleAuth.Extensions;
8+
using AppleAuth.Interfaces;
9+
using AppleAuth.Native;
10+
#endif
11+
12+
using UnityEngine;
13+
using VirtueSky.Inspector;
14+
15+
namespace VirtueSky.GameService
16+
{
17+
[EditorIcon("icon_authentication")]
18+
public class AppleAuthentication : MonoBehaviour
19+
{
20+
[SerializeField] private bool dontDestroyOnLoad;
21+
public static event Action<string> ServerCodeEvent;
22+
public static event Action<string> AuthorizationCodeEvent;
23+
public static event Action<string> UserIdEvent;
24+
public static event Action<StatusLogin> StatusLoginEvent;
25+
private static AppleAuthentication ins;
26+
27+
#if UNITY_IOS && VIRTUESKY_APPLE_AUTH
28+
private IAppleAuthManager _iAppleAuthManager;
29+
#endif
30+
private void Awake()
31+
{
32+
if (dontDestroyOnLoad)
33+
{
34+
DontDestroyOnLoad(gameObject);
35+
}
36+
37+
if (ins == null)
38+
{
39+
ins = this;
40+
}
41+
else
42+
{
43+
Destroy(gameObject);
44+
}
45+
}
46+
47+
private void Start()
48+
{
49+
Init();
50+
}
51+
52+
private void Init()
53+
{
54+
#if UNITY_IOS && VIRTUESKY_APPLE_AUTH
55+
if (AppleAuthManager.IsCurrentPlatformSupported)
56+
{
57+
// Creates a default JSON deserializer, to transform JSON Native responses to C# instances
58+
var deserializer = new PayloadDeserializer();
59+
// Creates an Apple Authentication manager with the deserializer
60+
this._iAppleAuthManager = new AppleAuthManager(deserializer);
61+
}
62+
#endif
63+
}
64+
65+
private void Update()
66+
{
67+
#if UNITY_IOS && VIRTUESKY_APPLE_AUTH
68+
// Updates the AppleAuthManager instance to execute
69+
// pending callbacks inside Unity's execution loop
70+
if (this._iAppleAuthManager != null)
71+
{
72+
this._iAppleAuthManager.Update();
73+
}
74+
#endif
75+
}
76+
77+
private void InternalLogin()
78+
{
79+
#if UNITY_IOS && VIRTUESKY_APPLE_AUTH
80+
var loginArgs =
81+
new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
82+
83+
this._iAppleAuthManager.LoginWithAppleId(
84+
loginArgs,
85+
credential =>
86+
{
87+
// Obtained credential, cast it to IAppleIDCredential
88+
if (credential is IAppleIDCredential appleIdCredential)
89+
{
90+
// Apple User ID
91+
// You should save the user ID somewhere in the device
92+
var userId = appleIdCredential.User;
93+
94+
// Email (Received ONLY in the first login)
95+
var email = appleIdCredential.Email;
96+
97+
// Full name (Received ONLY in the first login)
98+
var fullName = appleIdCredential.FullName;
99+
100+
// Identity token
101+
var identityToken = Encoding.UTF8.GetString(
102+
appleIdCredential.IdentityToken,
103+
0,
104+
appleIdCredential.IdentityToken.Length);
105+
106+
// Authorization code
107+
var authorizationCode = Encoding.UTF8.GetString(
108+
appleIdCredential.AuthorizationCode,
109+
0,
110+
appleIdCredential.AuthorizationCode.Length);
111+
112+
// And now you have all the information to create/login a user in your system
113+
ServerCodeEvent?.Invoke(identityToken);
114+
AuthorizationCodeEvent?.Invoke(authorizationCode);
115+
UserIdEvent?.Invoke(userId);
116+
StatusLoginEvent?.Invoke(StatusLogin.Successful);
117+
}
118+
else
119+
{
120+
StatusLoginEvent?.Invoke(StatusLogin.Failed);
121+
}
122+
},
123+
error =>
124+
{
125+
// Something went wrong
126+
var authorizationErrorCode = error.GetAuthorizationErrorCode();
127+
StatusLoginEvent?.Invoke(StatusLogin.Failed);
128+
});
129+
#endif
130+
}
131+
132+
#region Api
133+
134+
public static void Login() => ins.InternalLogin();
135+
136+
#endregion
137+
}
138+
}

Module/GameService/Runtime/AppleAuthentication.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#if UNITY_ANDROID && VIRTUESKY_GPGS
2+
using GooglePlayGames;
3+
using GooglePlayGames.BasicApi;
4+
#endif
5+
using System;
6+
using UnityEngine;
7+
using VirtueSky.Inspector;
8+
9+
namespace VirtueSky.GameService
10+
{
11+
[EditorIcon("icon_authentication")]
12+
public class GooglePlayGamesAuthentication : MonoBehaviour
13+
{
14+
[SerializeField] private bool dontDestroyOnLoad;
15+
public static event Action<string> ServerCodeEvent;
16+
public static event Action<StatusLogin> StatusLoginEvent;
17+
private static GooglePlayGamesAuthentication ins;
18+
19+
private void Awake()
20+
{
21+
if (dontDestroyOnLoad)
22+
{
23+
DontDestroyOnLoad(gameObject);
24+
}
25+
26+
if (ins == null)
27+
{
28+
ins = this;
29+
}
30+
else
31+
{
32+
Destroy(gameObject);
33+
}
34+
}
35+
36+
private void Start()
37+
{
38+
Init();
39+
}
40+
41+
private void Init()
42+
{
43+
#if UNITY_ANDROID && VIRTUESKY_GPGS
44+
PlayGamesPlatform.Activate();
45+
#endif
46+
}
47+
48+
private void InternalLogin()
49+
{
50+
#if UNITY_ANDROID && VIRTUESKY_GPGS
51+
PlayGamesPlatform.Instance.Authenticate((success) =>
52+
{
53+
if (success == SignInStatus.Success)
54+
{
55+
Debug.Log("Login with Google Play games successful.");
56+
PlayGamesPlatform.Instance.RequestServerSideAccess(true,
57+
code =>
58+
{
59+
Debug.Log("Authorization code: " + code);
60+
ServerCodeEvent?.Invoke(code);
61+
StatusLoginEvent?.Invoke(StatusLogin.Successful);
62+
});
63+
}
64+
else
65+
{
66+
PlayGamesPlatform.Instance.ManuallyAuthenticate(success =>
67+
{
68+
if (success == SignInStatus.Success)
69+
{
70+
PlayGamesPlatform.Instance.RequestServerSideAccess(true,
71+
code =>
72+
{
73+
Debug.Log("Authorization code: " + code);
74+
ServerCodeEvent?.Invoke(code);
75+
StatusLoginEvent?.Invoke(StatusLogin.Successful);
76+
});
77+
}
78+
else
79+
{
80+
Debug.Log("Login Failed");
81+
ServerCodeEvent?.Invoke("");
82+
StatusLoginEvent?.Invoke(StatusLogin.Failed);
83+
}
84+
});
85+
}
86+
});
87+
#endif
88+
}
89+
90+
#region Api
91+
92+
public static void Login() => ins.InternalLogin();
93+
94+
public static bool IsSignIn()
95+
{
96+
#if UNITY_ANDROID && VIRTUESKY_GPGS
97+
return PlayGamesPlatform.Instance.IsAuthenticated();
98+
99+
#else
100+
return false;
101+
#endif
102+
}
103+
104+
#endregion
105+
}
106+
}

Module/GameService/Runtime/GooglePlayGamesAuthentication.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace VirtueSky.GameService
2+
{
3+
public enum StatusLogin
4+
{
5+
NotLoggedIn,
6+
Successful,
7+
Failed
8+
}
9+
}

Module/GameService/Runtime/StatusLogin.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)