├── .gitignore ├── Assets ├── Editor.meta ├── Editor │ ├── GridEditor.cs │ └── GridEditor.cs.meta ├── Pictures.meta ├── Pictures │ ├── Dirt-1.png │ ├── Dirt-1.png.meta │ ├── Dirt-Grass-1.png │ └── Dirt-Grass-1.png.meta ├── Prefabs.meta ├── Prefabs │ ├── GameObject 1.prefab │ ├── GameObject 1.prefab.meta │ ├── Grass-ground.prefab │ ├── Grass-ground.prefab.meta │ ├── TileSet.asset │ └── TileSet.asset.meta ├── Scripts.meta ├── Scripts │ ├── Grid.cs │ ├── Grid.cs.meta │ ├── GridWindow.cs │ ├── GridWindow.cs.meta │ ├── TileSet.cs │ └── TileSet.cs.meta ├── testLevel.unity └── testLevel.unity.meta ├── LICENSE ├── ProjectSettings ├── AudioManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NavMeshLayers.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── ProjectVersion.txt ├── QualitySettings.asset ├── TagManager.asset ├── TimeManager.asset ├── UnityAdsSettings.asset └── UnityAnalyticsManager.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | [Bb]uild/ 5 | 6 | # Autogenerated VS/MD solution and project files 7 | *.csproj 8 | *.unityproj 9 | *.sln 10 | *.suo 11 | <<<<<<< HEAD 12 | ======= 13 | *.tmp 14 | >>>>>>> bb29ed18aa056f6900de7e0c1f3253b81a1c8ea8 15 | *.user 16 | *.userprefs 17 | *.pidb 18 | *.booproj 19 | 20 | <<<<<<< HEAD 21 | # Unity3D Generated File On Crash Reports 22 | sysinfo.txt 23 | 24 | # Certificates 25 | *.pfx 26 | *.pfx.meta 27 | ======= 28 | # Unity3D generated meta files 29 | *.pidb.meta 30 | 31 | # Unity3D Generated File On Crash Reports 32 | sysinfo.txt 33 | >>>>>>> bb29ed18aa056f6900de7e0c1f3253b81a1c8ea8 34 | -------------------------------------------------------------------------------- /Assets/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9b5cc7565f2abe142bd3d2465431636c 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/Editor/GridEditor.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | 4 | using System.Collections; 5 | using System.IO; 6 | 7 | [CustomEditor(typeof(Grid))] 8 | public class GridEditor : Editor { 9 | 10 | Grid grid; 11 | 12 | private int oldIndex = 0; 13 | 14 | private Vector3 mouseBeginPos; 15 | private Vector3 mouseEndPos; 16 | 17 | void OnEnable(){ 18 | grid = (Grid)target; 19 | } 20 | 21 | [MenuItem("Assets/Create/TileSet")] 22 | static void CreateTileSet(){ 23 | var asset = ScriptableObject.CreateInstance(); 24 | var path = AssetDatabase.GetAssetPath(Selection.activeObject); 25 | 26 | if(string.IsNullOrEmpty(path)){ 27 | path = "Assets"; 28 | }else if(Path.GetExtension(path) != ""){ 29 | path = path.Replace (Path.GetFileName(path),""); 30 | }else{ 31 | path+="/"; 32 | } 33 | 34 | var assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "TileSet.asset"); 35 | AssetDatabase.CreateAsset(asset,assetPathAndName); 36 | AssetDatabase.SaveAssets(); 37 | EditorUtility.FocusProjectWindow(); 38 | Selection.activeObject = asset; 39 | asset.hideFlags = HideFlags.DontSave; 40 | } 41 | 42 | public override void OnInspectorGUI(){ 43 | //base.OnInspectorGUI(); 44 | 45 | grid.width = createSlider("Width",grid.width); 46 | grid.height = createSlider("Height", grid.height); 47 | 48 | if(GUILayout.Button("Open Grid Window")){ 49 | GridWindow window = (GridWindow)EditorWindow.GetWindow (typeof(GridWindow)); 50 | window.init(); 51 | } 52 | 53 | // Tile Prefab 54 | EditorGUI.BeginChangeCheck(); 55 | var newTilePrefab = (Transform)EditorGUILayout.ObjectField("Tile Prefab",grid.tilePrefab,typeof(Transform),false); 56 | if(EditorGUI.EndChangeCheck()){ 57 | grid.tilePrefab = newTilePrefab; 58 | Undo.RecordObject(target,"Grid Changed"); 59 | } 60 | 61 | //Tile Map 62 | EditorGUI.BeginChangeCheck(); 63 | var newTileSet = (TileSet) EditorGUILayout.ObjectField("Tileset", grid.tileSet,typeof(TileSet),false); 64 | if(EditorGUI.EndChangeCheck()){ 65 | grid.tileSet = newTileSet; 66 | Undo.RecordObject(target,"Grid Changed"); 67 | } 68 | 69 | if(grid.tileSet != null){ 70 | EditorGUI.BeginChangeCheck(); 71 | var names = new string[grid.tileSet.prefabs.Length]; 72 | var values = new int[names.Length]; 73 | 74 | for(int i = 0; i < names.Length;i++){ 75 | names[i] = grid.tileSet.prefabs[i] != null ? grid.tileSet.prefabs[i].name : ""; 76 | values[i] = i; 77 | } 78 | 79 | var index = EditorGUILayout.IntPopup("Select Tile",oldIndex,names,values); 80 | 81 | if(EditorGUI.EndChangeCheck()){ 82 | Undo.RecordObject(target,"Grid Changed"); 83 | if(oldIndex != index){ 84 | oldIndex = index; 85 | grid.tilePrefab = grid.tileSet.prefabs[index]; 86 | 87 | float width = grid.tilePrefab.GetComponent().bounds.size.x; 88 | float height = grid.tilePrefab.GetComponent().bounds.size.y; 89 | 90 | grid.width = width; 91 | grid.height = height; 92 | 93 | } 94 | } 95 | } 96 | 97 | EditorGUI.BeginChangeCheck(); 98 | 99 | bool draggable = EditorGUILayout.Toggle ("Toggle Dragging: ", grid.draggable); 100 | if(EditorGUI.EndChangeCheck()){ 101 | grid.draggable = draggable; 102 | } 103 | } 104 | 105 | private float createSlider(string labelName, float sliderPosition){ 106 | GUILayout.BeginHorizontal(); 107 | GUILayout.Label ("Grid " + labelName); 108 | sliderPosition = EditorGUILayout.Slider(sliderPosition,1f,100f,null); 109 | GUILayout.EndHorizontal(); 110 | 111 | return sliderPosition; 112 | } 113 | 114 | void OnSceneGUI(){ 115 | int controlId = GUIUtility.GetControlID(FocusType.Passive); 116 | Event e = Event.current; 117 | Ray ray = Camera.current.ScreenPointToRay (new Vector3(e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight)); 118 | Vector3 mousePos = ray.origin; 119 | 120 | // create tile 121 | if(e.isMouse && e.type ==EventType.MouseDown && e.button == 0){ 122 | GUIUtility.hotControl = controlId; 123 | e.Use(); 124 | 125 | if(grid.draggable){ 126 | mouseBeginPos = mousePos; 127 | }else{ 128 | 129 | GameObject gameObject; 130 | Transform prefab = grid.tilePrefab; 131 | 132 | if(prefab){ 133 | Undo.IncrementCurrentGroup(); 134 | Vector3 aligned = new Vector3(Mathf.Floor (mousePos.x/grid.width) * grid.width + grid.width/2.0f,Mathf.Floor (mousePos.y/grid.height) * grid.height + grid.height/2.0f,0.0f); 135 | 136 | if(GetTransformFromPosition(aligned) != null) return; 137 | 138 | gameObject = (GameObject) PrefabUtility.InstantiatePrefab (prefab.gameObject); 139 | gameObject.transform.position = aligned; 140 | gameObject.transform.parent = grid.transform; 141 | 142 | Undo.RegisterCreatedObjectUndo(gameObject, " Create " + gameObject.name); 143 | } 144 | } 145 | } 146 | 147 | if(e.isMouse & e.type == EventType.MouseDown && (e.button == 0 || e.button == 1)){ 148 | GUIUtility.hotControl = controlId; 149 | e.Use(); 150 | Vector3 aligned = new Vector3(Mathf.Floor (mousePos.x/grid.width) * grid.width + grid.width/2.0f,Mathf.Floor (mousePos.y/grid.height) * grid.height + grid.height/2.0f,0.0f); 151 | Transform transform = GetTransformFromPosition(aligned); 152 | if(transform != null){ 153 | DestroyImmediate( transform.gameObject); 154 | } 155 | } 156 | 157 | if((e.isMouse && e.type == EventType.MouseUp && (e.button == 0 || e.button == 1))){ 158 | if(grid.draggable && e.button == 0){ 159 | mouseEndPos = mousePos; 160 | FillArea(mouseBeginPos, mouseEndPos); 161 | 162 | mouseEndPos = Vector3.zero; 163 | mouseBeginPos = Vector3.zero; 164 | } 165 | 166 | GUIUtility.hotControl = 0; 167 | } 168 | 169 | 170 | } 171 | 172 | 173 | Transform GetTransformFromPosition( Vector3 aligned){ 174 | 175 | int i = 0; 176 | while( i < grid.transform.childCount){ 177 | Transform transform = grid.transform.GetChild (i); 178 | if( transform.position == aligned){ 179 | return transform; 180 | } 181 | 182 | i++; 183 | } 184 | return null; 185 | } 186 | 187 | 188 | void FillArea(Vector3 _StartPosition, Vector3 _EndPosition){ 189 | 190 | Transform prefab = grid.tilePrefab; 191 | if(prefab == null){ 192 | Debug.LogError("No prefab attached to grid."); 193 | } 194 | 195 | // contains the number of tiles to the real start position, this means we begin in (0,0,0).... our value might be (2.0f,1.0f) then we begin at the position = 0 + 2.0f * grid.width 196 | Vector2 numberOfTilesToStartPosition = new Vector2(); 197 | Vector2 numberOfTilesToEndPosition = new Vector2(); 198 | 199 | Vector2 tilesToFill = new Vector2(); 200 | _StartPosition.x = Mathf.Floor(_StartPosition.x /grid.width) * grid.width; 201 | _StartPosition.y = Mathf.Floor(_StartPosition.y /grid.height) * grid.height; 202 | 203 | _EndPosition.x = Mathf.Floor(_EndPosition.x /grid.width) * grid.width; 204 | _EndPosition.y = Mathf.Floor(_EndPosition.y /grid.height) * grid.height ; 205 | 206 | Vector2 numberOfTilesToFill = new Vector2(); 207 | 208 | 209 | // look if there is a drag from right to left or bottom to top 210 | // if so swap entries 211 | if(_StartPosition.x > _EndPosition.x){ 212 | numberOfTilesToFill.x = _StartPosition.x - _EndPosition.x; 213 | }else{ 214 | numberOfTilesToFill.x = _EndPosition.x - _StartPosition.x; 215 | } 216 | 217 | if(_StartPosition.y > _EndPosition.y){ 218 | numberOfTilesToFill.y = _StartPosition.y - _EndPosition.y; 219 | }else{ 220 | numberOfTilesToFill.y = _EndPosition.y - _StartPosition.y; 221 | } 222 | 223 | // swap to fill from left to right 224 | if(_StartPosition.x >= _EndPosition.x){ 225 | Vector3 tmp = new Vector3(); 226 | tmp = _EndPosition; 227 | _StartPosition = _EndPosition; 228 | _EndPosition = tmp; 229 | } 230 | 231 | numberOfTilesToFill.x = numberOfTilesToFill.x / grid.width + 1.0f; 232 | numberOfTilesToFill.y = numberOfTilesToFill.y / grid.height + 1.0f; 233 | // save them 234 | int currentXTileNumber = 0; 235 | int currentYTileNumber = 0; 236 | 237 | 238 | do{ 239 | currentYTileNumber = 0; 240 | do{ 241 | 242 | Vector3 realWorldPosition = new Vector3(); 243 | GameObject gameObject; 244 | 245 | realWorldPosition.x = _StartPosition.x + (currentXTileNumber * grid.width) + grid.width / 2.0f; 246 | realWorldPosition.y = _StartPosition.y - (currentYTileNumber * grid.height) + grid.height / 2.0f; 247 | realWorldPosition.z = 0.0f; 248 | // if(GetTransformFromPosition(realWorldPosition) != null) continue; 249 | 250 | gameObject = (GameObject) PrefabUtility.InstantiatePrefab (prefab.gameObject); 251 | gameObject.transform.position = realWorldPosition; 252 | gameObject.transform.parent = grid.transform; 253 | 254 | 255 | ++currentYTileNumber; 256 | }while(currentYTileNumber < numberOfTilesToFill.y); 257 | 258 | ++ currentXTileNumber; 259 | }while(currentXTileNumber < numberOfTilesToFill.x); 260 | 261 | } 262 | 263 | // void FillArea(Vector3 beginPos, Vector3 endPos){ 264 | // int negX = 1; 265 | // int negY = 1; 266 | // int modX = 0; 267 | // int modY = 0; 268 | // 269 | // Vector3 beginChunks = new Vector3(Mathf.Floor (beginPos.x/grid.width) ,Mathf.Floor (beginPos.y/grid.height) ,0.0f); 270 | // Vector3 endChunks = new Vector3(Mathf.Floor (endPos.x/grid.width) ,Mathf.Floor (endPos.y/grid.height) ,0.0f); 271 | // 272 | // Vector3 beginChunk = new Vector3(Mathf.Floor (beginPos.x/grid.width),Mathf.Floor (beginPos.y/grid.height) ,0.0f); 273 | // 274 | // Vector3 tilesToDraw = endChunks - beginChunks; 275 | // 276 | // int x = (int)tilesToDraw.x; // +1 because of first tile is 0 277 | // int y = (int)tilesToDraw.y; 278 | // 279 | // if(x < 0) { 280 | // negX = -1; 281 | // x = (-1)*(x-1); 282 | // modX = -1; 283 | // }else{ 284 | // x = x+1; 285 | // } 286 | // if(y < 0) 287 | // { 288 | // negY = -1; 289 | // y = (-1)*(y-1); 290 | // modY = -1; 291 | // }else{ 292 | // y = y + 1; 293 | // } 294 | // 295 | // Debug.Log ("x " + x + " " + y); 296 | // Transform prefab = grid.tilePrefab; 297 | // // Undo.IncrementCurrentGroup(); 298 | // Vector3 aligned = new Vector3(); 299 | // 300 | // 301 | // for(int i = 0; i< x; i++){ 302 | // 303 | // Debug.Log (i); 304 | // aligned.x = (i+modX)*grid.width*negX+(beginChunk.x*grid.width) +grid.width*negX/2; 305 | // for(int j =0; j