Advertisement
Guest User

Untitled

a guest
Apr 12th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.80 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class MouseManager : MonoBehaviour {
  4.     private const int LMB = 0;
  5.     private const int RMB = 1;
  6.  
  7.     private GameManager gm;
  8.     private bool dragSelectionFlag = false;
  9.     private Vector3 dragCornerInitial;
  10.     private Vector3 dragCornerFinal;
  11.  
  12.     private Mesh dragSelectionMesh;
  13.     private MeshCollider meshCollider;
  14.  
  15.     void OnCollisionEnter(Collision collision) {
  16.         Debug.Log("COLLIDE");
  17.     }
  18.  
  19.     void OnGUI() {
  20.         if (dragSelectionFlag) {
  21.             Rect dragRect = new Rect(Mathf.Min(dragCornerInitial.x, dragCornerFinal.x), Screen.height - Mathf.Max(dragCornerInitial.y, dragCornerFinal.y),
  22.                 Mathf.Abs(dragCornerInitial.x - dragCornerFinal.x), Mathf.Abs(dragCornerInitial.y - dragCornerFinal.y));
  23.  
  24.             Texture2D tex = new Texture2D(2, 2);
  25.  
  26.             for (int i = 0; i < tex.width; i++) {
  27.                 for (int j = 0; j < tex.height; j++) {
  28.                     tex.SetPixel(i, j, new Color(0.0f, 1.0f, 0.0f, 0.5f));
  29.                 }
  30.             }
  31.  
  32.             tex.Apply();
  33.             GUIStyle tempstyle = new GUIStyle();
  34.             tempstyle.normal.background = tex;
  35.             GUILayout.BeginArea(dragRect, tempstyle);
  36.             GUILayout.EndArea();
  37.         }
  38.     }
  39.  
  40.     void Start() {
  41.         gm = transform.parent.gameObject.GetComponent<GameManager>();
  42.  
  43.         dragSelectionMesh = new Mesh();
  44.         dragSelectionMesh.name = "Drag Selection Mesh";
  45.         meshCollider = gameObject.GetComponent<MeshCollider>();
  46.         MeshFilter filter = gameObject.GetComponent<MeshFilter>();
  47.         filter.mesh = dragSelectionMesh;
  48.     }
  49.  
  50.     void Update() {
  51.         if (Input.GetMouseButtonDown(LMB)) {
  52.             dragSelectionFlag = true;
  53.             dragCornerInitial = dragCornerFinal = Input.mousePosition;
  54.         } else if (Input.GetMouseButtonUp(LMB)) {
  55.             dragSelectionFlag = false;
  56.         }
  57.  
  58.         if (Input.GetMouseButtonDown(RMB)) {
  59.             dragSelectionFlag = false;
  60.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  61.             RaycastHit hit;
  62.             if (Physics.Raycast(ray, out hit)) {
  63.                 gm.MovePlayer(0, hit.point);
  64.             }
  65.         }
  66.  
  67.         if (dragSelectionFlag) {
  68.             dragCornerFinal = Input.mousePosition;
  69.  
  70.             Vector3 topLeft = new Vector3(Mathf.Min(dragCornerInitial.x, dragCornerFinal.x), Mathf.Max(dragCornerInitial.y, dragCornerFinal.y));
  71.             Vector3 topRight = new Vector3(Mathf.Max(dragCornerInitial.x, dragCornerFinal.x), Mathf.Max(dragCornerInitial.y, dragCornerFinal.y));
  72.             Vector3 bottomRight = new Vector3(Mathf.Max(dragCornerInitial.x, dragCornerFinal.x), Mathf.Min(dragCornerInitial.y, dragCornerFinal.y));
  73.             Vector3 bottomLeft = new Vector3(Mathf.Min(dragCornerInitial.x, dragCornerFinal.x), Mathf.Min(dragCornerInitial.y, dragCornerFinal.y));
  74.  
  75.             Ray topLeftRay = Camera.main.ScreenPointToRay(topLeft);
  76.             Ray topRightRay = Camera.main.ScreenPointToRay(topRight);
  77.             Ray bottomRightRay = Camera.main.ScreenPointToRay(bottomRight);
  78.             Ray bottomLeftRay = Camera.main.ScreenPointToRay(bottomLeft);
  79.  
  80.             RaycastHit topLeftHit;
  81.             RaycastHit topRightHit;
  82.             RaycastHit bottomLeftHit;
  83.             RaycastHit bottomRightHit;
  84.  
  85.             Physics.Raycast(topLeftRay, out topLeftHit);
  86.             Physics.Raycast(topRightRay, out topRightHit);
  87.             Physics.Raycast(bottomRightRay, out bottomRightHit);
  88.             Physics.Raycast(bottomLeftRay, out bottomLeftHit);
  89.  
  90.             Vector3 topLeftNear = Camera.main.ScreenToWorldPoint(new Vector3(topLeft.x, topLeft.y, Camera.main.nearClipPlane));
  91.             Vector3 bottomRightNear = Camera.main.ScreenToWorldPoint(new Vector3(bottomRight.x, bottomRight.y, Camera.main.nearClipPlane));
  92.  
  93.             Vector3 topLeftFar = topLeftHit.point;
  94.             Vector3 topRightFar = topRightHit.point;
  95.             Vector3 bottomRightFar = bottomRightHit.point;
  96.             Vector3 bottomLeftFar = bottomLeftHit.point;
  97.  
  98.             dragSelectionMesh.Clear();
  99.  
  100.             #region Vertices
  101.             Vector3 p0 = new Vector3(topLeftFar.x, topLeftFar.y, topLeftFar.z);
  102.             Vector3 p1 = new Vector3(topRightFar.x, topRightFar.y, topRightFar.z);
  103.             Vector3 p2 = new Vector3(bottomRightFar.x, bottomRightFar.y, bottomRightFar.z);
  104.             Vector3 p3 = new Vector3(bottomLeftFar.x, bottomLeftFar.y, bottomLeftFar.z);
  105.  
  106.             Vector3 p4 = new Vector3(topLeftNear.x, bottomRightNear.y, bottomRightNear.z);
  107.             Vector3 p5 = new Vector3(bottomRightNear.x, bottomRightNear.y, bottomRightNear.z);
  108.             Vector3 p6 = new Vector3(bottomRightNear.x, topLeftNear.y, topLeftNear.z);
  109.             Vector3 p7 = new Vector3(topLeftNear.x, topLeftNear.y, topLeftNear.z);
  110.  
  111.             Vector3[] vertices = new Vector3[] {
  112.                 // Bottom
  113.                 p1, p2, p3, p0,
  114.  
  115.                 // Left
  116.                 p3, p4, p7, p0,
  117.  
  118.                 // Front
  119.                 p3, p2, p5, p4,
  120.  
  121.                 // Back
  122.                 p1, p0, p7, p6,
  123.  
  124.                 // Right
  125.                 p2, p1, p6, p5,
  126.  
  127.                 // Top
  128.                 p4, p5, p6, p7
  129.             };
  130.             #endregion
  131.  
  132.             #region Normales
  133.             Vector3 up = Vector3.up;
  134.             Vector3 down = Vector3.down;
  135.             Vector3 front = Vector3.forward;
  136.             Vector3 back = Vector3.back;
  137.             Vector3 left = Vector3.left;
  138.             Vector3 right = Vector3.right;
  139.  
  140.             Vector3[] normales = new Vector3[] {
  141.                 // Bottom
  142.                 down, down, down, down,
  143.  
  144.                 // Left
  145.                 left, left, left, left,
  146.  
  147.                 // Front
  148.                 front, front, front, front,
  149.  
  150.                 // Back
  151.                 back, back, back, back,
  152.  
  153.                 // Right
  154.                 right, right, right, right,
  155.  
  156.                 // Top
  157.                 up, up, up, up
  158.             };
  159.             #endregion
  160.  
  161.             #region UVs
  162.             Vector2 _00 = new Vector2(0f, 0f);
  163.             Vector2 _10 = new Vector2(1f, 0f);
  164.             Vector2 _01 = new Vector2(0f, 1f);
  165.             Vector2 _11 = new Vector2(1f, 1f);
  166.  
  167.             Vector2[] uvs = new Vector2[] {
  168.                 // Bottom
  169.                 _11, _01, _00, _10,
  170.  
  171.                 // Left
  172.                 _11, _01, _00, _10,
  173.  
  174.                 // Front
  175.                 _11, _01, _00, _10,
  176.  
  177.                 // Back
  178.                 _11, _01, _00, _10,
  179.  
  180.                 // Right
  181.                 _11, _01, _00, _10,
  182.  
  183.                 // Top
  184.                 _11, _01, _00, _10,
  185.             };
  186.             #endregion
  187.  
  188.             #region Triangles
  189.             int[] triangles = new int[] {
  190.                 // Bottom
  191.                 3, 1, 0,
  192.                 3, 2, 1,           
  193.  
  194.                 // Left
  195.                 3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
  196.                 3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
  197.  
  198.                 // Front
  199.                 3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
  200.                 3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
  201.  
  202.                 // Back
  203.                 3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
  204.                 3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
  205.  
  206.                 // Right
  207.                 3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
  208.                 3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
  209.  
  210.                 // Top
  211.                 3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
  212.                 3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
  213.  
  214.             };
  215.             #endregion
  216.  
  217.             dragSelectionMesh.MarkDynamic();
  218.             dragSelectionMesh.vertices = vertices;
  219.             dragSelectionMesh.normals = normales;
  220.             dragSelectionMesh.uv = uvs;
  221.             dragSelectionMesh.triangles = triangles;
  222.  
  223.             dragSelectionMesh.RecalculateBounds();
  224.             dragSelectionMesh.Optimize();
  225.             meshCollider.sharedMesh = dragSelectionMesh;
  226.         }
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement