void OnSceneGUI(SceneView sceneView) { int numberX, numberY; float lineSizeX, lineSizeY; Vector3 initialPos, refSize, selObjPos, selObjSize; EditorObject selectedObject; mousePosition = Event.current.mousePosition; if (sceneView != null) { mousePosition.y = sceneView.camera.pixelHeight - mousePosition.y; mousePosition = sceneView.camera.ScreenToWorldPoint(mousePosition); mousePosition.y = -mousePosition.y; //cameraSize = sceneView.camera.ScreenToWorldPoint(new Vector3(sceneView.camera.pixelRect.width, sceneView.camera.pixelRect.height)); mouseConvPos = Helper.ConvertVectorToGrid(mousePosition, gridX, gridY); mouseConvPos = new Vector3(mouseConvPos.x * gridX, -(mouseConvPos.y + 1) * gridY); mouseGridPos = Helper.ConvertVectorToGrid(mousePosition, gridX, gridY); //TODO: we have to adapt this to the size of the camera lineSizeX = 40; lineSizeY = 30; numberX = Mathf.RoundToInt(lineSizeX / gridX) % 2 == 0 ? Mathf.RoundToInt(lineSizeX / gridX) : Mathf.RoundToInt(lineSizeX / gridX) + 1; numberY = Mathf.RoundToInt(lineSizeY / gridY) % 2 == 0 ? Mathf.RoundToInt(lineSizeY / gridY) : Mathf.RoundToInt(lineSizeY / gridY) + 1; initialPos = -new Vector2(gridX * numberX, gridY * numberY) / 2 + new Vector2(Helper.ConvertVectorToGrid(sceneView.pivot, gridX, gridY).x *gridX, Helper.ConvertVectorToGrid(sceneView.pivot, gridX, gridY).y *gridY); //Grid if (showGrid) { //Vertical Lines for (int i = 0; i <= numberX; i++) { Handles.color = gridColor; Handles.DrawLine(initialPos + new Vector3(i * gridX, 0), initialPos + new Vector3(i * gridX, gridX * numberY)); } //Horizontal lines for (int j = 0; j <= numberY; j++) { Handles.color = gridColor; Handles.DrawLine(initialPos + new Vector3(0, j * gridY), initialPos + new Vector3(gridY * numberX, j * gridY)); } } //Draw Cursor Handles.color = cursorColor; Handles.DrawLine(mouseConvPos, mouseConvPos + new Vector3(gridX, 0)); //Top Handles.DrawLine(mouseConvPos + new Vector3(gridX, 0), mouseConvPos + new Vector3(gridX, gridY)); //Right Handles.DrawLine(mouseConvPos + new Vector3(gridX, gridY), mouseConvPos + new Vector3(0, gridY)); //Down Handles.DrawLine(mouseConvPos + new Vector3(0, gridY), mouseConvPos); //Left //Draw selected objects if (selectedObjects != null) { foreach (EditorObject obj in selectedObjects) { selObjPos = obj.reference.transform.position; selObjSize = obj.reference.GetComponent <SpriteRenderer>().bounds.size; Handles.color = selectColor; Handles.DrawLine(selObjPos + new Vector3(-selObjSize.x, selObjSize.y) / 2, selObjPos + new Vector3(-selObjSize.x, selObjSize.y) / 2 + new Vector3(selObjSize.x, 0)); //Top Handles.DrawLine(selObjPos + new Vector3(-selObjSize.x, selObjSize.y) / 2 + new Vector3(selObjSize.x, 0), selObjPos + new Vector3(-selObjSize.x, selObjSize.y) / 2 + new Vector3(selObjSize.x, -selObjSize.y)); //Right Handles.DrawLine(selObjPos + new Vector3(-selObjSize.x, selObjSize.y) / 2 + new Vector3(selObjSize.x, -selObjSize.y), selObjPos + new Vector3(-selObjSize.x, -selObjSize.y) / 2); //Down Handles.DrawLine(selObjPos + new Vector3(-selObjSize.x, -selObjSize.y) / 2, selObjPos + new Vector3(-selObjSize.x, selObjSize.y) / 2); //Left } } Event e = Event.current; int controlID = GUIUtility.GetControlID(FocusType.Passive); switch (e.GetTypeForControl(controlID)) { case EventType.MouseDown: if (e.button == 0) { GUIUtility.hotControl = controlID; if (cursorState == CursorState.Add) { AddObject(); } if (cursorState == CursorState.Remove) { RemoveObject(); } if (cursorState == CursorState.Drag) { DragObject(); } //Selected Object (priority to the selected layer) selectedObject = FindMouseOverObject(selectedLayer); if (selectedObject == null) { foreach (Layer l in layers) { selectedObject = FindMouseOverObject(l); if (selectedObject != null) { break; } } } //If it's only one element; if (!e.shift) { selectedObjects.Clear(); } if (selectedObject != null) { //Boundings if (!e.shift) { selectedObject.HighlightTileBoundings(); } if (selectedObjects.Contains(selectedObject)) { selectedObjects.Remove(selectedObject); } else { selectedObjects.Add(selectedObject); } } e.Use(); } break; case EventType.MouseUp: if (e.button == 0) { GUIUtility.hotControl = 0; if (cursorState == CursorState.Drag && dragObject != null) { dragObject = null; } e.Use(); } break; case EventType.MouseDrag: if (e.button == 0) { GUIUtility.hotControl = controlID; if (cursorState == CursorState.Add) { AddObject(); } if (cursorState == CursorState.Remove) { RemoveObject(); } if (cursorState == CursorState.Drag && dragObject != null) { dragObject.reference.transform.position = GetObjSnappedPosition(dragObject.reference); dragObject.i = (int)mouseGridPos.x; dragObject.j = (int)mouseGridPos.y; } e.Use(); } break; case EventType.KeyDown: //Shortcut for removing if (e.keyCode == KeyCode.R) { cursorState = CursorState.Remove; UnityEditorInternal.InternalEditorUtility.RepaintAllViews(); } //Shortcut for selecting if (e.keyCode == KeyCode.S) { cursorState = CursorState.Drag; UnityEditorInternal.InternalEditorUtility.RepaintAllViews(); } break; } Handles.EndGUI(); } }
/// <summary> /// Add a new element /// </summary> /// <param name="e">A game object (prefab)</param> public void AddSceneElement(EditorObject e) { sceneElements.Add(e); e.reference.transform.parent = container.gameObject.transform; }