-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhichao Link
committed
Aug 10, 2023
1 parent
348b286
commit 07a12d6
Showing
74 changed files
with
5,251 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
ParticleWebGLDemo/ParticleWebGLDemo/Assets/Plugins/WebGL.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
ParticleWebGLDemo/ParticleWebGLDemo/Assets/Plugins/WebGL/ParticleAuthWebGL.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
248 changes: 248 additions & 0 deletions
248
ParticleWebGLDemo/ParticleWebGLDemo/Assets/Plugins/WebGL/ParticleAuthWebGL/ParticleAuth.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
using System.Runtime.InteropServices; | ||
|
||
|
||
public class ParticleAuth : MonoBehaviour { | ||
|
||
[DllImport("__Internal")] | ||
private static extern void InitParticleAuth(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void SetParticleLanguage(string language); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void SetParticleFiatCoin(string fiatCoin); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void SetParticleAuthTheme(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void LoginWithParticle(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void LogoutParticle(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern int IsParticleLoggedIn(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern string GetParticleUserInfo(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void OpenParticleAccountAndSecurity(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void GetParticleSecurityAccount(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void OpenParticleWallet(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void OpenParticleBuy(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern string GetParticleWalletAddress(); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ParticleSwitchChain(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ParticleEVMSendTransaction(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ParticleEVMPersonalSign(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ParticleEVMPersonalSignUniq(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ParticleEVMSignTypedData(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ParticleEVMSignTypedDataUniq(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ParticleSolanaSignAndSendTransaction(string options); | ||
|
||
[DllImport("__Internal")] | ||
private static extern void ParticleSolanasSignMessage(string options); | ||
|
||
public static ParticleAuth Instance; | ||
|
||
public string config; | ||
|
||
|
||
private TaskCompletionSource<string> loginTask; | ||
private TaskCompletionSource<bool> logoutTask; | ||
private TaskCompletionSource<string> getSecurityAccountTask; | ||
private TaskCompletionSource<string> switchChainTask; | ||
private TaskCompletionSource<string> evmSendTransactionTask; | ||
private TaskCompletionSource<string> evmPersonalSignTask; | ||
private TaskCompletionSource<string> evmPersonalSignUniqTask; | ||
private TaskCompletionSource<string> evmSignTypedDataTask; | ||
private TaskCompletionSource<string> evmSignTypedDataUniqTask; | ||
private TaskCompletionSource<string> solanaSignAndSendTransactionTask; | ||
private TaskCompletionSource<string> solanasSignMessageTask; | ||
|
||
|
||
|
||
void Awake() { | ||
Instance = this; | ||
// replace xxxx with particle project config | ||
// {"projectId":"34c6b829-5b89-44e8-90a9-6d982787b9c9","clientKey":"c6Z44Ml4TQeNhctvwYgdSv6DBzfjf6t6CB0JDscR","appId":"64f36641-b68c-4b19-aa10-5c5304d0eab3","chainName":"Ethereum","chainId":1}' | ||
InitParticleAuth(config); | ||
} | ||
|
||
void OnDestroy() { | ||
Instance = null; | ||
} | ||
|
||
public void SetLanguage(string language) { | ||
SetParticleLanguage(language); | ||
} | ||
|
||
public void SetFiatCoin(string fiatCoin) { | ||
SetParticleFiatCoin(fiatCoin); | ||
} | ||
|
||
public void SetAuthTheme(string options) { | ||
SetParticleAuthTheme(options); | ||
} | ||
|
||
public Task<string> Login(string options) { | ||
loginTask = new TaskCompletionSource<string>(); | ||
LoginWithParticle(options); | ||
return loginTask.Task; | ||
} | ||
|
||
// Called from browser | ||
public void OnLogin(string json) { | ||
loginTask?.TrySetResult(json); | ||
} | ||
|
||
public Task<bool> Logout() { | ||
logoutTask = new TaskCompletionSource<bool>(); | ||
LogoutParticle(); | ||
return logoutTask.Task; | ||
} | ||
|
||
// Called from browser | ||
public void OnLogout() { | ||
logoutTask?.TrySetResult(true); | ||
} | ||
|
||
public bool IsLoggedIn() { | ||
return IsParticleLoggedIn() == 1; | ||
} | ||
|
||
public string GetUserInfo() { | ||
return GetParticleUserInfo(); | ||
} | ||
|
||
public void OpenAccountAndSecurity() { | ||
OpenParticleAccountAndSecurity(); | ||
} | ||
|
||
public Task<string> GetSecurityAccount() { | ||
getSecurityAccountTask = new TaskCompletionSource<string>(); | ||
GetParticleSecurityAccount(); | ||
return getSecurityAccountTask.Task; | ||
} | ||
|
||
public void OnGetSecurityAccount(string json) { | ||
getSecurityAccountTask?.TrySetResult(json); | ||
} | ||
|
||
public void OpenWallet() { | ||
OpenParticleWallet(); | ||
} | ||
|
||
public void OpenBuy(string options) { | ||
OpenParticleBuy(options); | ||
} | ||
|
||
public string GetWalletAddress() { | ||
return GetParticleWalletAddress(); | ||
} | ||
|
||
public Task<string> SwitchChain(string options) { | ||
switchChainTask = new TaskCompletionSource<string>(); | ||
ParticleSwitchChain(options); | ||
return switchChainTask.Task; | ||
} | ||
|
||
public void OnSwitchChain(string json) { | ||
switchChainTask?.TrySetResult(json); | ||
} | ||
|
||
public Task<string> EVMSendTransaction(string options) { | ||
evmSendTransactionTask = new TaskCompletionSource<string>(); | ||
ParticleEVMSendTransaction(options); | ||
return evmSendTransactionTask.Task; | ||
} | ||
|
||
public void OnEVMSendTransaction(string json) { | ||
evmSendTransactionTask?.TrySetResult(json); | ||
} | ||
|
||
public Task<string> EVMPersonalSign(string options) { | ||
evmPersonalSignTask = new TaskCompletionSource<string>(); | ||
ParticleEVMPersonalSign(options); | ||
return evmPersonalSignTask.Task; | ||
} | ||
|
||
public void OnEVMPersonalSign(string json) { | ||
evmPersonalSignTask?.TrySetResult(json); | ||
} | ||
|
||
public Task<string> EVMPersonalSignUniq(string options) { | ||
evmPersonalSignUniqTask = new TaskCompletionSource<string>(); | ||
ParticleEVMPersonalSignUniq(options); | ||
return evmPersonalSignUniqTask.Task; | ||
} | ||
|
||
public void OnEVMPersonalSignUniq(string json) { | ||
evmPersonalSignUniqTask?.TrySetResult(json); | ||
} | ||
|
||
public Task<string> EVMSignTypedData(string options) { | ||
evmSignTypedDataTask = new TaskCompletionSource<string>(); | ||
ParticleEVMSignTypedData(options); | ||
return evmSignTypedDataTask.Task; | ||
} | ||
|
||
public void OnEVMSignTypedData(string json) { | ||
evmSignTypedDataTask?.TrySetResult(json); | ||
} | ||
|
||
public Task<string> EVMSignTypedDataUniq(string options) { | ||
evmSignTypedDataUniqTask = new TaskCompletionSource<string>(); | ||
ParticleEVMSignTypedDataUniq(options); | ||
return evmSignTypedDataUniqTask.Task; | ||
} | ||
|
||
public void OnEVMSignTypedDataUniq(string json) { | ||
evmSignTypedDataUniqTask?.TrySetResult(json); | ||
} | ||
|
||
public Task<string> SolanaSignAndSendTransaction(string options) { | ||
solanaSignAndSendTransactionTask = new TaskCompletionSource<string>(); | ||
ParticleSolanaSignAndSendTransaction(options); | ||
return solanaSignAndSendTransactionTask.Task; | ||
} | ||
|
||
public void OnSolanaSignAndSendTransaction(string json) { | ||
solanaSignAndSendTransactionTask?.TrySetResult(json); | ||
} | ||
|
||
public Task<string> SolanasSignMessage(string options) { | ||
solanasSignMessageTask = new TaskCompletionSource<string>(); | ||
ParticleSolanasSignMessage(options); | ||
return solanasSignMessageTask.Task; | ||
} | ||
|
||
public void OnSolanasSignMessage(string json) { | ||
solanasSignMessageTask?.TrySetResult(json); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...leWebGLDemo/ParticleWebGLDemo/Assets/Plugins/WebGL/ParticleAuthWebGL/ParticleAuth.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
89 changes: 89 additions & 0 deletions
89
...cleWebGLDemo/ParticleWebGLDemo/Assets/Plugins/WebGL/ParticleAuthWebGL/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Unity WebGL & Particle Auth | ||
|
||
1. Copy `ParticleAuthWebGL` to `Assets/Plugins/WebGL`. | ||
2. Bind `ParticleAuth.cs` to GameObject, named "ParticleAuth", then add `config` property value. | ||
3. Custom WebGL templates, add function `SendMessage` in `index.html`, refer to the following code: | ||
|
||
```js | ||
//add SendMessage function | ||
var _unityInstance = null; | ||
function SendMessage(objectName, methodName, value) { | ||
_unityInstance?.SendMessage(objectName, methodName, value); | ||
} | ||
|
||
createUnityInstance(canvas, config, (progress) => { | ||
// loading progress | ||
}).then((unityInstance) => { | ||
// set global unityInstance | ||
_unityInstance = unityInstance; | ||
}); | ||
``` | ||
Function params refer to [Particle Docs](https://docs.particle.network/developers/auth-service/sdks/web) | ||
Learn more about [WebGL templates](https://docs.unity3d.com/2020.3/Documentation/Manual/webgl-templates.html). | ||
Learn more about [Interaction with browser scripting](https://docs.unity3d.com/cn/2023.2/Manual/webgl-interactingwithbrowserscripting.html). | ||
## Interaction | ||
```C# | ||
|
||
// login | ||
var userInfo = await ParticleAuth.Instance.Login(); | ||
|
||
// logout | ||
var result = await ParticleAuth.Instance.Logout(); | ||
|
||
// check is logged in | ||
var result = ParticleAuth.Instance.IsLoggedIn(); | ||
|
||
// get address | ||
var address = ParticleAuth.Instance.GetWalletAddress(); | ||
|
||
// get user info | ||
var userInfo = ParticleAuth.Instance.GetUserInfo(); | ||
|
||
// get security account | ||
var securityAccount = await ParticleAuth.Instance.GetSecurityAccount(); | ||
|
||
// set language | ||
ParticleAuth.Instance.SetLanguage(language) | ||
|
||
// set fiat coin | ||
ParticleAuth.Instance.SetFiatCoin(fiatCoin) | ||
|
||
// set auth theme | ||
ParticleAuth.Instance.SetAuthTheme(options) | ||
|
||
// open wallet | ||
ParticleAuth.Instance.OpenWallet(); | ||
|
||
// open buy | ||
ParticleAuth.Instance.OpenBuy(options); | ||
|
||
// switch chain | ||
var result = await ParticleAuth.Instance.SwitchChain(options); | ||
|
||
// evm: send transaction | ||
var result = await ParticleAuth.Instance.EVMSendTransaction(options); | ||
|
||
// evm: personal sign | ||
var result = await ParticleAuth.Instance.EVMPersonalSign(options); | ||
|
||
// evm: personal sign uniq | ||
var result = await ParticleAuth.Instance.EVMPersonalSignUniq(options); | ||
|
||
// evm: sign typed data | ||
var result = await ParticleAuth.Instance.EVMSignTypedData(options); | ||
|
||
// evm: sign typed data uniq | ||
var result = await ParticleAuth.Instance.EVMSignTypedDataUniq(options); | ||
|
||
// solana: send transaction | ||
var result = await ParticleAuth.Instance.SolanaSignAndSendTransaction(options); | ||
|
||
// solana: sign message | ||
var result = await ParticleAuth.Instance.SolanasSignMessage(options); | ||
|
||
``` |
7 changes: 7 additions & 0 deletions
7
ParticleWebGLDemo/ParticleWebGLDemo/Assets/Plugins/WebGL/ParticleAuthWebGL/README.md.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.