赞
踩
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class Manager_fan : MonoBehaviour
- {
- public static Manager_fan instance;
- public Button button_target;
- public Button button_close;
- public Button button_capture;
- public Button button_clear_txt;
- public Button button_clear_images;
- public Button button_sub;
- public Transform target;
- [Header("文本")]
- public GameObject prefabs;
- [Header("图片item")]
- public GameObject prefabs_image;
- public Transform txt_parent;
- public Transform capture_parent;
-
- public GameObject[] gameObjects;
- private Dictionary<string, GameObject> dic_obj = new Dictionary<string, GameObject>();
-
- private bool stop;
- //content\base64
- private Texture2D sprite,sprite1;
- private List<text_demo> text_Demos = new List<text_demo>();
- private bool stop1;
- public bool Stop1 { get => stop1; set => stop1 = value; }
-
- public class text_demo
- {
- public Vector3 hit_v3;
- public GameObject prefabs_item;
- }
- private void Awake()
- {
- instance = this;
- for (int i = 0; i < gameObjects.Length; i++)
- {
- dic_obj.Add(gameObjects[i].name, gameObjects[i]);
- }
- sprite = Resources.Load<Texture2D>("target");
- sprite1 = Resources.Load<Texture2D>("close");
- }
- void Addlister_arr()
- {
- button_target.onClick.AddListener(delegate ()
- {
- if (stop == false)
- {
- Cursor.SetCursor(sprite, new Vector2(338, 338), CursorMode.Auto);
- Stop1 = false;
- }
- else
- {
- Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
- }
- stop = !stop;
- });
-
- button_close.onClick.AddListener(delegate ()
- {
- if (Stop1 == false)
- {
- Cursor.SetCursor(sprite1, new Vector2(16, 16), CursorMode.Auto);
- stop = false;
- }
- else
- {
- Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
- }
- Stop1 = !Stop1;
-
- });
- button_capture.onClick.AddListener(delegate ()
- {
- dic_obj["相机区域"].SetActive(true);
-
- });
- }
-
- // Start is called before the first frame update
- void Start()
- {
-
- Addlister_arr();
- }
-
- // Update is called once per frame
- void Update()
- {
- Model_ray();
- }
- void Model_ray()
- {
- if (stop == true)
- {
- if (Input.GetMouseButtonDown(0))
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, Mathf.Infinity))
- {
- if (hit.transform == target)
- {
- GameObject gameObject = Instantiate(prefabs, txt_parent);
- text_demo text_Demo = new text_demo();
- text_Demo.hit_v3 = hit.point;
- text_Demo.prefabs_item = gameObject;
-
- text_Demos.Add(text_Demo);
- }
- }
- }
- }
- }
-
- private void LateUpdate()
- {
- if (text_Demos.Count != 0)
- {
- for (int i = 0; i < text_Demos.Count; i++)
- {
- text_Demos[i].prefabs_item.transform.position = Camera.main.WorldToScreenPoint(text_Demos[i].hit_v3);
- }
- }
- }
- /// <summary>
- /// 删除UI
- /// </summary>
- /// <param name="obj"></param>
- public void Delete_from_obj(GameObject obj)
- {
- for (int i = 0; i < text_Demos.Count; i++)
- {
- if(text_Demos[i].prefabs_item== obj)
- {
- text_Demos.Remove(text_Demos[i]);
- Destroy(obj);
- }
- }
- }
- }
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.UI;
- using DG.Tweening;
- public delegate void CallBack();
- public class ScreenShotTest : MonoBehaviour
- {
- public Camera renderCamera;
- public GameObject ui_bg;
- public Canvas scaler;
-
- //************
- [Header("根据当前ui大小截屏")]
- public Transform camera_picture;
- //画布界限
- private float X_left, Y_up, X_right, Y_down;
-
- public Image image_show;
- [Header("相机but")]
- public Button but_capture;
-
-
- private void Awake()
- {
-
- but_capture.onClick.AddListener(delegate ()
- {
- ui_bg.SetActive(false);
- Reset_image_rect();
- StartCoroutine(ScreenCapture(new Rect(X_left, Y_down, X_right - X_left, Y_up - Y_down), "", Callback));
- });
-
- }
- public void Base64ToImg(Image imgComponent, string base64)
- {
- byte[] bytes = Convert.FromBase64String(base64);
- Texture2D tex2D = new Texture2D(45, 55);
- tex2D.LoadImage(bytes);
- Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));
- imgComponent.sprite = s;
- Resources.UnloadUnusedAssets();
- }
- //************
- //获取并更新截屏区域(防止分辨率变化的影响scaler为当前 Canvas的scale)下面并无涉及可用new Rect(X_left, Y_down, X_right-X_left, Y_up-Y_down)代替固定分辨率
- private void Reset_image_rect()
- {
- print("canvas" + scaler.scaleFactor);
-
- X_left = camera_picture.transform.position.x - camera_picture.GetComponent<RectTransform>().rect.width * scaler.scaleFactor / 2;
- X_right = camera_picture.transform.position.x + camera_picture.GetComponent<RectTransform>().rect.width * scaler.scaleFactor/ 2;
-
-
-
- Y_up = camera_picture.transform.position.y + camera_picture.GetComponent<RectTransform>().rect.height * scaler.scaleFactor/ 2;
- Y_down = camera_picture.transform.position.y - camera_picture.GetComponent<RectTransform>().rect.height * scaler.scaleFactor/ 2;
- }
- public void Callback()
- {
- ui_bg.SetActive(true);
- }
- /// <summary>
- /// 截取游戏屏幕内的像素
- /// </summary>
- /// <param name="rect">截取区域:屏幕左下角为0点</param>
- /// <param name="fileName">文件名</param>
- /// <param name="callBack">截图完成回调</param>
- /// <returns></returns>
- public IEnumerator ScreenCapture(Rect rect, string fileName, CallBack callBack = null)
- {
- yield return new WaitForEndOfFrame();//等到帧结束,不然会报错
- Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象
- tex.ReadPixels(rect, 0, 0);//读取像素,屏幕左下角为0点
- tex.Apply();//保存像素信息
-
- byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个png图片
-
- Sprite sprite = Sprite.Create(tex, new Rect(0,0, rect.width, rect.height), new Vector2 (0.5f, 0.5f));//转化为精灵 Vector2 (0.5f, 0.5f)为锚点
-
- StartCoroutine(Show_image(sprite));
- string base64String = Convert.ToBase64String(bytes);
-
- Base64ToImg(image_show, base64String);
- callBack?.Invoke();
- #if UNITY_EDITOR
- UnityEditor.AssetDatabase.Refresh();//刷新Unity的资产目录
- #endif
- }
- IEnumerator Show_image(Sprite sprite)
- {
- image_show.gameObject.SetActive(true);
- image_show.sprite = sprite;
- image_show.preserveAspect = true;
- image_show.transform.DOScale(Vector3.one, 0.2f);
- image_show.transform.position =transform.position;
- yield return new WaitForSeconds(0.5f);
- image_show.transform.DOScale(Vector3.zero, 0.5f);
- image_show.transform.DOMove(Manager_fan.instance.capture_parent.position, 0.5f);
- yield return new WaitForSeconds(0.5f);
- GameObject gameObject = Instantiate(Manager_fan.instance.prefabs_image, Manager_fan.instance.capture_parent);
-
- gameObject.GetComponent<Image>().sprite = sprite;
- image_show.gameObject.SetActive(false);
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- public class Text_input : MonoBehaviour, IPointerDownHandler
- {
-
- public void OnPointerDown(PointerEventData eventData)
- {
- if (eventData.button == PointerEventData.InputButton.Left||
- eventData.button == PointerEventData.InputButton.Right||
- eventData.button == PointerEventData.InputButton.Middle)
- {
-
- if (Manager_fan.instance.Stop1 == true)
- {
- Manager_fan.instance.Delete_from_obj(this.gameObject);
- }
- }
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。