void OnEnable(){ //associate all the initial variable InitialiseVariables (); //try to get the vtilesObjects vTilesObjects = GameObject.Find("TilesGroup"); if (vTilesObjects == null) { vTilesObjects = (GameObject)PrefabUtility.InstantiatePrefab (vTilesObject); } //clear old tiles ClearTilesPreview(); oldIndex = 0; grid = (Tds_Grid)target; //increase localscale with a formula if (grid != null) { vFactorScale = (1024 / grid.dimension); grid.width = 4f; grid.height = 4f; } //try to get the last Action or last levels GameObject vGameManagerObject = GameObject.Find("GameManager"); if (vGameManagerObject != null) { //make sure we go the gamemanager component if (vGameManagerObject.GetComponent<Tds_GameManager> () != null) { vGameManager = vGameManagerObject.GetComponent<Tds_GameManager> (); } } //get the teleport tile by default (hardcoded!) Texture2D vTexture2D = Resources.Load<Texture2D> ("Customs/TeleportIcon"); if (vTexture2D != null) { Tds_Texture vTds_Texture = new Tds_Texture(); vTds_Texture.vFilename = "Customs/TeleportIcon"; vTds_Texture.vTexture = vTexture2D; Texture2D vTextSelection = Resources.Load<Texture2D> ("Customs/teleportTile"); Tds_Texture vSelectionTexture = new Tds_Texture(); vSelectionTexture.vFilename = "Customs/teleportTile"; vSelectionTexture.vTexture = vTextSelection; //attach the pixeltexture to the grid if (grid != null) { grid.teleportPrefab = vTds_Texture; grid.selectionPrefab = vSelectionTexture; } } else Debug.Log ("default teleportTile is missing! Default Path = Resources/Custom/TeleportIcon.png"); //showse vAction = ""; TopMenuPickerIndex = -1; }
void HandleTexture (Tds_Texture vTexture) { //switch between skin if (grid.tilePrefab == vTexture) GUI.skin = vCustSkinSelected; else GUI.skin = vCustSkin; if (GUILayout.Button (new GUIContent ("", vTexture.vTexture), GUILayout.Width (50), GUILayout.Height (50))) { grid.tilePrefab = vTexture; } }
void OnSceneGUI() { /////////////TOP MENU///////////// //go back to normal skin GUI.skin = vNONESkin; ////////////////tile sie///////////// if (IsSizingTile) { GUILayout.BeginHorizontal (); //go back to normal skin GUI.skin = vNONESkin; //get the number of item to be shown vNbrButton = 3; vBoxSize = 65; //create a windows GUILayout.Window (3, new Rect (Screen.width - 50 - (vBoxSize * vNbrButton), 0, vNbrButton * 85, 100), (id) => { //by default it's -1 TileSize = -1; List<Texture2D> vSizeList = new List<Texture2D> (); vSizeList.Add (v1x1); vSizeList.Add (v2x2); vSizeList.Add (v3x3); //get the right size on the list TileSize = GUILayout.SelectionGrid (TileSize, vSizeList.ToArray (), 20);GUILayout.Height (vBoxSize); //check if we selected something if (TileSize > -1) { //increase that number by 1 TileSize++; //switch back to CreateTile SaveVariables ("CreateTile"); IsSizingTile = false; } }, ""); //go back to normal skin GUI.skin = null; GUILayout.EndHorizontal (); } ////////////////////////////////// /////////////COLOR PICKER/////////////// //check if we have something so we show them, the user will select which of them we want! //may have many many tile on the same grid! if (vTilesFound != null) if (vTilesFound.Count > 0) { //remove teleport tile before clicking on it grid.tilePrefab = null; GUILayout.BeginHorizontal (); //go back to normal skin GUI.skin = vCustSkin; //get the number of item to be shown vNbrButton = vTilesFound.Count; int vBoxSizeY = 96; int vBoxSizeX = 40; int vLargestX = 96; //make sure we got enought room to show the current texture foreach (Texture2D vCurText in vTilesFound) { //incease width for every tiles vBoxSizeX += vCurText.width; //the main bos is equal to the biggest tiles in the list if (vBoxSizeY < vCurText.height) vBoxSizeY = vCurText.height; //keep the longest so we can draw the button correctly if (vLargestX < vCurText.width) vLargestX = vCurText.width; } //create a windows GUILayout.Window (3, new Rect (Screen.width - (vLargestX*vNbrButton)-10, 20, (vLargestX*vNbrButton), vBoxSizeY), (id) => { ColorPickerIndex = -1; //get the right color picker on the list + ALSO get the same tiles specs EX : Ground, Order layer 40, levels, isdoor...) ColorPickerIndex = GUILayout.SelectionGrid (ColorPickerIndex, vTilesFound.ToArray (), vTilesFound.Count, GUILayout.Height(vBoxSizeY-20)); //check if we selected something if (ColorPickerIndex > -1) { Tds_Texture vTextureFound = new Tds_Texture (); //get the right texture to use! foreach (Tds_Folder vFolder in grid.tileSet.prefabs) foreach (Tds_Texture vTexture in vFolder.vPixelTextureList) if (vTexture.vTexture == vTilesFound [ColorPickerIndex]) vTextureFound = vTexture; //if we found it, we make our default pixeltexture this one if (vTextureFound != null) { //check if we SELECT the tiles or we ONLY GET THE TILES TO REDRAW if (vAction == "SelectTile") { GameObject vGameObjectSel = null; //get the right texture to use! foreach (GameObject vCurObject in vGameobjectsFound) { if (vCurObject.GetComponent<SpriteRenderer>().sprite.texture == vTextureFound.vTexture) vGameObjectSel = vCurObject; } //try to see if it's the teleport gameobject we want to get if (vGameObjectSel == null) foreach (GameObject vCurObject in vGameobjectsFound) { //get the first teleport in this if (vCurObject.GetComponent<Tds_Tile>().vTileType == Tds_Tile.cTileType.Teleport) vGameObjectSel = vCurObject; } //check if we found it, so we select it to see the inspector! if (vGameObjectSel != null) { //get the pixel tile vSelectedTds_Tile = vGameObjectSel.GetComponent<Tds_Tile>(); //switch to the Selected Tile manually TabIndex = 2; //reset both list vTilesFound = new List<Texture2D> (); vGameobjectsFound = new List<GameObject>(); //refresh editor Repaint(); } } else { //switch to Create Tile SaveVariables ("CreateTile"); //get the new texture! grid.tilePrefab = vTextureFound; //try to get the foreach (GameObject vCurObject in vGameobjectsFound) { if (vCurObject.GetComponent<SpriteRenderer>().sprite.texture == vTextureFound.vTexture) { //get the pixel tile! Tds_Tile vCurTds_Tile = vCurObject.GetComponent<Tds_Tile>(); //now get the right info about this tile on the grid //grid.IsDoor = vCurTds_Tile.IsDoor; grid.OrderLayer = vCurTds_Tile.LayerOrder; grid.vTileType = vCurTds_Tile.vTileType; grid.ShowParticle = vCurTds_Tile.ShowParticles; } } //remove color picker vTilesFound = new List<Texture2D> (); } } } }, ""); //go back to normal skin GUI.skin = null; GUILayout.EndHorizontal (); } ////////////////Level Selector/////////////// Handles.BeginGUI (); //stretch with the number of buttons vNbrButton = 5; vBoxSize = 50; //go back to normal skin GUI.skin = null; //GUILayout.EndHorizontal (); Handles.EndGUI (); //////////////END of the Level Selector/////// ////////////////TILE EDITOR/////////////// Handles.BeginGUI (); //GUILayout.BeginHorizontal (); //stretch with the number of buttons vNbrButton = 7; vBoxSize = 50; //go back to normal skin GUI.skin = vNONESkin; //create a windows GUILayout.Window (2, new Rect (Screen.width - 65, Screen.height - (57 * vNbrButton), 70, vNbrButton * 85), (id) => { //check if we selected create tile on the GUI Texture2D vCreateTileToggle = vCreateTileText; if (vAction == "CreateTile") vCreateTileToggle = vCreateTileTextSel; //check if we have select tile selected Texture2D vSelectTileToggle = vSelectTileText; if (vAction == "SelectTile") vSelectTileToggle = vSelectTileTextSel; //check if we selected create tile on the GUI Texture2D vDeleteTileTextToggle = vDeleteTileText; if (vAction == "DeleteTile") vDeleteTileTextToggle = vDeleteTileTextSel; //check if we selected create tile on the GUI Texture2D vColorPickerToggle = vColorPickerText; if (vAction == "ColorPicker") { vColorPickerToggle = vColorPickerTextSel; } //get the right WallIcon Texture2D vWallIcon = vCanSeeWall; if (!IsLookingWall) vWallIcon = vCannotSeeWall; //by default it's 1x1 Texture2D vSelSizeText = v1x1; if (TileSize == 2) vSelSizeText = v2x2; else if (TileSize == 3) vSelSizeText = v3x3; //Tile Size if (GUILayout.Button (vSelSizeText, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { SaveVariables ("SetSize"); //remove color picker vTilesFound = new List<Texture2D> (); //show the size tile IsSizingTile = true; //refresh editor RefreshEditor(); } //Selection Tile if (GUILayout.Button (vSelectTileToggle, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { //remove old preview RefreshPreview(); //remove the current prefab to select a new one grid.tilePrefab = null; //switch action SaveVariables ("SelectTile"); //when selecting, we have a tile size of 1 TileSize = 1; //remove color picker vTilesFound = new List<Texture2D> (); //refresh editor RefreshEditor(); } //Create Tile if (GUILayout.Button (vCreateTileToggle, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { SaveVariables ("CreateTile"); //remove color picker vTilesFound = new List<Texture2D> (); //make sure a user doesn't waste his time being on a teleport if (grid.vTileType == Tds_Tile.cTileType.Teleport) grid.vTileType = Tds_Tile.cTileType.Ground; //refresh editor RefreshEditor(); } //Delete Tile if (GUILayout.Button (vDeleteTileTextToggle, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { SaveVariables ("DeleteTile"); //remove color picker vTilesFound = new List<Texture2D> (); //refresh editor RefreshEditor(); } //Color Picker if (GUILayout.Button (vColorPickerToggle, GUILayout.Width (vBoxSize), GUILayout.Height (vBoxSize))) { SaveVariables ("ColorPicker"); RefreshEditor(); } }, ""); //go back to normal skin GUI.skin = null; //GUILayout.EndHorizontal (); Handles.EndGUI (); //////////////END of the TILE EDITOR/////// int controlId = GUIUtility.GetControlID (FocusType.Passive); Event e = Event.current; Ray ray = Camera.current.ScreenPointToRay (new Vector3 (e.mousePosition.x, -e.mousePosition.y + Camera.current.pixelHeight)); Vector3 mousePos = ray.origin; Texture2D prefab = null; if (grid.tilePrefab != null) prefab = grid.tilePrefab.vTexture; //calculate where is the mouse cursor Vector3 aligned = new Vector3 (Mathf.Floor (mousePos.x / grid.width) * grid.width, Mathf.Floor (mousePos.y / grid.height) * grid.height, 0.0f); //check if the texture has been found before going further if (prefab) { //check if we have a prefab already bruilt if (TilePreview == null) { //initialize it before TilePreview = new List<GameObject> (); //create a preview tile by default TilePreview = CreateTile (1500, Tds_Tile.cTileType.Ground, aligned, true); //positionnate the tilepreview to be shown at the mouse location foreach (GameObject vObject in TilePreview) { Undo.IncrementCurrentGroup (); //vObject.transform.position = aligned; vObject.transform.parent = vAutoTilesObjects.transform; Undo.RegisterCreatedObjectUndo (vObject, "Create" + vObject.name); } } else if (aligned != lastaligned){ //only refresh preview if we change square lastaligned = aligned; RefreshPreview (); } } if (e.isMouse && e.button == 0 && (e.type == EventType.MouseDown || e.type == EventType.MouseDrag)) { if (vLastEventType == EventType.Ignore) { GUIUtility.hotControl = controlId; e.Use (); vLastEventType = e.type; } List<GameObject> gameObject = new List<GameObject> (); //check if the texture has been found before going further if (prefab && vAction == "CreateTile") { gameObject = CreateTile (grid.OrderLayer, grid.vTileType, aligned); //positionnate the tilepreview to be shown at the mouse location foreach (GameObject vObject in gameObject) { Undo.IncrementCurrentGroup (); //vObject.transform.position = aligned; vObject.transform.parent = vTilesObjects.transform; Undo.RegisterCreatedObjectUndo (vObject, "Create" + vObject.name); } } //destroy the same tiles! else if (vAction == "DeleteTile") { //delete the same tile that we got we found! List<GameObject> vFoundTile = GridHaveThisTile (prefab, aligned); //if we have found the same tiles, we can now delete it! if (vFoundTile != null) if (vFoundTile.Count > 0) foreach (GameObject vTileToDestroy in vFoundTile) GameObject.DestroyImmediate (vTileToDestroy); } }//allow right mouse else if (e.type == EventType.MouseUp && vLastEventType != null && e.button == 0) { GUIUtility.hotControl = 0; vLastEventType = EventType.Ignore; //use ignore EventType to skip it } }
//refresh the TileSet Path public void RefreshTileSet(string vpath) { IsRefreshing = true; //if we have found a real folder, we create a new fresh array for the new PNG files grid.tileSet.prefabs = new List<Tds_Folder>(); //check if the folder exist on the local computer if (Directory.Exists(vpath)) { grid.tileSet.TileSetPath = vpath; int cpt = 0; string[] allfiles = System.IO.Directory.GetFiles(vpath, "*.png", System.IO.SearchOption.AllDirectories); foreach (string vFilePath in allfiles) { //get the filepath without extension string vpath2 = vFilePath.Substring(vFilePath.IndexOf("Resources")+10); vpath2 = vpath2.Substring(0, vpath2.Length - 4); vpath2 = vpath2.Replace("\\","/"); //get to the right folder string[] vFolder = vpath2.Split('/'); //get the last one string vLastF = vFolder [vFolder.Length - 2]; string vFileName = vFolder [vFolder.Length - 1]; //try to get the master folder name string vMasterF = ""; if (vFolder.Length > 2) vMasterF = vFolder [vFolder.Length - 3]; //create the new Tds_Texture Tds_Texture vTexture = new Tds_Texture(); //load the texture! Texture2D vText = Resources.Load(vpath2,typeof(Texture2D)) as Texture2D; //store the filename vTexture.vFilename = Path.GetFileName(vFilePath); vTexture.vTexture = vText; //check if it exist! if (grid.tileSet.prefabs.Find (x => GetNiceName(x.vFolderName) == GetNiceName(vLastF) && GetNiceName(x.vMasterFolderName) == GetNiceName(vMasterF)) == null) { //create a new pixel folder Tds_Folder vNewFolder = new Tds_Folder (); vNewFolder.vFolderName = GetNiceName(vLastF); //keep the parent folder name vNewFolder.vMasterFolderName = GetNiceName(vMasterF); //keep the master folder name //check if there is a animated tile to add if (vFileName.Contains ("[")) { vTexture.vAnimationList .Add (vTexture); //add itself to the list for the first animated tile } //then add the pixeltexture to the list vNewFolder.vPixelTextureList.Add(vTexture); //add it grid.tileSet.prefabs.Add (vNewFolder); } else foreach (Tds_Folder vTds_Folder in grid.tileSet.prefabs) { if (GetNiceName(vTds_Folder.vFolderName) == GetNiceName(vLastF)) { //check if it's a animated tile or normal tile if (vFileName.Contains ("[")) { bool vFound = false; string vShortFileName = vFileName.Substring (0, vFileName.LastIndexOf ("[")); //remove the [#] //check if it's the first animated Texture foreach (Tds_Texture vAPixelT in vTds_Folder.vPixelTextureList) { if (!vFound) { //get the filename string[] vF = vAPixelT.vFilename.Split ('/'); string vFN = vF [vF.Length - 1]; //check if it's animated! if (vFN.Contains ("[")) { //remove the [#] vFN = vFN.Substring (0, vFN.LastIndexOf ("[")); //check if match! if (vShortFileName == vFN) { vFound = true; //add it for this one vAPixelT.vAnimationList.Add(vTexture); } } } } //if we still haven't found it, we just add the texture like a normal one, but add itself to the animation list so the next one will go in his list if (!vFound) { //add himself to the animation list before adding to the main list vTexture.vAnimationList.Add (vTexture); vTds_Folder.vPixelTextureList.Add (vTexture); } } else vTds_Folder.vPixelTextureList.Add (vTexture); //found the folder, then add the texture to it } } } } else grid.tileSet.TileSetPath = ""; IsRefreshing = false; }