UnitSelectionComponent.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. foreach( var selectableObject in FindObjectsOfType<SelectableUnitComponent>() )
  50. {
  51. if( IsWithinSelectionBounds( selectableObject.gameObject ) )
  52. {
  53. if( selectableObject.selectionCircle == null )
  54. {
  55. selectableObject.selectionCircle = Instantiate( selectionCirclePrefab );
  56. selectableObject.selectionCircle.transform.SetParent( selectableObject.transform, false );
  57. selectableObject.selectionCircle.transform.eulerAngles = new Vector3( 90, 0, 0 );
  58. }
  59. }
  60. else
  61. {
  62. if( selectableObject.selectionCircle != null )
  63. {
  64. Destroy( selectableObject.selectionCircle.gameObject );
  65. selectableObject.selectionCircle = null;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. public bool IsWithinSelectionBounds( GameObject gameObject )
  72. {
  73. if( !isSelecting )
  74. return false;
  75. var camera = Camera.main;
  76. var viewportBounds = Utils.GetViewportBounds( camera, mousePosition1, Input.mousePosition );
  77. return viewportBounds.Contains( camera.WorldToViewportPoint( gameObject.transform.position ) );
  78. }
  79. void OnGUI()
  80. {
  81. if( isSelecting )
  82. {
  83. // Create a rect from both mouse positions
  84. var rect = Utils.GetScreenRect( mousePosition1, Input.mousePosition );
  85. Utils.DrawScreenRect( rect, new Color( 0.8f, 0.8f, 0.95f, 0.25f ) );
  86. Utils.DrawScreenRectBorder( rect, 2, new Color( 0.8f, 0.8f, 0.95f ) );
  87. }
  88. }
  89. }