using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
public class DrawRectOperate : MonoBehaviour
{
static DrawRectOperate _instance;
private Material m_material;
private float3 m_startPos;
private float3 m_endPos;
private bool Drow = false;
public static DrawRectOperate Instance()
{
return _instance;
}
private void Awake()
{
_instance = this;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Drow = true;
m_startPos = GetScreenTouchPos();
}
if (Drow)
{
m_endPos = GetScreenTouchPos();
}
if (Input.GetMouseButtonUp(0))
{
Drow = false;
m_startPos = 0;
m_endPos = 0;
DrawAreaByPos();
}
}
private void OnRenderObject()
{
if(Drow)
{
DrawAreaByPos();
}
}
void DrawAreaByPos()
{
DrawArea(new Rect(math.min(m_startPos.x, m_endPos.x), math.min(m_startPos.y, m_endPos.y),
math.max(m_startPos.x, m_endPos.x) - math.min(m_startPos.x, m_endPos.x),
math.max(m_startPos.y, m_endPos.y) - math.min(m_startPos.y, m_endPos.y)));
}
void DrawArea(Rect rect)
{
Begin();
GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0);
GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0);
GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0);
GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0);
End();
}
void Begin()
{
if (m_material == null)
m_material = new Material(Shader.Find("Unlit/Color"));
m_material.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
m_material.SetColor("_Color", Color.green);
}
void End()
{
GL.End();
}
public static Vector3 GetScreenTouchPos()
{
return GetTouchPosition();
}
public static Vector3 GetTouchPosition()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
return Input.mousePosition;
#else
return Input.touches[0].position;
#endif
}
}
|