1.初始化
SDK会在Unity启动前就初始化好,但是又有Init的接口,所以这里通过
StarkSDK.s_ContainerEnv 判断有没有初始化,没有的话就手动初始化
public override void Init(string code, Action callback)
{
Debug.Log("初始化抖音SDK");
if (StarkSDK.s_ContainerEnv == null)
{
Debug.LogError("没有初始化完毕,设置回调");
//base.Init(code, callback);
//设置回调
StarkSDK.API.SetContainerInitCallback((env) =>
{
Debug.Log("抖音sdk init finish");
//callback?.Invoke();
Login(callback);
});
StarkSDK.Init();
}
else
{
Debug.Log("已经初始化了,直接回调");
Login(callback);
//callback?.Invoke();
}
}
2.登录
登录后可以获得一系列玩家数据,anonymousCode可以作为玩家的唯一ID
登录之后云函数才能获得对应的玩家OpenID
public void Login(Action action)
{
StarkSDK.API.GetAccountManager().Login((code, anonymousCode, isLogin) =>
{
Debug.Log($"玩家已经登录:{
code}, {
anonymousCode}, {
isLogin}");
isLogined = isLogin;
unionID = anonymousCode;
action?.Invoke();
}, (errormsg) =>
{
Debug.LogError($"玩家登录失败:{
errormsg},用户无法使用存档");
isLogined = false;
unionID = SystemInfo.deviceUniqueIdentifier;
action?.Invoke();
}, false);
}
3.设置剪贴板
public override void Copy2ClipBoard(string val)
{
//base.Copy2ClipBoard(val);
StarkSDK.API.GetStarkClipboard().SetClipboardData(val, (success, str) =>
{
if (!success) Debug.LogError("复制到剪贴版失败");
});
}
4.设置前后台切换监听
public override void SetFocusListener(Action<bool> onFocus)
{
//base.SetFocusListener(onFocus);
StarkSDK.API.GetStarkAppLifeCycle().OnShowWithDict = (param) =>
{
onFocus(true);
};
StarkSDK.API.GetStarkAppLifeCycle().OnHide = () =>
{
onFocus(false);
};
}
5.获取玩家信息
public override void GetPlayerName(Action<string> callback)
{
base.GetPlayerName(callback);
StarkSDK.API.GetAccountManager().GetScUserInfo((ref ScUserInfo user) =>
{
Debug.Log($"获取到玩家名:{
user.nickName}");
SystemHelper.PlayerInfo.GetPlayer().SetPlayerName(user.nickName);
}, (error) =>
{
Debug.Log($"获取玩家名字失败:{
error}");
});
}
6.震动
WebGL下,传入参数大于400的话就是长震动,否则就是短震动
static long[] virbrateLongParam = new long[] {
9999 };
public override void VirbrateLong()
{
//base.VirbrateLong();
StarkSDK.API.Vibrate(virbrateLongParam);
}
static long[] virbrateShortParam = new long[] {
10 };
public override void VirbrateShort()
{
//base.VirbrateLong();
StarkSDK.API.Vibrate(virbrateShortParam);
}
7.云函数调用
云函数相关可以详见:【Unity】Unity项目转抖音小游戏(二)云数据库和云函数
public override void CallCloudFunction(string functionName, string json, Action<string> success, Action<string> fail)
{
StarkSDK.API.GetStarkDouyinCloudManager().CallContainer(
DYCloudID,
DYServerID,
functionName,
new StarkDouyinCloud.Options()
{
Method = "POST",
Data = json
},
(res) =>
{
success?.Invoke(res.Data);
Debug.Log($"云函数{
functionName}成功:{
res.Data}");
},
(res) =>
{
fail?.Invoke(res..ErrMsg);
Debug.LogError($"云函数:{
res.StatusCode}失败,{
res.ErrMsg}");
}); ;
}