using System.Collections; using System.Collections.Generic; using UnityEngine; public class rectSelect : MonoBehaviour { bool canSelect = false; bool isDrag = false; Manager man = null; public Texture2D mouseCursor; Vector2 p1, p2; float rectAlpha = 1f; bool rectAlphaDir = false; float rectW = 1000f; float rectH = 1000f; public Texture2D boxTexture; void Start() { man = GameObject.Find("Manager").GetComponent(); } void Update() { if (!canSelect) return; if (!isDrag && Input.GetMouseButtonDown(0)) { isDrag = true; p1 = Input.mousePosition; } if (isDrag && Input.GetMouseButtonUp(0)) { isDrag = false; canSelect = false; Cursor.SetCursor(null, Vector2.zero, CursorMode.ForceSoftware); //p1 = new Vector3(p1.x, Screen.height - p1.y, p1.z); //p2 = new Vector3(Input.mousePosition.x, Screen.height - Input.mousePosition.y, Input.mousePosition.z); //if (man == null) Start(); //Vector3 g1 = man.cam.ScreenToWorldPoint(p1); //Vector3 g2 = man.cam.ScreenToWorldPoint(p2); //GameObject.Find("Panel1Content").GetComponent().InsActShowRect(g1.x, g1.y, g1.z, g2.x, g2.y, g2.z); Vector2 g1 = GUIUtility.ScreenToGUIPoint(p1); Vector2 g2 = GUIUtility.ScreenToGUIPoint(p2); //float scrCoef = ((float)Screen.width / (float)Screen.height) / (16f / 9f); int x1 = (int)( rectW * ( (p1.x - Screen.width/2f) / Screen.width) ); int y1 = (int)( rectH * ( (Screen.height/2f - p1.y) / (Screen.height)) ); // *scrCoef приводим высоту экрана к соотношению 16:9 int x2 = (int)( rectW * ( (p2.x - Screen.width / 2f) / Screen.width) ); int y2 = (int)( rectH * ( (Screen.height / 2f - p2.y) / (Screen.height)) ); //* scrCoef GameObject.Find("Panel1Content").GetComponent().InsActShowRect(x1, y1, x2, y2); } } void DrawLine(float x1, float y1, float x2, float y2, float a = 1f) { Vector2 pointA = new Vector2(x1, Screen.height - y1); Vector2 pointB = new Vector2(x2, Screen.height - y2); Texture2D lineTex = new Texture2D(1, 1); Matrix4x4 matrixBackup = GUI.matrix; float width = 3.0f; GUI.color = new Color(0, 1, 0, a); float angle = Mathf.Atan2(pointB.y - pointA.y, pointB.x - pointA.x) * 180f / Mathf.PI; GUIUtility.RotateAroundPivot(angle, pointA); GUI.DrawTexture(new Rect(pointA.x - width / 2f, pointA.y-width/2f, Mathf.Max(Mathf.Abs(pointA.x - pointB.x), Mathf.Abs(pointA.y - pointB.y)), width), lineTex);//lineTex Mathf.Abs(pointA.x-pointB.x), Mathf.Abs(pointA.y - pointB.y) GUI.matrix = matrixBackup; } void OnGUI() { if (!canSelect || !isDrag) return; if (man.mode != "scedit") return; p2 = Input.mousePosition; Vector2 g1 = GUIUtility.ScreenToGUIPoint(p1); Vector2 g2 = GUIUtility.ScreenToGUIPoint(p2); DrawLine(g1.x, g1.y, g2.x, g1.y, rectAlpha); DrawLine(g2.x, g1.y, g2.x, g2.y, rectAlpha); DrawLine(g2.x, g2.y, g1.x, g2.y, rectAlpha); DrawLine(g1.x, g2.y, g1.x, g1.y, rectAlpha); if (rectAlphaDir) rectAlpha += 0.004f; else rectAlpha -= 0.004f; if (rectAlphaDir && rectAlpha >= 1f) { rectAlpha = 1f; rectAlphaDir = false; } if (!rectAlphaDir && rectAlpha <= 0.3f) { rectAlpha = 0.3f; rectAlphaDir = true; } } public void StartSelectRect() { if (man.mode != "scedit") return; canSelect = true; Vector2 hotSpot = new Vector2(24, 24); Cursor.SetCursor(mouseCursor, hotSpot, CursorMode.Auto); } }