SDK平台能力适配
为降低Unity开发者使用平台能力门槛,在vivo转换插件中我们内置了C# SDK,开发者可使用熟悉的C#语言进行平台能力的使用。
注意: 游戏进度存储使用Unity的PlayerPrefs无法生效,SDK提供了vivo小游戏平台的PlayerPrefs,需要区分使用。
已实现的平台能力及用法如下:
系统
创建桌面图标
C# 示例如下,对应javascript平台API文档。
QG.InstallShortcut(
"",
(msg) => { Debug.Log("QG.InstallShortcut success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.InstallShortcut fail = " + msg.errMsg); }
);
获取桌面图标是否创建
C# 示例如下,对应javascript平台API文档。
QG.HasShortcutInstalled(
(msg) => { Debug.Log("QG.HasShortcutInstalled success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.HasShortcutInstalled fail = " + msg.errMsg); }
);
获取系统信息 15.1+
C# 示例如下,对应javascript平台API文档。
QG.GetSystemInfo(
(msg) => { Debug.Log("QG.GetSystemInfo success = " + msg.data); },
(msg) => { Debug.Log("QG.GetSystemInfo fail = " + msg.errMsg); }
);
同步获取系统信息 15.1+
C# 示例如下,对应javascript平台API文档。
var infoSync = QG.GetSystemInfoSync();
Debug.Log("QG.GetSystemInfoSync" + infoSync);
退出游戏
C# 示例如下
QG.ExitApplication();
判断是否是vivo运行环境
C# 示例如下
QG.IsVivoRuntime();
设备
修改剪贴板内容 15.1+
C# 示例如下,对应javascript平台API文档。
QG.SetClipboardData("text",
(msg) => { Debug.Log("QG.SetClipboardData success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.SetClipboardData fail = " + msg.errMsg); }
);
读取剪贴板内容 15.1+
C# 示例如下,对应javascript平台API文档。
QG.GetClipboardData(
(msg) => { Debug.Log("QG.GetClipboardData success = " + msg.data); },
(msg) => { Debug.Log("QG.GetClipboardData fail = " + msg.errMsg); }
);
厂商服务
登录功能
C# 示例如下,对应javascript平台API文档。
QG.Login(
(msg) => { Debug.Log("QG.Login success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.Login fail = " + msg.errMsg); }
);
获取用户信息
C# 示例如下,对应javascript平台API文档。
QG.GetUserInfo(
(msg) => { Debug.Log("QG.GetUserInfo success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.GetUserInfo fail = " + msg.errMsg); }
);
Pay 支付
C# 示例如下,对应javascript平台API文档。
PayParam param = new PayParam()
{
appId = "123",
cpOrderNumber = "234",
productName = "xxxx",
productDesc = "dddd",
orderAmount = 1,
notifyUrl = "mmnmn",
expireTime = "2565",
extInfo = "8955",
vivoSignature = "98898"
};
QG.Pay(
param,
(msg) => { Debug.Log("QG.Pay success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.Pay fail = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.Pay cancel = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.Pay complete = " + JsonUtility.ToJson(msg)); }
);
V订阅
// 获取订阅状态
QG.GetStatus(
(msg) => { Debug.Log("QG.GetStatus success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.GetStatus fail = " + msg.errMsg); });
// 订阅
SubscribeParam param = new SubscribeParam()
{
templateIds = "0078ecfa90d44d119ab2c12c90794da3",
clientId = "10000186",
userId = "000",
scene = "aaa",
type = 2,
subDesc = "222"
};
QG.Subscribe(
param,
(msg) => { Debug.Log("QG.Subscribe success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.Subscribe fail = " + msg.errMsg); });
// 取消订阅
SubscribeParam param = new SubscribeParam()
{
templateIds = "0078ecfa90d44d119ab2c12c90794da3",
clientId = "10000186",
userId = "000",
scene = "aaa",
type = 2,
subDesc = "222"
};
QG.UnSubscribe(
param,
(msg) => { Debug.Log("QG.UnSubscribe success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.UnSubscribe fail = " + msg.errMsg); });
// IsRelationExist是否存在关系
SubscribeParam param = new SubscribeParam()
{
templateIds = "0078ecfa90d44d119ab2c12c90794da3",
clientId = "10000186",
userId = "000",
scene = "aaa",
type = 2,
subDesc = "222"
};
QG.IsRelationExist(
param,
(msg) => { Debug.Log("QG.UnSubscribe success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.UnSubscribe fail = " + msg.errMsg); });
广告
创建Banner广告
C# 示例如下,对应javascript平台API文档。
bannerAd = QG.CreateBannerAd(new QGCreateBannerAdParam()
{
posId = "fb6051b85cf046a7b3410deef10910ac",
adIntervals = 30,
style = new Style()
{
// left = 0,
// top = 0
}
});
bannerAd.OnLoad(() =>
{
bannerAd.Show(
(msg) => { Debug.Log("QG.bannerAd.Show success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.bannerAd.Show fail = " + msg.errMsg); }
);
});
bannerAd.OnError((QGBaseResponse msg) =>
{
Debug.Log("QG.bannerAd.OnError success = " + JsonUtility.ToJson(msg));
});
创建插屏广告
C# 示例如下,对应javascript平台API文档。
interstitialAd = QG.CreateInterstitialAd(new QGCommonAdParam()
{
posId = "a11c3c4d0637485abf41024e6191c9cc"
});
interstitialAd.OnLoad(() =>
{
interstitialAd.Show(
(msg) => { Debug.Log("QG.interstitialAd.Show success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.interstitialAd.Show fail = " + msg.errMsg); }
);
});
interstitialAd.OnError((QGBaseResponse msg) =>
{
Debug.Log("QG.interstitialAd.OnError success = " + JsonUtility.ToJson(msg));
});
interstitialAd.Hide(
(msg) => { Debug.Log("QG.interstitialAd.Hide success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.interstitialAd.Hide fail = " + msg.errMsg); }
);
创建激励视频广告
C# 示例如下,对应javascript平台API文档。
if (rewardedVideoAd == null)
{
rewardedVideoAd = QG.CreateRewardedVideoAd(new QGCommonAdParam()
{
posId = "cf6d0f43846341828291afe78b816551"
});
}
else
{
rewardedVideoAd.Load(
(msg) => { Debug.Log("QG.rewardedVideoAd.Load success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.rewardedVideoAd.Load fail = " + msg.errMsg); }
);
}
rewardedVideoAd.OnLoad(() =>
{
rewardedVideoAd.Show(
(msg) => { Debug.Log("QG.rewardedVideoAd.Show success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.rewardedVideoAd.Show fail = " + msg.errMsg); }
);
});
rewardedVideoAd.OnError((QGBaseResponse msg) =>
{
Debug.Log("QG.rewardedVideoAd.OnError success = " + JsonUtility.ToJson(msg));
});
rewardedVideoAd.OnClose((QGRewardedVideoResponse msg) =>
{
if (msg.isEnded)
{
Debug.Log("QG.rewardedVideoAd.OnClose success = " + " 播放成功");
}
});
rewardedVideoAd.Hide(
(msg) => { Debug.Log("QG.rewardedVideoAd.Hide success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.rewardedVideoAd.Hide fail = " + msg.errMsg); }
);
创建原生广告
C# 示例如下,对应javascript平台API文档。
if (nativeAd == null)
{
nativeAd = QG.CreateNativeAd(new QGCommonAdParam()
{
posId = "0f36aa6ce7fd44a3a7c391c2a524308c"
});
}
else
{
nativeAd.Load(
(msg) => { Debug.Log("QG.nativeAd.Load success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.nativeAd.Load fail = " + msg.errMsg); }
);
}
nativeAd.OnLoad((QGNativeResponse rec) =>
{
Debug.Log("QG.nativeAd.OnLoad success = " + JsonUtility.ToJson(rec));
if (rec != null && rec.adList != null)
{
nativeCurrentAd = rec.adList[0];
}
});
nativeAd.OnError((QGBaseResponse msg) =>
{
Debug.Log("QG.nativeAd.OnError success = " + JsonUtility.ToJson(msg));
});
if (nativeCurrentAd != null)
{
nativeAd.ReportAdShow(new QGNativeReportParam()
{
adId = nativeCurrentAd.adId
});
nativeAd.ReportAdClick(new QGNativeReportParam()
{
adId = nativeCurrentAd.adId
});
}
创建模板广告
C# 示例如下,对应javascript平台API文档。
customAd = QG.CreateCustomAd(new QGCreateCustomAdParam()
{
posId = "25a289ebd99b4ccd99c49524931f97a0"
});
customAd.OnLoad(() =>
{
Debug.Log("QG.customAd.OnLoad success = ");
customAd.Show(
(msg) => { Debug.Log("QG.customAd.Show success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.customAd.Show fail = " + msg.errMsg); }
);
});
customAd.OnError((QGBaseResponse msg) =>
{
Debug.Log("QG.customAd.OnError success = " + JsonUtility.ToJson(msg));
});
customAd.OnHide(() =>
{
Debug.Log("QG.customAd.OnHide success ");
});
customAd.Hide(
(msg) => { Debug.Log("QG.customAd.Hide success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.customAd.Hide fail = " + msg.errMsg); }
);
创建横幅广告
C# 示例如下,对应javascript平台API文档。
boxBannerAd = QG.CreateBoxBannerAd(new QGCommonAdParam()
{
posId = "xxxxxx1"
});
boxBannerAd.OnLoad(() =>
{
Debug.Log("QG.boxBannerAd.OnLoad success = ");
boxBannerAd.Show(
(msg) => { Debug.Log("QG.boxBannerAd.Show success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.boxBannerAd.Show fail = " + msg.errMsg); }
);
});
boxBannerAd.OnError((QGBaseResponse msg) =>
{
Debug.Log("QG.boxBannerAd.OnError success = " + JsonUtility.ToJson(msg));
});
boxBannerAd.OnClose(() =>
{
Debug.Log("QG.boxBannerAd.OnClose success ");
});
boxBannerAd.Hide(
(msg) => { Debug.Log("QG.boxBannerAd.Hide success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.boxBannerAd.Hide fail = " + msg.errMsg); }
);
创建九宫格广告
C# 示例如下,对应javascript平台API文档。
boxPortalAd = QG.CreateBoxPortalAd(new QGCreateBoxPortalAdParam()
{
posId = "xxxxxx2",
image = "",
marginTop = 200
});
boxPortalAd.OnLoad(() =>
{
boxPortalAd.Show(
(msg) => { Debug.Log("QG.boxPortalAd.Show success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.boxPortalAd.Show fail = " + msg.errMsg); }
);
});
boxPortalAd.OnError((QGBaseResponse msg) =>
{
Debug.Log("QG.boxPortalAd.OnError success = " + JsonUtility.ToJson(msg));
});
boxPortalAd.OnShow(() =>
{
Debug.Log("QG.boxPortalAd.OnShow success = ");
});
boxPortalAd.Hide(
(msg) => { Debug.Log("QG.boxPortalAd.Hide success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.boxPortalAd.Hide fail = " + msg.errMsg); }
);
文件操作
注意uri格式,请参考 文件系统。
判断文件是否存在
C# 示例如下,对应javascript平台API文档。
QG.AccessFile("internal://cache/path/to/file")
读取文件
C# 示例如下,对应javascript平台API文档。
QGFileParam param = new QGFileParam()
{
uri = "internal://files/hehe.txt",
encoding = "utf8"
};
QG.ReadFile(
param,
(msg) => { Debug.Log("QG.ReadFile success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.ReadFile fail = " + JsonUtility.ToJson(msg)); }
);
同步读取文件
C# 示例如下,对应javascript平台API文档。
QGFileParam param = new QGFileParam()
{
uri = "internal://files/hehe.txt",
encoding = "utf8"
};
QGFileInfo info = QG.ReadFileSync(param);
string fileStr = info.textStr;
byte[] fileData = info.textData;
Debug.LogError(fileStr);
Debug.LogError(fileData);
写入文件
C# 示例如下,对应javascript平台API文档。
QGFileParam param = new QGFileParam()
{
uri = "internal://files/hehe.txt",
encoding = "utf8",
textStr = "你是谁啊 "
};
QG.WriteFile(
param,
(msg) => { Debug.Log("QG.WriteFile success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.WriteFile fail = " + JsonUtility.ToJson(msg)); }
);
同步写入文件
C# 示例如下,对应javascript平台API文档。
QGFileParam param = new QGFileParam()
{
uri = "internal://files/hehe.txt",
encoding = "utf8",
textStr = "你是谁啊 "
};
QG.WriteFileSync(
param
);
界面交互
软键盘
- ShowKeyboard C# 示例如下,对应javascript平台API文档。
KeyboardParam param = new KeyboardParam()
{
defaultValue = "vivo小游戏",
maxLength = 500,
multiple = true,
confirmHold = false,
confirmType = "done"
};
QG.ShowKeyboard(
param,
(msg) => { Debug.Log("QG.ShowKeyboard success = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.ShowKeyboard cancel = " + JsonUtility.ToJson(msg)); },
(msg) => { Debug.Log("QG.ShowKeyboard complete = " + JsonUtility.ToJson(msg)); }
);
- OnKeyboardInput
QG.OnKeyboardInput(
(msg) => { Debug.Log("QG.OnKeyboardInput input = " + JsonUtility.ToJson(msg)); },
);
- OnKeyboardConfirm
QG.OnKeyboardConfirm(
(msg) => { Debug.Log("QG.OnKeyboardConfirm input = " + JsonUtility.ToJson(msg)); },
);
- OnKeyboardComplete
QG.OnKeyboardConfirm(
(msg) => { Debug.Log("QG.OnKeyboardComplete input = " + JsonUtility.ToJson(msg)); },
);
- OffKeyboardInput
对应OnKeyboardInput,配合使用
string inputId = QG.OnKeyboardInput(
(msg) => { Debug.Log("QG.OnKeyboardInput input = " + JsonUtility.ToJson(msg)); },
);
QG.OffKeyboardInput(
inputId,
(msg) => { Debug.Log("QG.OffKeyboardInput input = " + JsonUtility.ToJson(msg)); },
);
- OffKeyboardConfirm
对应OnKeyboardConfirm,配合使用
string confirmId = QG.OnKeyboardConfirm(
(msg) => { Debug.Log("QG.OnKeyboardComplete input = " + JsonUtility.ToJson(msg)); },
);
QG.OffKeyboardConfirm(
confirmId,
(msg) => { Debug.Log("QG.OffKeyboardConfirm input = " + JsonUtility.ToJson(msg)); },
);
- OffKeyboardComplete
对应OnKeyboardComplete,配合使用
string completeId = QG.OnKeyboardComplete(
(msg) => { Debug.Log("QG.OnKeyboardComplete input = " + JsonUtility.ToJson(msg)); },
);
QG.OffKeyboardComplete(
completeId,
(msg) => { Debug.Log("QG.OffKeyboardComplete input = " + JsonUtility.ToJson(msg)); },
);
- HideKeyboard
QG.HideKeyboard();
覆盖unity的PlayerPrefs
SetInt/String/Float(key, value)
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
QG.StorageSetInt/String/FloatSync(key, value);
} else {
UnityEngine.PlayerPrefs.SetInt/String/Float(key, value);
}
GetInt/String/Float(key, defaultValue)
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
return QG.StorageGetIntSync(key, defaultValue);
}
else
{
return UnityEngine.PlayerPrefs.GetInt/String/Float(key, defaultValue);
}
DeleteAll()
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
QG.StorageDeleteAllSync();
}
else
{
UnityEngine.PlayerPrefs.DeleteAll();
}
DeleteKey(key);
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
QG.StorageDeleteKeySync(key);
}
else
{
UnityEngine.PlayerPrefs.DeleteKey(key);
}
HasKey(key);
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
return QG.StorageHasKeySync(key);
}
else
{
return UnityEngine.PlayerPrefs.HasKey(key);
}
Save();
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
//do nothing
}
else
{
UnityEngine.PlayerPrefs.Save();
}
自定义拓展
请参考Unity提供的C#调用JS方法的 代码示例
主要在Plugins中的JS插件(qg.minigame.jslib)编写JS代码及在QGMiniGameManager封装调用入口