using Newtonsoft.Json; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using QRCodeShareMain; using static Cinemachine.CinemachineBlendDefinition; using TMPro; public class ToFriend : MonoBehaviour { public Image Tofrienf; private Texture2D currentQRCodeGenerate = null; public UserInfomation14 userInfomation14; public Button SaveButton; // Start is called before the first frame update async void Start() { Dictionary head14 = new Dictionary { { "Authorization", Global.global.serverResponse.data.token } }; // 异步查询玩家信息 string response14 = await web.SendRequest(web.URL + "/snail/user/queryUserInfo", "POST", "{}", head14); Debug.Log("1.4查询玩家信息" + response14); userInfomation14 = JsonConvert.DeserializeObject(response14); OnClickGenerateQRCode(userInfomation14.data.inviteCodeMy); SaveButton.onClick.AddListener((() => { StartCoroutine(TakeScreenshotAndSave()); })); } private IEnumerator TakeScreenshotAndSave() { yield return new WaitForEndOfFrame(); Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0); ss.Apply(); // Save the screenshot to Gallery/Photos NativeGallery.Permission permission = NativeGallery.SaveImageToGallery(ss, "GalleryTest", "Image.png", (success, path) => Debug.Log("Media save result: " + path + " " + path)); Debug.Log("Permission result: " + permission); // To avoid memory leaks Destroy(ss); } private void OnClickGenerateQRCode(string content) { currentQRCodeGenerate = HelloWorldQRCode(content); if (currentQRCodeGenerate != null) { ShowImage(Tofrienf, currentQRCodeGenerate); // Enable the download image button is the texture is not null } } private void ShowImage(Image showImage, Texture2D image) { showImage.sprite = ImageProcessing.ConvertTexture2DToSprite(image); float imageSize = Mathf.Max(showImage.GetComponent().sizeDelta.x, showImage.GetComponent().sizeDelta.y); showImage.GetComponent().sizeDelta = image.width <= image.height ? new Vector2(imageSize / image.height * image.width, imageSize) : new Vector2(imageSize, imageSize * image.height / image.width); } // Update is called once per frame void Update() { } private Texture2D HelloWorldQRCode(string content) { QRImageProperties properties = new QRImageProperties(500, 500, 50); Texture2D QRCodeImage = QRCodeShare.CreateQRCodeImage(content, properties); return QRCodeImage; } }