赞
踩
此处添加需要开启的权限 然后设置隐私授权弹窗
微信开发者工具下载地址与更新日志 | 微信开放文档 (qq.com)
minigame-unity-webgl-transform: Unity WebGL 微信小游戏适配方案 (gitee.com)
此处包含微信官方示例 可以下载看一下
具体可看官方API文档
游戏引擎 / Unity WebGL微信小游戏适配 (qq.com)
-
- /// <summary>
- /// 初始化SDK
- /// </summary>
- private void InitSDK()
- {
- WX.InitSDK((code) =>
- {
- Debug.Log("微信SDK初始化成功");
-
- LoginOption loginOption = new LoginOption()
- {
- success = LoginSuccess,
- fail = (result) => { Debug.Log($"登录失败 code={result.errno} msg={result.errMsg}"); }
- };
-
- Debug.Log("开始登录");
- WX.Login(loginOption);
- });
- }
-
-
- /// <summary>
- /// 登陆成功回调
- /// </summary>
- /// <param name="result"></param>
- private void LoginSuccess(LoginSuccessCallbackResult result)
- {
- Debug.Log($"登录成功 code={result.code} msg={result.errMsg}");
-
- code = result.code;
- GetSettingOption getSettingOption = new GetSettingOption()
- {
- success = GetSettingSuccess,
- fail = (result) => { Debug.Log($"获取设置信息失败 {result.errMsg}"); }
- };
- WX.GetSetting(getSettingOption);
- }
-
-
-
- /// <summary>
- /// 获取玩家配置成功
- /// </summary>
- /// <param name="result"></param>
- private void GetSettingSuccess(GetSettingSuccessCallbackResult result)
- {
- if (!result.authSetting.ContainsKey("scope.userInfo") || !result.authSetting["scope.userInfo"])
- {
- Debug.Log("生成按钮开始请求获取用户信息");
-
- //此处设置虚拟按钮大小
- WXUserInfoButton wxUserInfoButton = WX.CreateUserInfoButton(0, 0, Screen.width, Screen.height, "zh_CN", false);
- wxUserInfoButton.Show();
- wxUserInfoButton.OnTap((data) =>
- {
- if (data.errCode == 0)
- {
- //获取成功
- Debug.Log($"用户同意授权 用户名:{data.userInfo.nickName} 用户头像{data.userInfo.avatarUrl}");
- Xcxlogin();
- wxUserInfoButton.Hide();
- }
- else
- {
- Debug.Log("用户拒绝授权");
- wxUserInfoButton.Hide();
- }
- });
- }
- else
- {
- Debug.Log("已获取过权限");
- GetUserInfoOption getUserInfoOption = new GetUserInfoOption()
- {
- lang = "zh_CN",
- withCredentials = false,
- success = GetUserInfoSuccess,
- fail = (result) => { Debug.Log($"获取玩家信息失败 {result.errMsg}"); }
- };
- WX.GetUserInfo(getUserInfoOption);
- }
- }
-
- /// <summary>
- /// 获取玩家信息成功回调
- /// </summary>
- /// <param name="data"></param>
- private void GetUserInfoSuccess(GetUserInfoSuccessCallbackResult data)
- {
- Debug.Log($"用户名:{data.userInfo.nickName} 用户头像{data.userInfo.avatarUrl}");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。