123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using SUIFW;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class AdvancedDrawingPad : MonoBehaviour
- {
- private GameObject clone;
- private LineRenderer line;
- private int i;
- private int index = 0;
- public static Material mat;
- public static bool canUse;
- public GameObject obs;
- public GameObject lineparent;
- private Vector3 startpos;
- public RectTransform canvas;
- public GameObject listColor;
- public Sprite[] spriteList;
- public Image img;
- int colorIndex = 1;
- public static AdvancedDrawingPad Instance;
- void Start()
- {
- Instance = this;
- }
- void Awake()
- {
-
- }
- public static bool isqianming;
- private void OnEnable()
- {
- isqianming=false;
-
-
- Screen.orientation = ScreenOrientation.LandscapeLeft;
- Screen.autorotateToLandscapeLeft = false;
- Screen.autorotateToLandscapeRight = false;
- Screen.autorotateToPortrait = false;
- Screen.autorotateToPortraitUpsideDown = false;
- canUse = true;
- listColor.SetActive(false);
- colorIndex = 1;
- }
- public void showAndCloseColor()
- {
- listColor.SetActive(!listColor.activeSelf);
- canUse = !listColor.activeSelf;
- }
- public static Action<Texture2D> callback;
- public void closejietu()
- {
- if(isqianming)
- {
- Rect rect = new Rect(0, 0, Screen.width, Screen.height);
- callback.Invoke(ScreenShot(Camera.main,rect));
- }
- }
-
-
-
-
- public Texture2D ScreenShot(Camera camera, Rect rect)
- {
- RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
- camera.targetTexture = rt;
- camera.Render();
- RenderTexture.active = rt;
- Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGBA32, true);
- screenShot.ReadPixels(rect, 0, 125);
- screenShot.Apply();
- camera.targetTexture = null;
- RenderTexture.active = null;
- GameObject.Destroy(rt);
-
- return screenShot;
-
- }
- void Update()
- {
- if (mat == null)
- {
- mat = new Material(Shader.Find("Shader/line"));
- }
- if (canUse)
- {
- if (Input.GetMouseButtonDown(0))
- {
- clone = (GameObject)Instantiate(obs, new Vector3(0, 0, this.transform.position.z), this.transform.rotation, lineparent.transform);
- clone.name += index.ToString();
- line = clone.AddComponent<LineRenderer>();
-
- line.material = new Material(Shader.Find("Sprites/Default"));
- switch(colorIndex)
- {
- case 1:
- mat.color = Color.black;
- break;
- case 2:
- mat.color = Color.black;
- break;
- case 3:
- mat.color = Color.black;
- break;
- case 4:
- mat.color = Color.black;
- break;
- default:
- mat.color = Color.black;
- break;
- }
- line.sortingOrder = 1;
-
- line.startColor = mat.color;
- line.endColor = mat.color;
-
- line.startWidth = 0.5f;
- line.endWidth = 0.5f;
- line.useWorldSpace = false;
- i = 0;
- index++;
- startpos = GetuiPos();
- }
- if (Input.GetMouseButton(0))
- {
- if (line != null)
- {
- if (Input.mousePosition.x < 0 || Input.mousePosition.x > Screen.width || Input.mousePosition.y < 0 || Input.mousePosition.y > Screen.height)
- {
- }
- listColor.SetActive(false);
- Vector3 pos = GetuiPos();
- if (Vector3.Distance(startpos, pos) > 0.5f)
- {
- i++;
- line.positionCount = i;
- line.SetPosition(i - 1, pos);
- }
- isqianming=true;
- }
- }
- if (Input.GetMouseButtonUp(0))
- {
- line = null;
-
- }
- }
- }
- public Vector2 GetuiPos()
- {
- Vector2 uisize = canvas.sizeDelta;
- Vector2 screenpos = Input.mousePosition;
- Vector2 screenpos2;
- screenpos2.x = screenpos.x - (Screen.width / 2);
- screenpos2.y = screenpos.y - (Screen.height / 2);
- Vector2 uipos;
- uipos.x = screenpos2.x * (uisize.x / Screen.width);
- uipos.y = screenpos2.y * (uisize.y / Screen.height);
- return uipos;
- }
-
- public void ClickPaint(int ci)
- {
- listColor.SetActive(false);
- colorIndex = ci;
- canUse = true;
- img.sprite = spriteList[ci - 1];
- }
- public void DeleteLine()
- {
- if (lineparent.transform.childCount > 0)
- {
- for (int i = 0; i < lineparent.transform.childCount; i++)
- {
- Destroy(lineparent.transform.GetChild(i).gameObject);
- }
- }
- line = null;
- }
- private void OnDisable()
- {
-
- Screen.orientation = ScreenOrientation.Portrait;
- Screen.autorotateToLandscapeLeft = false;
- Screen.autorotateToLandscapeRight = false;
- Screen.autorotateToPortrait = false;
- Screen.autorotateToPortraitUpsideDown = false;
- if (lineparent.transform.childCount > 0)
- {
- for (int i = 0; i < lineparent.transform.childCount; i++)
- {
- Destroy(lineparent.transform.GetChild(i).gameObject);
- }
- }
- }
- }
|