├── Demo ├── .gitignore ├── Assets │ ├── RTSCamera.cs │ ├── RTSCamera.cs.meta │ ├── Scene.unity │ ├── Scene.unity.meta │ ├── Selectable.cs │ ├── Selectable.cs.meta │ ├── Selectable.prefab │ └── Selectable.prefab.meta └── ProjectSettings │ ├── AudioManager.asset │ ├── DynamicsManager.asset │ ├── EditorBuildSettings.asset │ ├── EditorSettings.asset │ ├── GraphicsSettings.asset │ ├── InputManager.asset │ ├── NavMeshLayers.asset │ ├── NetworkManager.asset │ ├── Physics2DSettings.asset │ ├── ProjectSettings.asset │ ├── QualitySettings.asset │ ├── TagManager.asset │ └── TimeManager.asset ├── LICENSE.txt ├── README.md └── RTSCamera.cs /Demo/.gitignore: -------------------------------------------------------------------------------- 1 | # Unity 2 | [Ll]ibrary/ 3 | [Tt]emp/ 4 | [Oo]bj/ 5 | [Bb]uild/ 6 | /*.csproj 7 | /*.unityproj 8 | /*.sln 9 | /*.suo 10 | /*.user 11 | /*.userprefs 12 | /*.pidb 13 | /*.booproj 14 | sysinfo.txt 15 | 16 | # OSX 17 | .DS_Store 18 | .AppleDouble 19 | .LSOverride 20 | # Icon must end with two \r 21 | Icon 22 | ._* 23 | .Spotlight-V100 24 | .Trashes 25 | .AppleDB 26 | .AppleDesktop 27 | Network Trash Folder 28 | Temporary Items 29 | .apdisk 30 | -------------------------------------------------------------------------------- /Demo/Assets/RTSCamera.cs: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | RTS camera for Unity 4 | 5 | The MIT License (MIT) 6 | 7 | Copyright (c) 2014 Evan Hahn 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in all 17 | copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | */ 28 | 29 | using UnityEngine; 30 | using System.Collections; 31 | 32 | public class RTSCamera : MonoBehaviour { 33 | 34 | public bool disablePanning = false; 35 | public bool disableSelect = false; 36 | public bool disableZoom = false; 37 | 38 | public float maximumZoom = 1f; 39 | public float minimumZoom = 20f; 40 | public Color selectColor = Color.green; 41 | public float selectLineWidth = 2f; 42 | 43 | public float lookDamper = 5f; 44 | public string selectionObjectName = "RTS Selection"; 45 | 46 | private readonly string[] INPUT_MOUSE_BUTTONS = {"Mouse Look", "Mouse Select"}; 47 | private bool ready; 48 | private bool[] isDragging = new bool[2]; 49 | private Vector3 selectStartPosition; 50 | private Texture2D pixel; 51 | private GameObject selection; 52 | private bool debugSelection; 53 | 54 | void Start() { 55 | try { 56 | startupChecks(); 57 | ready = true; 58 | } catch (UnityException exception) { 59 | ready = false; 60 | throw exception; 61 | } 62 | setPixel(selectColor); 63 | debugSelection = true; 64 | } 65 | 66 | void Update() { 67 | if (!ready) { return; } 68 | updateDragging(); 69 | updateLook(); 70 | updateZoom(); 71 | } 72 | 73 | void OnGUI() { 74 | if (!ready) { return; } 75 | updateSelect(); 76 | } 77 | 78 | private void updateDragging() { 79 | for (int index = 0; index <= 1; index ++) { 80 | if (isClicking(index) && !isDragging[index]) { 81 | isDragging[index] = true; 82 | if (index == 1) { 83 | selectStartPosition = Input.mousePosition; 84 | } 85 | } else if (!isClicking(index) && isDragging[index]) { 86 | isDragging[index] = false; 87 | if (index == 1) { 88 | dropSelection(selectStartPosition, Input.mousePosition); 89 | } 90 | } 91 | } 92 | } 93 | 94 | private void updateLook() { 95 | if (!isDragging[0] || disablePanning) { return; } 96 | var newPosition = transform.position; 97 | var mousePosition = getMouseMovement(); 98 | newPosition.x = newPosition.x - (mousePosition.x * camera.orthographicSize / lookDamper); 99 | newPosition.y = newPosition.y - (mousePosition.y * camera.orthographicSize / lookDamper); 100 | transform.position = newPosition; 101 | } 102 | 103 | private void updateSelect() { 104 | if (!isDragging[1] || disableSelect) { return; } 105 | var x = selectStartPosition.x; 106 | var y = Screen.height - selectStartPosition.y; 107 | var width = (Input.mousePosition - selectStartPosition).x; 108 | var height = (Screen.height - Input.mousePosition.y) - y; 109 | GUI.DrawTexture(new Rect(x, y, width, selectLineWidth), pixel); 110 | GUI.DrawTexture(new Rect(x, y, selectLineWidth, height), pixel); 111 | GUI.DrawTexture(new Rect(x, y + height, width, selectLineWidth), pixel); 112 | GUI.DrawTexture(new Rect(x + width, y, selectLineWidth, height), pixel); 113 | } 114 | 115 | private void updateZoom() { 116 | if (disableZoom) { return; } 117 | var newSize = camera.orthographicSize - Input.GetAxis("Mouse ScrollWheel"); 118 | newSize = Mathf.Clamp(newSize, maximumZoom, minimumZoom); 119 | camera.orthographicSize = newSize; 120 | } 121 | 122 | private bool isClicking(int index) { 123 | return Input.GetAxis(INPUT_MOUSE_BUTTONS[index]) == 1f; 124 | } 125 | 126 | private Vector2 getMouseMovement() { 127 | return new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); 128 | } 129 | 130 | private void setPixel(Color color) { 131 | pixel = new Texture2D(1, 1); 132 | pixel.SetPixel(0, 0, color); 133 | pixel.Apply(); 134 | } 135 | 136 | private void startupChecks() { 137 | if (!camera) { 138 | throw new MissingComponentException("RTS Camera must be attached to a camera."); 139 | } 140 | try { 141 | Input.GetAxis(INPUT_MOUSE_BUTTONS[0]); 142 | Input.GetAxis(INPUT_MOUSE_BUTTONS[1]); 143 | } catch (UnityException) { 144 | throw new UnassignedReferenceException("Inputs " + INPUT_MOUSE_BUTTONS[0] + " and " + 145 | INPUT_MOUSE_BUTTONS[1] + " must be defined."); 146 | } 147 | } 148 | 149 | private void dropSelection(Vector3 screenStart, Vector3 screenEnd) { 150 | if (!selection) { 151 | selection = new GameObject(selectionObjectName); 152 | { 153 | var collider = selection.AddComponent() as BoxCollider; 154 | collider.isTrigger = true; 155 | var size = collider.size; 156 | size.z = 1000000f; // super friggin tall 157 | collider.size = size; 158 | } 159 | { 160 | var body = selection.AddComponent() as Rigidbody; 161 | body.useGravity = false; 162 | } 163 | } 164 | { 165 | var start = camera.ScreenToWorldPoint(screenStart); 166 | var finish = camera.ScreenToWorldPoint(screenEnd); 167 | selection.transform.position = new Vector3( 168 | (start.x + finish.x) / 2, 169 | (start.y + finish.y) / 2, 170 | 0.5f); 171 | selection.transform.localScale = new Vector3( 172 | Mathf.Abs(start.x - finish.x), 173 | Mathf.Abs(start.y - finish.y), 174 | 1f); 175 | } 176 | } 177 | 178 | } 179 | -------------------------------------------------------------------------------- /Demo/Assets/RTSCamera.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c71a8349d5bf94df3b203d64bed585ea 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Demo/Assets/Scene.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/Assets/Scene.unity -------------------------------------------------------------------------------- /Demo/Assets/Scene.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d87b8577f01ec414da084c1031b4ff60 3 | DefaultImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Demo/Assets/Selectable.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Selectable : MonoBehaviour { 5 | 6 | void Start() { 7 | renderer.material.color = Color.red; 8 | } 9 | 10 | void OnTriggerEnter(Collider other) { 11 | if (other.gameObject.name == "RTS Selection") { 12 | renderer.material.color = Color.green; 13 | } 14 | } 15 | 16 | void OnTriggerExit(Collider other) { 17 | if (other.gameObject.name == "RTS Selection") { 18 | renderer.material.color = Color.red; 19 | } 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Demo/Assets/Selectable.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 11a0c3a894afe4e26a14d421f5d56273 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Demo/Assets/Selectable.prefab: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/Assets/Selectable.prefab -------------------------------------------------------------------------------- /Demo/Assets/Selectable.prefab.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 223403e3a8d7a44efabd743866239a42 3 | NativeFormatImporter: 4 | userData: 5 | -------------------------------------------------------------------------------- /Demo/ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/NavMeshLayers.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/NavMeshLayers.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /Demo/ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EvanHahn/Unity-RTS-camera/b94b7752bc83ab40cdf050630d66076731f0a8d2/Demo/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Evan Hahn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | RTS camera for Unity 2 | ==================== 3 | 4 | Attach this script to a camera and then you can use it RTS-style. 5 | 6 | ![](http://evanhahn.com/tape/unity_rts.gif) 7 | 8 | *Note: I'm a Unity newbie, so I'm sorry if I don't know what I'm doing! Happy to accept pull requests.* 9 | 10 | ## Quick start 11 | 12 | 1. Attach this script to a camera. 13 | 2. Add "Mouse Look" and "Mouse Select" to your inputs. I recommend changing the default Fire1 and Fire2 for left and right mouse buttons. 14 | 3. Make sure selectable objects have 3D colliders on them. 15 | 4. For any selectable object, add the following code: 16 | 17 | ```csharp 18 | void OnTriggerEnter(Collider other) { 19 | if (other.gameObject.name == "RTS Selection") { 20 | // This object has been selected; do stuff! 21 | } 22 | } 23 | 24 | void OnTriggerExit(Collider other) { 25 | if (other.gameObject.name == "RTS Selection") { 26 | // This object has been deselected; do stuff! 27 | } 28 | } 29 | ``` 30 | 31 | That's it! 32 | 33 | For more usage, check out the demo. 34 | 35 | ## Class RTSCamera 36 | 37 | One RTSCamera should be attached to the main camera for your game. 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 |
PropertyTypeDefaultDescription
disablePanningboolfalseWhen true, the player cannot pan the camera.
disableSelectboolfalseWhen true, the player cannot select.
disableZoomboolfalseWhen true, the player cannot zoom.
selectColorColorColor.greenThe selection box drawn on-screen will be this color.
selectLineWidthfloat2fThe selection box drawn on-screen will have this line width.
maximumZoomfloat1fMaximum zoom; minimum camera scale. (This value will be less than minimumZoom, which might seem backwards.)
minimumZoomfloat20fMinimum zoom; maximum camera scale. (This value will be greater than maximumZoom, which might seem backwards.)
lookDamperfloat5fPanning speed will be divided by this value. A higher number makes for slower panning.
selectionObjectNamestring"RTS Selection"When a selection happens, a trigger (a BoxCollider) will be dropped briefly into the scene. It will have this name.
112 | 113 | Enjoy! 114 | -------------------------------------------------------------------------------- /RTSCamera.cs: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | RTS camera for Unity 4 | 5 | The MIT License (MIT) 6 | 7 | Copyright (c) 2014 Evan Hahn 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | 16 | The above copyright notice and this permission notice shall be included in all 17 | copies or substantial portions of the Software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | */ 28 | 29 | using UnityEngine; 30 | using System.Collections; 31 | 32 | public class RTSCamera : MonoBehaviour { 33 | 34 | public bool disablePanning = false; 35 | public bool disableSelect = false; 36 | public bool disableZoom = false; 37 | 38 | public float maximumZoom = 1f; 39 | public float minimumZoom = 20f; 40 | public Color selectColor = Color.green; 41 | public float selectLineWidth = 2f; 42 | 43 | public float lookDamper = 5f; 44 | public string selectionObjectName = "RTS Selection"; 45 | 46 | private readonly string[] INPUT_MOUSE_BUTTONS = {"Mouse Look", "Mouse Select"}; 47 | private bool ready; 48 | private bool[] isDragging = new bool[2]; 49 | private Vector3 selectStartPosition; 50 | private Texture2D pixel; 51 | private GameObject selection; 52 | private bool debugSelection; 53 | 54 | void Start() { 55 | try { 56 | startupChecks(); 57 | ready = true; 58 | } catch (UnityException exception) { 59 | ready = false; 60 | throw exception; 61 | } 62 | setPixel(selectColor); 63 | debugSelection = true; 64 | } 65 | 66 | void Update() { 67 | if (!ready) { return; } 68 | updateDragging(); 69 | updateLook(); 70 | updateZoom(); 71 | } 72 | 73 | void OnGUI() { 74 | if (!ready) { return; } 75 | updateSelect(); 76 | } 77 | 78 | private void updateDragging() { 79 | for (int index = 0; index <= 1; index ++) { 80 | if (isClicking(index) && !isDragging[index]) { 81 | isDragging[index] = true; 82 | if (index == 1) { 83 | selectStartPosition = Input.mousePosition; 84 | } 85 | } else if (!isClicking(index) && isDragging[index]) { 86 | isDragging[index] = false; 87 | if (index == 1) { 88 | dropSelection(selectStartPosition, Input.mousePosition); 89 | } 90 | } 91 | } 92 | } 93 | 94 | private void updateLook() { 95 | if (!isDragging[0] || disablePanning) { return; } 96 | var newPosition = transform.position; 97 | var mousePosition = getMouseMovement(); 98 | newPosition.x = newPosition.x - (mousePosition.x * camera.orthographicSize / lookDamper); 99 | newPosition.y = newPosition.y - (mousePosition.y * camera.orthographicSize / lookDamper); 100 | transform.position = newPosition; 101 | } 102 | 103 | private void updateSelect() { 104 | if (!isDragging[1] || disableSelect) { return; } 105 | var x = selectStartPosition.x; 106 | var y = Screen.height - selectStartPosition.y; 107 | var width = (Input.mousePosition - selectStartPosition).x; 108 | var height = (Screen.height - Input.mousePosition.y) - y; 109 | GUI.DrawTexture(new Rect(x, y, width, selectLineWidth), pixel); 110 | GUI.DrawTexture(new Rect(x, y, selectLineWidth, height), pixel); 111 | GUI.DrawTexture(new Rect(x, y + height, width, selectLineWidth), pixel); 112 | GUI.DrawTexture(new Rect(x + width, y, selectLineWidth, height), pixel); 113 | } 114 | 115 | private void updateZoom() { 116 | if (disableZoom) { return; } 117 | var newSize = camera.orthographicSize - Input.GetAxis("Mouse ScrollWheel"); 118 | newSize = Mathf.Clamp(newSize, maximumZoom, minimumZoom); 119 | camera.orthographicSize = newSize; 120 | } 121 | 122 | private bool isClicking(int index) { 123 | return Input.GetAxis(INPUT_MOUSE_BUTTONS[index]) == 1f; 124 | } 125 | 126 | private Vector2 getMouseMovement() { 127 | return new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); 128 | } 129 | 130 | private void setPixel(Color color) { 131 | pixel = new Texture2D(1, 1); 132 | pixel.SetPixel(0, 0, color); 133 | pixel.Apply(); 134 | } 135 | 136 | private void startupChecks() { 137 | if (!camera) { 138 | throw new MissingComponentException("RTS Camera must be attached to a camera."); 139 | } 140 | try { 141 | Input.GetAxis(INPUT_MOUSE_BUTTONS[0]); 142 | Input.GetAxis(INPUT_MOUSE_BUTTONS[1]); 143 | } catch (UnityException) { 144 | throw new UnassignedReferenceException("Inputs " + INPUT_MOUSE_BUTTONS[0] + " and " + 145 | INPUT_MOUSE_BUTTONS[1] + " must be defined."); 146 | } 147 | } 148 | 149 | private void dropSelection(Vector3 screenStart, Vector3 screenEnd) { 150 | if (!selection) { 151 | selection = new GameObject(selectionObjectName); 152 | { 153 | var collider = selection.AddComponent() as BoxCollider; 154 | collider.isTrigger = true; 155 | var size = collider.size; 156 | size.z = 1000000f; // super friggin tall 157 | collider.size = size; 158 | } 159 | { 160 | var body = selection.AddComponent() as Rigidbody; 161 | body.useGravity = false; 162 | } 163 | } 164 | { 165 | var start = camera.ScreenToWorldPoint(screenStart); 166 | var finish = camera.ScreenToWorldPoint(screenEnd); 167 | selection.transform.position = new Vector3( 168 | (start.x + finish.x) / 2, 169 | (start.y + finish.y) / 2, 170 | 0.5f); 171 | selection.transform.localScale = new Vector3( 172 | Mathf.Abs(start.x - finish.x), 173 | Mathf.Abs(start.y - finish.y), 174 | 1f); 175 | } 176 | } 177 | 178 | } 179 | --------------------------------------------------------------------------------