Unity微信小游戏——登录 获取用户头像名称

1.微信公众平台开启权限

此处添加需要开启的权限 然后设置隐私授权弹窗

2.下载微信开发者工具与unity转小程序的插件

微信开发者工具下载地址与更新日志 | 微信开放文档 (qq.com)

minigame-unity-webgl-transform: Unity WebGL 微信小游戏适配方案 (gitee.com)

此处包含微信官方示例 可以下载看一下

3.登录 获取玩家信息

具体可看官方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}");   
    }
Unity开发微信小游戏时,获取设备分辨率通常通过Unity的InputManager或GraphicsSettings API来完成。以下是一个简单的步骤: 1. 首先,在Unity编辑器中,打开`C#`脚本并添加以下代码: ```csharp using UnityEngine; using System.Runtime.InteropServices; public class DeviceResolution : MonoBehaviour { [DllImport("Unity-iPhone-iOS9")] private static extern void UIApplicationMain(int argc, string[] argv, string principalClassString, string delegateClassName); public void GetScreenResolution() { int width = Screen.width; int height = Screen.height; Debug.Log($"屏幕宽度: {width}, 屏幕高度: {height}"); } // 如果是在微信小程序环境中,这里需要判断 void Start() { if (Application.platform == RuntimePlatform.IPhonePlayer) { GetScreenResolution(); } else if (IsWechatXiaoGame()) // 判断是否是微信小游戏环境 { float deviceScale = ApplicationInfo.currentDeviceAspectRatio; // 微信小游戏有自己的deviceScale float scaledWidth = deviceScale * Screen.width; float scaledHeight = deviceScale * Screen.height; Debug.Log($"实际分辨率为 ({scaledWidth}, {scaledHeight})"); } } // 简单的微信小游戏环境检测函数,可能需要根据实际情况调整 private bool IsWechatXiaoGame() { return Application.tenantId != ""; } } ``` 注意,对于微信小游戏,`currentDeviceAspectRatio` 可能会返回一个大于1的值,这是因为微信小游戏会应用自适应缩放(例如2倍屏)。你需要根据实际需求调整分辨率获取
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值