Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class MouseManager : MonoBehaviour {
- private const int LMB = 0;
- private const int RMB = 1;
- private GameManager gm;
- private bool dragSelectionFlag = false;
- private Vector3 dragCornerInitial;
- private Vector3 dragCornerFinal;
- private Mesh dragSelectionMesh;
- private MeshCollider meshCollider;
- void OnCollisionEnter(Collision collision) {
- Debug.Log("COLLIDE");
- }
- void OnGUI() {
- if (dragSelectionFlag) {
- Rect dragRect = new Rect(Mathf.Min(dragCornerInitial.x, dragCornerFinal.x), Screen.height - Mathf.Max(dragCornerInitial.y, dragCornerFinal.y),
- Mathf.Abs(dragCornerInitial.x - dragCornerFinal.x), Mathf.Abs(dragCornerInitial.y - dragCornerFinal.y));
- Texture2D tex = new Texture2D(2, 2);
- for (int i = 0; i < tex.width; i++) {
- for (int j = 0; j < tex.height; j++) {
- tex.SetPixel(i, j, new Color(0.0f, 1.0f, 0.0f, 0.5f));
- }
- }
- tex.Apply();
- GUIStyle tempstyle = new GUIStyle();
- tempstyle.normal.background = tex;
- GUILayout.BeginArea(dragRect, tempstyle);
- GUILayout.EndArea();
- }
- }
- void Start() {
- gm = transform.parent.gameObject.GetComponent<GameManager>();
- dragSelectionMesh = new Mesh();
- dragSelectionMesh.name = "Drag Selection Mesh";
- meshCollider = gameObject.GetComponent<MeshCollider>();
- MeshFilter filter = gameObject.GetComponent<MeshFilter>();
- filter.mesh = dragSelectionMesh;
- }
- void Update() {
- if (Input.GetMouseButtonDown(LMB)) {
- dragSelectionFlag = true;
- dragCornerInitial = dragCornerFinal = Input.mousePosition;
- } else if (Input.GetMouseButtonUp(LMB)) {
- dragSelectionFlag = false;
- }
- if (Input.GetMouseButtonDown(RMB)) {
- dragSelectionFlag = false;
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit)) {
- gm.MovePlayer(0, hit.point);
- }
- }
- if (dragSelectionFlag) {
- dragCornerFinal = Input.mousePosition;
- Vector3 topLeft = new Vector3(Mathf.Min(dragCornerInitial.x, dragCornerFinal.x), Mathf.Max(dragCornerInitial.y, dragCornerFinal.y));
- Vector3 topRight = new Vector3(Mathf.Max(dragCornerInitial.x, dragCornerFinal.x), Mathf.Max(dragCornerInitial.y, dragCornerFinal.y));
- Vector3 bottomRight = new Vector3(Mathf.Max(dragCornerInitial.x, dragCornerFinal.x), Mathf.Min(dragCornerInitial.y, dragCornerFinal.y));
- Vector3 bottomLeft = new Vector3(Mathf.Min(dragCornerInitial.x, dragCornerFinal.x), Mathf.Min(dragCornerInitial.y, dragCornerFinal.y));
- Ray topLeftRay = Camera.main.ScreenPointToRay(topLeft);
- Ray topRightRay = Camera.main.ScreenPointToRay(topRight);
- Ray bottomRightRay = Camera.main.ScreenPointToRay(bottomRight);
- Ray bottomLeftRay = Camera.main.ScreenPointToRay(bottomLeft);
- RaycastHit topLeftHit;
- RaycastHit topRightHit;
- RaycastHit bottomLeftHit;
- RaycastHit bottomRightHit;
- Physics.Raycast(topLeftRay, out topLeftHit);
- Physics.Raycast(topRightRay, out topRightHit);
- Physics.Raycast(bottomRightRay, out bottomRightHit);
- Physics.Raycast(bottomLeftRay, out bottomLeftHit);
- Vector3 topLeftNear = Camera.main.ScreenToWorldPoint(new Vector3(topLeft.x, topLeft.y, Camera.main.nearClipPlane));
- Vector3 bottomRightNear = Camera.main.ScreenToWorldPoint(new Vector3(bottomRight.x, bottomRight.y, Camera.main.nearClipPlane));
- Vector3 topLeftFar = topLeftHit.point;
- Vector3 topRightFar = topRightHit.point;
- Vector3 bottomRightFar = bottomRightHit.point;
- Vector3 bottomLeftFar = bottomLeftHit.point;
- dragSelectionMesh.Clear();
- #region Vertices
- Vector3 p0 = new Vector3(topLeftFar.x, topLeftFar.y, topLeftFar.z);
- Vector3 p1 = new Vector3(topRightFar.x, topRightFar.y, topRightFar.z);
- Vector3 p2 = new Vector3(bottomRightFar.x, bottomRightFar.y, bottomRightFar.z);
- Vector3 p3 = new Vector3(bottomLeftFar.x, bottomLeftFar.y, bottomLeftFar.z);
- Vector3 p4 = new Vector3(topLeftNear.x, bottomRightNear.y, bottomRightNear.z);
- Vector3 p5 = new Vector3(bottomRightNear.x, bottomRightNear.y, bottomRightNear.z);
- Vector3 p6 = new Vector3(bottomRightNear.x, topLeftNear.y, topLeftNear.z);
- Vector3 p7 = new Vector3(topLeftNear.x, topLeftNear.y, topLeftNear.z);
- Vector3[] vertices = new Vector3[] {
- // Bottom
- p1, p2, p3, p0,
- // Left
- p3, p4, p7, p0,
- // Front
- p3, p2, p5, p4,
- // Back
- p1, p0, p7, p6,
- // Right
- p2, p1, p6, p5,
- // Top
- p4, p5, p6, p7
- };
- #endregion
- #region Normales
- Vector3 up = Vector3.up;
- Vector3 down = Vector3.down;
- Vector3 front = Vector3.forward;
- Vector3 back = Vector3.back;
- Vector3 left = Vector3.left;
- Vector3 right = Vector3.right;
- Vector3[] normales = new Vector3[] {
- // Bottom
- down, down, down, down,
- // Left
- left, left, left, left,
- // Front
- front, front, front, front,
- // Back
- back, back, back, back,
- // Right
- right, right, right, right,
- // Top
- up, up, up, up
- };
- #endregion
- #region UVs
- Vector2 _00 = new Vector2(0f, 0f);
- Vector2 _10 = new Vector2(1f, 0f);
- Vector2 _01 = new Vector2(0f, 1f);
- Vector2 _11 = new Vector2(1f, 1f);
- Vector2[] uvs = new Vector2[] {
- // Bottom
- _11, _01, _00, _10,
- // Left
- _11, _01, _00, _10,
- // Front
- _11, _01, _00, _10,
- // Back
- _11, _01, _00, _10,
- // Right
- _11, _01, _00, _10,
- // Top
- _11, _01, _00, _10,
- };
- #endregion
- #region Triangles
- int[] triangles = new int[] {
- // Bottom
- 3, 1, 0,
- 3, 2, 1,
- // Left
- 3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
- 3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
- // Front
- 3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
- 3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
- // Back
- 3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
- 3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
- // Right
- 3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
- 3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
- // Top
- 3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
- 3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
- };
- #endregion
- dragSelectionMesh.MarkDynamic();
- dragSelectionMesh.vertices = vertices;
- dragSelectionMesh.normals = normales;
- dragSelectionMesh.uv = uvs;
- dragSelectionMesh.triangles = triangles;
- dragSelectionMesh.RecalculateBounds();
- dragSelectionMesh.Optimize();
- meshCollider.sharedMesh = dragSelectionMesh;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement