add Tech3CSDK
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
package vn.tech3c.sdk.unity;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import vn.tech3c.sdk.auth.callback.OnAuthCallback;
|
||||
import vn.tech3c.sdk.auth.controller.Tech3CIdController;
|
||||
import vn.tech3c.sdk.auth.entities.enums.LoginType;
|
||||
import vn.tech3c.sdk.auth.exceptions.Tech3CIdException;
|
||||
|
||||
import com.unity3d.player.UnityPlayer;
|
||||
|
||||
/**
|
||||
* Unity Bridge for Tech3C SDK
|
||||
* This class receives callbacks from Tech3C SDK and sends them to Unity via UnitySendMessage
|
||||
*/
|
||||
public class Tech3CUnityBridge implements OnAuthCallback {
|
||||
private static final String TAG = "Tech3CUnityBridge";
|
||||
private static final String UNITY_GAME_OBJECT = "Tech3CSDKBridge";
|
||||
|
||||
private static Tech3CUnityBridge instance;
|
||||
|
||||
private OnUnityAuthCallback unityAuthCallback;
|
||||
private OnUnityLogoutCallback unityLogoutCallback;
|
||||
private OnUnityUserInfoCallback unityUserInfoCallback;
|
||||
|
||||
// Singleton
|
||||
public static synchronized Tech3CUnityBridge getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new Tech3CUnityBridge();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Interfaces for Unity callbacks
|
||||
public interface OnUnityAuthCallback {
|
||||
void onAuthSuccess(String userId, String password, String accessToken, String refreshToken, LoginType loginType, long expiryTime);
|
||||
void onAuthCancelled();
|
||||
void onAuthError(int errorCode, String errorMessage);
|
||||
}
|
||||
|
||||
public interface OnUnityLogoutCallback {
|
||||
void onLogoutSuccess();
|
||||
void onLogoutError(int errorCode, String errorMessage);
|
||||
}
|
||||
|
||||
public interface OnUnityUserInfoCallback {
|
||||
void onUserInfoReceived(String userId, String username, String email, String phone);
|
||||
void onUserInfoCancelled();
|
||||
void onUserInfoError(int errorCode, String errorMessage);
|
||||
}
|
||||
|
||||
// Set callbacks
|
||||
public void setUnityAuthCallback(OnUnityAuthCallback callback) {
|
||||
this.unityAuthCallback = callback;
|
||||
}
|
||||
|
||||
public void setUnityLogoutCallback(OnUnityLogoutCallback callback) {
|
||||
this.unityLogoutCallback = callback;
|
||||
}
|
||||
|
||||
public void setUnityUserInfoCallback(OnUnityUserInfoCallback callback) {
|
||||
this.unityUserInfoCallback = callback;
|
||||
}
|
||||
|
||||
// Send message to Unity
|
||||
private void sendToUnity(String methodName, String message) {
|
||||
try {
|
||||
UnityPlayer.UnitySendMessage(UNITY_GAME_OBJECT, methodName, message);
|
||||
Log.d(TAG, "Sent to Unity: " + methodName + " - " + message);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error sending to Unity: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// ========== OnAuthCallback Implementation ==========
|
||||
|
||||
@Override
|
||||
public void onLoginSuccess(String userId, String password, String accessToken, String refreshToken, LoginType loginType, long expiryTime) {
|
||||
Log.d(TAG, "onLoginSuccess: " + userId);
|
||||
|
||||
// Send to Unity
|
||||
String message = userId + "|" + password + "|" + accessToken + "|" + refreshToken + "|" + loginType.name() + "|" + expiryTime;
|
||||
sendToUnity("OnAuthSuccess", message);
|
||||
|
||||
// Also call Unity callback if set
|
||||
if (unityAuthCallback != null) {
|
||||
unityAuthCallback.onAuthSuccess(userId, password, accessToken, refreshToken, loginType, expiryTime);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRegisterSuccess(String accessToken, String refreshToken, String userId, long expiryTime) {
|
||||
Log.d(TAG, "onRegisterSuccess: " + userId);
|
||||
|
||||
// Treat register success as auth success (password is empty for register)
|
||||
String message = userId + "|" + "" + "|" + accessToken + "|" + refreshToken + "|REGISTER|" + expiryTime;
|
||||
sendToUnity("OnAuthSuccess", message);
|
||||
|
||||
if (unityAuthCallback != null) {
|
||||
unityAuthCallback.onAuthSuccess(userId, "", accessToken, refreshToken, LoginType.ACCOUNT, expiryTime);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthCancelled() {
|
||||
Log.d(TAG, "onAuthCancelled");
|
||||
sendToUnity("OnAuthCancelled", "");
|
||||
|
||||
if (unityAuthCallback != null) {
|
||||
unityAuthCallback.onAuthCancelled();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAuthScreenOpened() {
|
||||
Log.d(TAG, "onAuthScreenOpened");
|
||||
// Optional: Send to Unity if needed
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Tech3CIdException exception) {
|
||||
Log.d(TAG, "onError: " + exception.getMessage());
|
||||
|
||||
String message = exception.getErrorCode() + "|" + exception.getMessage();
|
||||
sendToUnity("OnAuthError", message);
|
||||
|
||||
if (unityAuthCallback != null) {
|
||||
unityAuthCallback.onAuthError(exception.getErrorCode(), exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Additional Callback Methods ==========
|
||||
|
||||
public void onLogoutSuccess() {
|
||||
Log.d(TAG, "onLogoutSuccess");
|
||||
sendToUnity("OnLogoutSuccess", "");
|
||||
|
||||
if (unityLogoutCallback != null) {
|
||||
unityLogoutCallback.onLogoutSuccess();
|
||||
}
|
||||
}
|
||||
|
||||
public void onLogoutError(int errorCode, String errorMessage) {
|
||||
Log.d(TAG, "onLogoutError: " + errorMessage);
|
||||
|
||||
String message = errorCode + "|" + errorMessage;
|
||||
sendToUnity("OnLogoutError", message);
|
||||
|
||||
if (unityLogoutCallback != null) {
|
||||
unityLogoutCallback.onLogoutError(errorCode, errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public void onUserInfoReceived(String userId, String username, String email, String phone) {
|
||||
Log.d(TAG, "onUserInfoReceived: " + userId);
|
||||
|
||||
String message = userId + "|" + username + "|" + email + "|" + phone;
|
||||
sendToUnity("OnUserInfoReceived", message);
|
||||
|
||||
if (unityUserInfoCallback != null) {
|
||||
unityUserInfoCallback.onUserInfoReceived(userId, username, email, phone);
|
||||
}
|
||||
}
|
||||
|
||||
public void onUserInfoCancelled() {
|
||||
Log.d(TAG, "onUserInfoCancelled");
|
||||
sendToUnity("OnUserInfoCancelled", "");
|
||||
|
||||
if (unityUserInfoCallback != null) {
|
||||
unityUserInfoCallback.onUserInfoCancelled();
|
||||
}
|
||||
}
|
||||
|
||||
public void onUserInfoError(int errorCode, String errorMessage) {
|
||||
Log.d(TAG, "onUserInfoError: " + errorMessage);
|
||||
|
||||
String message = errorCode + "|" + errorMessage;
|
||||
sendToUnity("OnUserInfoError", message);
|
||||
|
||||
if (unityUserInfoCallback != null) {
|
||||
unityUserInfoCallback.onUserInfoError(errorCode, errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user