UNITY 微信小游戏SDK使用

admin1年前微信小游戏105


1.WX.InitSDK

初始化SDK 成功返回200

        WX.InitSDK((code) => {
            Debug.Log("初始化结束");
            Debug.Log(code);

        });



2.WX.GetSetting();

获取设置信息

GetSettingOption setTingOp = new GetSettingOption();setTingOp.success = (e) =>{
	//if (!e.authSetting.ContainsKey("scope.userInfo") || !e.authSetting["scope.userInfo"])
	//scope.userInfo
	//scope.userInfo};WX.GetSetting(setTingOp);


3. WX.Authorize();

发起授权操作 获取用户信息前需要授权

AuthorizeOption authorizeOption = new AuthorizeOption();authorizeOption.scope = "scope.userInfo"; //{scope: "scope.userInfo"}authorizeOption.success = (e) =>{
    Debug.Log("success");};authorizeOption.fail = (e) =>{
     Debug.Log("fail");};authorizeOption.complete = (e) =>{
     Debug.Log("complete");};
 WX.Authorize(authorizeOption);



4. WX.Login();

登陆操作

 LoginOption loginOption = new LoginOption();
            loginOption.complete = (e) =>
            {

                

                PlayerPrefs.SetString("login", "yes");
                Debug.Log("完成");
                Debug.Log(e);
            };
            loginOption.fail = (err) =>
            {
                Debug.Log("登陆失败");
            };
            WX.Login(loginOption);


5. WX.GetUserInfo();

获取用户信息 使用这个API前需要授权通过

GetUserInfoOption callBack = new GetUserInfoOption();
                        callBack.withCredentials = true;
                        callBack.lang = "zh_CN";
                        //Debug.Log(e.code);
                        callBack.success = (d) =>
                        {
                            /*Debug.Log(e.encryptedData);
                            Debug.Log(e.iv);
                            Debug.Log(e.rawData);
                            Debug.Log(e.signature);*/
                            string url = string.Format("https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code", "自己的appid", "自己的secret", e.code);
                            StartCoroutine(GetRequest(url));
                            RoleManager.roleData.roleName = d.userInfo.nickName;
                            RoleManager.roleData.headUrl = d.userInfo.avatarUrl;
                            Debug.Log(d.userInfo.avatarUrl);
                        };
                        callBack.complete = (e) =>
                        {
                            Debug.Log("完成");
                        };
                        callBack.fail = (e) =>
                        {
                            Debug.Log("失败");
                            Debug.Log(e.errMsg);
                        };



WX.GetUserInfo(callBack);


private IEnumerator GetRequest(string url)    {        using (UnityWebRequest webRequest = UnityWebRequest.Get(url))        {            yield return webRequest.SendWebRequest();            if (!string.IsNullOrEmpty(webRequest.error))            {                Debug.Log("error:" + webRequest.error);            }            else            {                UserData data = JsonUtility.FromJson<UserData>(webRequest.downloadHandler.text);                if (data != null)                {                                                        }                Debug.Log(webRequest.downloadHandler.text);            }        }    }

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。