private void EventHandler(SceneView sceneview) { if (m_myTarget == null) { m_myTarget = target as SnapToGrid; } //we should call this only when changing values, not always... but then again do it later. ToolsSupport.UnityHandlesHidden = LevelGrid.Ins.hideUnityHandles; if (!LevelGrid.Ins.hideUnityHandles && LevelGrid.Ins.autoRectTool) { Tools.current = Tool.Rect; } if (m_myTarget.useChildBoxCollider != null) { BoxCollider _boxCollider = m_myTarget.GetComponent <BoxCollider>(); _boxCollider.size = m_myTarget.useChildBoxCollider.bounds.size; _boxCollider.center = m_myTarget.useChildBoxCollider.bounds.center; } HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit[] hits = Physics.RaycastAll(worldRay, 1000); for (int i = 0; i < hits.Length; i++) { //Debug.Log("hit:" + hits[i].collider.gameObject.name); if (hits[i].transform.gameObject.layer == LayerMask.NameToLayer("Grid")) { gridPos = hits[i].point; } else if (hits[i].transform.GetComponent <SnapToGrid>() != null) { //Debug.Log("Object name: " + hits[i].collider.name); onMouseOverGameObject = hits[i].transform.gameObject; } } if (hits.Length <= 0) { onMouseOverGameObject = null; } //Debug.Log(onMouseOverGameObject); UpdateKeyEvents(); //mouse position in the grid //recalculate this with new values. float col = (float)gridPos.x / ((float)LevelGrid.Ins.gridSize * LevelGrid.Ins.scaleFactor); float row = (float)gridPos.z / ((float)LevelGrid.Ins.gridSize * LevelGrid.Ins.scaleFactor); LevelGrid.Ins.UpdateInputGridHeight(); if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { isMouseDown = true; if (onMouseOverGameObject == null) { return; } if (onMouseOverGameObject == m_myTarget.gameObject) { isThisObject = true; } else { isThisObject = false; Selection.activeGameObject = (onMouseOverGameObject != null) ? onMouseOverGameObject : null; } } if (isMouseDown && m_rotationKeyPressed) { if (LevelGrid.Ins.selectedGameObject == null) { LevelGrid.Ins.selectedGameObject = Selection.activeGameObject; } LevelGrid.Ins.selectedGameObject.transform.eulerAngles += new Vector3(0, 90f, 0); CalculateRotation(col, row); m_rotationKeyPressed = false; } //mouse click and dragandrop if (Event.current.type == EventType.MouseDrag && Event.current.button == 0) { if (Selection.activeGameObject == null) { return; } CalculateRotation(col, row); SnapToGrid(finalCol, finalRow, LevelGrid.Ins.height); objectDragged = true; } //if mouse released when control pressed, make a copy / otherwise, destroy old object. if (Event.current.type == EventType.MouseUp && Event.current.button == 0) { isMouseDown = false; if (LevelGrid.Ins.selectedGameObject) { //make copy if control is pressed if (!m_controlPressed) { Undo.IncrementCurrentGroup(); if (m_instantiated) { Undo.DestroyObjectImmediate(m_myTarget.gameObject); } } if (objectDragged) { Selection.activeGameObject = LevelGrid.Ins.selectedGameObject; objectDragged = false; } m_instantiated = false; } } //show hide grid if ((Event.current.type == EventType.keyUp) && (Event.current.keyCode == KeyCode.O)) { LevelGrid.Ins.showGrid = !LevelGrid.Ins.showGrid; } }
private void EventHandler(SceneView sceneview) { if (m_myTarget == null) { m_myTarget = target as SnapToGrid; } if (m_myTarget.childToApplyBoxCollider != null) { BoxCollider _boxCollider = m_myTarget.GetComponent <BoxCollider>(); _boxCollider.size = m_myTarget.childToApplyBoxCollider.bounds.size; _boxCollider.center = m_myTarget.childToApplyBoxCollider.bounds.center; } HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Native)); Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit[] hits = Physics.RaycastAll(worldRay, 1000); for (int i = 0; i < hits.Length; i++) { if (hits[i].transform.gameObject.layer == LayerMask.NameToLayer("Grid")) { gridPos = hits[i].point; } else if (hits[i].transform.GetComponent <SnapToGrid>() != null) { //Debug.Log("Object name: " + hits[i].collider.name); onMouseOverGameObject = hits[i].transform.gameObject; } } if (hits.Length <= 1) { onMouseOverGameObject = null; } //Debug.Log(onMouseOverGameObject); if ((Event.current.type == EventType.keyDown) && (Event.current.keyCode == KeyCode.A || Event.current.keyCode == KeyCode.S)) { m_aPressed = true; } if ((Event.current.type == EventType.keyUp) && (Event.current.keyCode == KeyCode.A || Event.current.keyCode == KeyCode.S)) { m_aPressed = false; } //check if control is pressed. if ((Event.current.type == EventType.keyDown) && (Event.current.keyCode == KeyCode.LeftControl || Event.current.keyCode == KeyCode.RightControl)) { m_controlPressed = true; } if ((Event.current.type == EventType.keyUp) && (Event.current.keyCode == KeyCode.LeftControl || Event.current.keyCode == KeyCode.RightControl)) { m_controlPressed = false; } //mouse position in the grid float col = (float)gridPos.x / ((float)LevelGrid.Ins.gridSize * LevelGrid.Ins.scaleFactor); float row = (float)gridPos.z / ((float)LevelGrid.Ins.gridSize * LevelGrid.Ins.scaleFactor); if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { if (onMouseOverGameObject == m_myTarget.gameObject) { isThisObject = true; } else { isThisObject = false; if (onMouseOverGameObject != null) { Selection.activeGameObject = onMouseOverGameObject; } else { Selection.activeGameObject = null; } } } //if (!isThisObject) // return; //mouse click and dragandrop if (Event.current.type == EventType.MouseDrag && Event.current.button == 0) { if (Selection.activeGameObject == null) { return; } if (m_aPressed) { LevelGrid.Ins.selectedGameObject.transform.eulerAngles += new Vector3(0, 90f, 0); m_aPressed = false; } SnapToGrid((int)col, (int)row, LevelGrid.Ins.height); } LevelGrid.Ins.UpdateInputGridHeight(); //if mouse released when control pressed, make a copy / otherwise, destroy old object. if (Event.current.type == EventType.MouseUp && Event.current.button == 0) { m_instantiated = false; if (LevelGrid.Ins.selectedGameObject) { //make copy if control is pressed if (!m_controlPressed) { Undo.IncrementCurrentGroup(); Undo.DestroyObjectImmediate(m_myTarget.gameObject); } Selection.activeGameObject = LevelGrid.Ins.selectedGameObject; } } }