void OnSceneGUI(SceneView sceneView) { //If click on the scene view and is left mouse button clicked if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { //make a ray from the scene view camera Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hit; //if find a element a tile can be placed on if (Physics.Raycast(ray, out hit, 1000, 1 << LayerMask.NameToLayer("Walkable"))) { //Create a element storing the selected face PathTileElement element = new PathTileElement(hit.transform.gameObject, hit.normal); //If the face is already selected, deselect it if (ContainsElement(element)) { RemoveElement(element); } else { //else, add it to the selection selectedFace.Add(element); } } else { ResetSelection(); } //Use the event so the editor does not take it as usual Event.current.Use(); } DisplaySelection(); }
//Remove the selected face from the scene void RemoveFace(PathTileElement element) { if (DoesFaceExists(element.tile, element.normal)) { //Delete the object corresponding to the selected face Transform child = GetFaceByOrientation(element.tile, element.normal); DestroyImmediate(child.gameObject); } }
//Return if the current element is in the selected faces or not bool ContainsElement(PathTileElement elt) { foreach (PathTileElement element in selectedFace) { if (element.normal == elt.normal && element.tile == elt.tile) { return(true); } } return(false); }
//Remove the selected element void RemoveElement(PathTileElement element) { for (int i = 0; i < selectedFace.Count; i++) { if (ContainsElement(selectedFace[i])) { selectedFace.RemoveAt(i); return; } } }