UnitSelectionComponent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using UnityEngine;
  2. using System;
  3. using System.Linq;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. public class UnitSelectionComponent : MonoBehaviour
  8. {
  9. bool isSelecting = false;
  10. Vector3 mousePosition1;
  11. public GameObject selectionCirclePrefab;
  12. void Update()
  13. {
  14. // If we press the left mouse button, begin selection and remember the location of the mouse
  15. if( Input.GetMouseButtonDown( 0 ) )
  16. {
  17. isSelecting = true;
  18. mousePosition1 = Input.mousePosition;
  19. foreach( var selectableObject in FindObjectsOfType<SelectableUnitComponent>() )
  20. {
  21. if( selectableObject.selectionCircle != null )
  22. {
  23. Destroy( selectableObject.selectionCircle.gameObject );
  24. selectableObject.selectionCircle = null;
  25. }
  26. }
  27. }
  28. // If we let go of the left mouse button, end selection
  29. if( Input.GetMouseButtonUp( 0 ) )
  30. {
  31. var selectedObjects = new List<SelectableUnitComponent>();
  32. foreach( var selectableObject in FindObjectsOfType<SelectableUnitComponent>() )
  33. {
  34. if( IsWithinSelectionBounds( selectableObject.gameObject ) )
  35. {
  36. selectedObjects.Add( selectableObject );
  37. }
  38. }
  39. var sb = new StringBuilder();
  40. sb.AppendLine( string.Format( "Selecting [{0}] Units", selectedObjects.Count ) );
  41. foreach( var selectedObject in selectedObjects )
  42. sb.AppendLine( "-> " + selectedObject.gameObject.name );
  43. Debug.Log( sb.ToString() );
  44. isSelecting = false;
  45. }
  46. // Highlight all objects within the selection box
  47. if( isSelecting )
  48. {
  49. var shader1 = Shader.Find("Outlined/Silhouetted Bumped Diffuse");
  50. var shader2 = Shader.Find("Standard");
  51. foreach ( var selectableObject in FindObjectsOfType<SelectableUnitComponent>() )
  52. {
  53. if( IsWithinSelectionBounds( selectableObject.gameObject ) )
  54. {
  55. if( selectableObject.selectionCircle == null )
  56. {
  57. selectableObject.selectionCircle = Instantiate( selectionCirclePrefab );
  58. selectableObject.selectionCircle.transform.SetParent( selectableObject.transform, false );
  59. selectableObject.selectionCircle.transform.eulerAngles = new Vector3( 90, 0, 0 );
  60. selectableObject.GetComponent<Renderer>().material.shader = shader1;
  61. }
  62. }
  63. else
  64. {
  65. if( selectableObject.selectionCircle != null )
  66. {
  67. Destroy( selectableObject.selectionCircle.gameObject );
  68. selectableObject.selectionCircle = null;
  69. selectableObject.GetComponent<Renderer>().material.shader = shader2;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. public bool IsWithinSelectionBounds( GameObject gameObject )
  76. {
  77. if( !isSelecting )
  78. return false;
  79. var camera = Camera.main;
  80. var viewportBounds = Utils.GetViewportBounds( camera, mousePosition1, Input.mousePosition );
  81. return viewportBounds.Contains( camera.WorldToViewportPoint( gameObject.transform.position ) );
  82. }
  83. void OnGUI()
  84. {
  85. if( isSelecting )
  86. {
  87. // Create a rect from both mouse positions
  88. var rect = Utils.GetScreenRect( mousePosition1, Input.mousePosition );
  89. Utils.DrawScreenRect( rect, new Color( 0.8f, 0.8f, 0.95f, 0.25f ) );
  90. Utils.DrawScreenRectBorder( rect, 2, new Color( 0.8f, 0.8f, 0.95f ) );
  91. }
  92. }
  93. }