当前位置:   article > 正文

unity 截屏并转化为base64并显示截图_unity byte转bs64

unity byte转bs64

 

 

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class Manager_fan : MonoBehaviour
  6. {
  7. public static Manager_fan instance;
  8. public Button button_target;
  9. public Button button_close;
  10. public Button button_capture;
  11. public Button button_clear_txt;
  12. public Button button_clear_images;
  13. public Button button_sub;
  14. public Transform target;
  15. [Header("文本")]
  16. public GameObject prefabs;
  17. [Header("图片item")]
  18. public GameObject prefabs_image;
  19. public Transform txt_parent;
  20. public Transform capture_parent;
  21. public GameObject[] gameObjects;
  22. private Dictionary<string, GameObject> dic_obj = new Dictionary<string, GameObject>();
  23. private bool stop;
  24. //content\base64
  25. private Texture2D sprite,sprite1;
  26. private List<text_demo> text_Demos = new List<text_demo>();
  27. private bool stop1;
  28. public bool Stop1 { get => stop1; set => stop1 = value; }
  29. public class text_demo
  30. {
  31. public Vector3 hit_v3;
  32. public GameObject prefabs_item;
  33. }
  34. private void Awake()
  35. {
  36. instance = this;
  37. for (int i = 0; i < gameObjects.Length; i++)
  38. {
  39. dic_obj.Add(gameObjects[i].name, gameObjects[i]);
  40. }
  41. sprite = Resources.Load<Texture2D>("target");
  42. sprite1 = Resources.Load<Texture2D>("close");
  43. }
  44. void Addlister_arr()
  45. {
  46. button_target.onClick.AddListener(delegate ()
  47. {
  48. if (stop == false)
  49. {
  50. Cursor.SetCursor(sprite, new Vector2(338, 338), CursorMode.Auto);
  51. Stop1 = false;
  52. }
  53. else
  54. {
  55. Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
  56. }
  57. stop = !stop;
  58. });
  59. button_close.onClick.AddListener(delegate ()
  60. {
  61. if (Stop1 == false)
  62. {
  63. Cursor.SetCursor(sprite1, new Vector2(16, 16), CursorMode.Auto);
  64. stop = false;
  65. }
  66. else
  67. {
  68. Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
  69. }
  70. Stop1 = !Stop1;
  71. });
  72. button_capture.onClick.AddListener(delegate ()
  73. {
  74. dic_obj["相机区域"].SetActive(true);
  75. });
  76. }
  77. // Start is called before the first frame update
  78. void Start()
  79. {
  80. Addlister_arr();
  81. }
  82. // Update is called once per frame
  83. void Update()
  84. {
  85. Model_ray();
  86. }
  87. void Model_ray()
  88. {
  89. if (stop == true)
  90. {
  91. if (Input.GetMouseButtonDown(0))
  92. {
  93. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  94. RaycastHit hit;
  95. if (Physics.Raycast(ray, out hit, Mathf.Infinity))
  96. {
  97. if (hit.transform == target)
  98. {
  99. GameObject gameObject = Instantiate(prefabs, txt_parent);
  100. text_demo text_Demo = new text_demo();
  101. text_Demo.hit_v3 = hit.point;
  102. text_Demo.prefabs_item = gameObject;
  103. text_Demos.Add(text_Demo);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. private void LateUpdate()
  110. {
  111. if (text_Demos.Count != 0)
  112. {
  113. for (int i = 0; i < text_Demos.Count; i++)
  114. {
  115. text_Demos[i].prefabs_item.transform.position = Camera.main.WorldToScreenPoint(text_Demos[i].hit_v3);
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 删除UI
  121. /// </summary>
  122. /// <param name="obj"></param>
  123. public void Delete_from_obj(GameObject obj)
  124. {
  125. for (int i = 0; i < text_Demos.Count; i++)
  126. {
  127. if(text_Demos[i].prefabs_item== obj)
  128. {
  129. text_Demos.Remove(text_Demos[i]);
  130. Destroy(obj);
  131. }
  132. }
  133. }
  134. }
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using DG.Tweening;
  8. public delegate void CallBack();
  9. public class ScreenShotTest : MonoBehaviour
  10. {
  11. public Camera renderCamera;
  12. public GameObject ui_bg;
  13. public Canvas scaler;
  14. //************
  15. [Header("根据当前ui大小截屏")]
  16. public Transform camera_picture;
  17. //画布界限
  18. private float X_left, Y_up, X_right, Y_down;
  19. public Image image_show;
  20. [Header("相机but")]
  21. public Button but_capture;
  22. private void Awake()
  23. {
  24. but_capture.onClick.AddListener(delegate ()
  25. {
  26. ui_bg.SetActive(false);
  27. Reset_image_rect();
  28. StartCoroutine(ScreenCapture(new Rect(X_left, Y_down, X_right - X_left, Y_up - Y_down), "", Callback));
  29. });
  30. }
  31. public void Base64ToImg(Image imgComponent, string base64)
  32. {
  33. byte[] bytes = Convert.FromBase64String(base64);
  34. Texture2D tex2D = new Texture2D(45, 55);
  35. tex2D.LoadImage(bytes);
  36. Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));
  37. imgComponent.sprite = s;
  38. Resources.UnloadUnusedAssets();
  39. }
  40. //************
  41. //获取并更新截屏区域(防止分辨率变化的影响scaler为当前 Canvas的scale)下面并无涉及可用new Rect(X_left, Y_down, X_right-X_left, Y_up-Y_down)代替固定分辨率
  42. private void Reset_image_rect()
  43. {
  44. print("canvas" + scaler.scaleFactor);
  45. X_left = camera_picture.transform.position.x - camera_picture.GetComponent<RectTransform>().rect.width * scaler.scaleFactor / 2;
  46. X_right = camera_picture.transform.position.x + camera_picture.GetComponent<RectTransform>().rect.width * scaler.scaleFactor/ 2;
  47. Y_up = camera_picture.transform.position.y + camera_picture.GetComponent<RectTransform>().rect.height * scaler.scaleFactor/ 2;
  48. Y_down = camera_picture.transform.position.y - camera_picture.GetComponent<RectTransform>().rect.height * scaler.scaleFactor/ 2;
  49. }
  50. public void Callback()
  51. {
  52. ui_bg.SetActive(true);
  53. }
  54. /// <summary>
  55. /// 截取游戏屏幕内的像素
  56. /// </summary>
  57. /// <param name="rect">截取区域:屏幕左下角为0点</param>
  58. /// <param name="fileName">文件名</param>
  59. /// <param name="callBack">截图完成回调</param>
  60. /// <returns></returns>
  61. public IEnumerator ScreenCapture(Rect rect, string fileName, CallBack callBack = null)
  62. {
  63. yield return new WaitForEndOfFrame();//等到帧结束,不然会报错
  64. Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
  65. tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
  66. tex.Apply();//保存像素信息
  67. byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个png图片
  68. Sprite sprite = Sprite.Create(tex, new Rect(0,0, rect.width, rect.height), new Vector2 (0.5f, 0.5f));//转化为精灵 Vector2 (0.5f, 0.5f)为锚点
  69. StartCoroutine(Show_image(sprite));
  70. string base64String = Convert.ToBase64String(bytes);
  71. Base64ToImg(image_show, base64String);
  72. callBack?.Invoke();
  73. #if UNITY_EDITOR
  74. UnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
  75. #endif
  76. }
  77. IEnumerator Show_image(Sprite sprite)
  78. {
  79. image_show.gameObject.SetActive(true);
  80. image_show.sprite = sprite;
  81. image_show.preserveAspect = true;
  82. image_show.transform.DOScale(Vector3.one, 0.2f);
  83. image_show.transform.position =transform.position;
  84. yield return new WaitForSeconds(0.5f);
  85. image_show.transform.DOScale(Vector3.zero, 0.5f);
  86. image_show.transform.DOMove(Manager_fan.instance.capture_parent.position, 0.5f);
  87. yield return new WaitForSeconds(0.5f);
  88. GameObject gameObject = Instantiate(Manager_fan.instance.prefabs_image, Manager_fan.instance.capture_parent);
  89. gameObject.GetComponent<Image>().sprite = sprite;
  90. image_show.gameObject.SetActive(false);
  91. }
  92. }
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. public class Text_input : MonoBehaviour, IPointerDownHandler
  7. {
  8. public void OnPointerDown(PointerEventData eventData)
  9. {
  10. if (eventData.button == PointerEventData.InputButton.Left||
  11. eventData.button == PointerEventData.InputButton.Right||
  12. eventData.button == PointerEventData.InputButton.Middle)
  13. {
  14. if (Manager_fan.instance.Stop1 == true)
  15. {
  16. Manager_fan.instance.Delete_from_obj(this.gameObject);
  17. }
  18. }
  19. }
  20. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/118505?site
推荐阅读
相关标签
  

闽ICP备14008679号