Introduction and Implementation of MaxSDK for Unity Cross‑Platform Game Development
The article presents MaxSDK, a unified C# SDK for Unity that abstracts platform‑specific native SDKs across Android, iOS, Windows, macOS and future platforms, offering a singleton interface, abstract base classes, data models, and callback listeners to dramatically cut integration effort, maintenance cost, and enable easy extension to other engines.
The article introduces MaxSDK, a cross‑engine, cross‑platform unified SDK designed to simplify the integration of third‑party SDKs (e.g., advertising, payment, analytics) for Unity‑based games. It explains the business pain points: multiple platforms (Android, iOS, HarmonyOS, mini‑games) and multiple internal business lines (37网游, 37手游, 37Games) require separate native SDK integrations, leading to high development and communication costs.
MaxSDK provides a single C# interface (e.g., for Unity) that abstracts away platform‑specific details, allowing developers to call a unified API and reducing the effort needed to support new platforms or business lines.
1. Overall Architecture
The SDK is divided into two main layers:
Engine Layer : Unity C# code that developers interact with. It contains the singleton entry class MaxSDK , the abstract base class MaxSDKUnitySupportBase , and data models (e.g., MaxSDKInitInfo , MaxSDKLoginInfo , MaxSDKPayInfo ).
Platform Layer : Platform‑specific implementations (Android, iOS, Windows, macOS). Each implementation bridges C# calls to native code via Unity’s AndroidJavaObject or iOS DLLImport mechanisms.
2. Core Interface Design
The abstract base class defines all SDK operations as abstract methods:
namespace maxsdk {
public abstract class MaxSDKUnitySupportBase {
public abstract int GetSDKType();
public abstract void SetListener(MaxSDKListener listener);
public abstract void Init(MaxSDKInitInfo info);
public abstract void Login(MaxSDKLoginInfo info);
public abstract void Logout(MaxSDKLogoutInfo info);
public abstract void Pay(MaxSDKPayInfo info);
public abstract void ReportRoleInfo(MaxSDKRoleInfo info);
public abstract void SwitchAccount(MaxSDKSwitchAccountInfo info);
public abstract void ReportEvent(MaxSDKReportEventInfo info);
public abstract void OpenActionExt(MaxSDKActionInfo info);
public abstract void Share(MaxSDKShareInfo info);
public abstract string DispatchSync(MaxSDKDispatchInfo info);
public abstract void DispatchASync(MaxSDKDispatchInfo info);
public abstract bool IsActionSupported(int type);
// virtual helpers omitted for brevity
}
}The singleton entry class ensures a single access point for the whole game:
namespace maxsdk {
public sealed class MaxSDK {
private static readonly MaxSDK INSTANCE = new MaxSDK();
private MaxSDKUnitySupportBase supportBase;
public static MaxSDK GetInstance() { return INSTANCE; }
private MaxSDK() {
Debug.Log("Starting Unity‑Platform bridge, current platform: " + Application.platform);
#if UNITY_ANDROID && !UNITY_EDITOR
supportBase = new MaxSDKUnitySupportAndroid();
#elif UNITY_IOS && !UNITY_EDITOR
supportBase = new MaxSDKUnitySupportIOS();
#elif UNITY_STANDALONE_WIN && !UNITY_EDITOR
supportBase = new MaxSDKUnitySupportWin();
#endif
}
// public wrappers that forward to supportBase (Init, Login, Pay, etc.)
}
}3. Data Models
All request parameters inherit from MaxSDKBaseInfo (which contains an extData JSON field). Example for payment:
namespace maxsdk {
public class MaxSDKBaseInfo { public string extData; }
public class MaxSDKPayInfo : MaxSDKBaseInfo {
public string productId;
public string productName;
public string serverId;
public string serverName;
public string roleId;
public string roleName;
public string roleType = "";
public int roleLevel;
public string roleBronLevel = "";
public string roleLevelMTime;
public string partyName;
public string cpOrderId;
public string orderTime;
public string sign;
public int radio = 10;
public float money;
public int gameCoin = -1;
public bool subscription;
}
}Response beans inherit from MaxSDKBaseBean , e.g., MaxSDKLoginBean contains token , puid , uid , uname .
4. Platform‑Specific Implementations
Android implementation uses AndroidJavaObject to call Java SDK methods. iOS implementation uses native DLL imports:
namespace maxsdk {
#if UNITY_IOS && !UNITY_EDITOR
public class MaxSDKUnitySupportIOS : MaxSDKUnitySupportBase {
public override int GetSDKType() {
string typeStr = MaxSDK_Call_Sync("getSDKType", "");
int sdkType; int.TryParse(typeStr, out sdkType);
return sdkType;
}
public override void SetListener(MaxSDKListener listener) {
if (listener == null) { Debug.LogError("listener is null"); return; }
MaxSDK_Call_Async("setGameObject", listener.gameObject.name);
}
public override void Init(MaxSDKInitInfo info) {
MaxSDK_Call_Async("init", JsonConvert.SerializeObject(info, Formatting.Indented));
}
// other overrides (Login, Pay, Share, etc.) omitted for brevity
[DllImport("__Internal")]
private static extern void MaxSDK_Call_Async(string method, string data);
[DllImport("__Internal")]
private static extern string MaxSDK_Call_Sync(string method, string data);
}
#endif
}Both Android and iOS implementations forward calls to native SDKs and receive callbacks via Unity’s UnityPlayer.UnitySendMessage mechanism.
5. Callback Design
All callbacks are routed through a unified listener class that inherits from MonoBehaviour . The listener defines methods such as CallInitSuccess(string json) , CallLoginSuccess(string json) , etc., which deserialize the JSON payload and invoke abstract methods ( OnInitSuccess , OnLoginSuccess , …) that game developers implement.
namespace maxsdk {
public abstract class MaxSDKListener : MonoBehaviour {
public void CallInitSuccess(string json) { OnInitSuccess(JsonConvert.DeserializeObject
(json)); }
public void CallInitFail(string json) { OnInitFail(JsonToErrorInfo(json)); }
public abstract void OnInitSuccess(MaxSDKInitBean bean);
public abstract void OnInitFail(MaxSDKFailBean bean);
// similar methods for Login, Pay, Logout, etc.
}
}6. Demo Integration
A simple Unity demo shows how to bind UI buttons to SDK calls. Example for a login button:
// SQWYUnityEventHandler.cs
public void OnClickLogin() {
#if UNITY_EDITOR
EditorUtility.DisplayDialog("提示", "点击了登录", "Yes", "No");
#endif
#if UNITY_ANDROID || UNITY_IOS || UNITY_STANDALONE_WIN
MaxSDKLoginInfo info = new MaxSDKLoginInfo();
MaxSDK.GetInstance().Login(info);
#endif
}The demo demonstrates the full flow: UI → C# singleton → platform bridge → native SDK → Unity callback.
7. Export & Distribution
Export Android project (update assets and jniLibs directories).
Export a .unitypackage containing all C# source files and dependencies.
Developers import the package into their Unity projects via Assets → Import Package → Custom Package .
8. Extensibility
The architecture supports adding new engines (Cocos, Unreal) and new platforms (HarmonyOS, mini‑games) by implementing additional MaxSDKUnitySupportXXX classes and extending the data model. The layered design (engine ↔ bridge ↔ platform) ensures low coupling and easy maintenance.
Conclusion
MaxSDK provides a unified, layered solution that reduces SDK integration cost for Unity game projects, improves code maintainability, and can be extended to other engines and platforms.
37 Interactive Technology Team
37 Interactive Technology Center
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.