private void OnEnable() { instance = this; if (selectedEditor != null) { DestroyImmediate(selectedEditor); } selectedEditor = null; Input.imeCompositionMode = IMECompositionMode.On; //autoRepaintOnSceneChange = true; }
private void OnGUI() { toolbarSelected = GUILayout.Toolbar(toolbarSelected, toolbarTitle, Prefs.toolbarStyle); if (toolbarSelected == 0) { //物体 if (PrefabInformation.Count == 0) { //大小为空 GUILayout.Label("……", Prefs.mapAssetBackgroundStyle, GUILayout.ExpandHeight(true)); } else { //selection list scollVector = GUILayout.BeginScrollView(scollVector); { List <string> assetNameList = new List <string>(); for (int i = 0; i < PrefabInformation.Count; i++) { assetNameList.Add(PrefabInformation.Keys[i].name); } EditorGUI.BeginChangeCheck(); prefabSelected = GUILayout.SelectionGrid(prefabSelected, assetNameList.ToArray(), 1, Prefs.mapAssetBackgroundStyle, GUILayout.Height(27 * PrefabInformation.Count)); if (EditorGUI.EndChangeCheck()) { MapInspector.FocusOn(CurrentPrefab, MapInspector.SelectionType.Prefab); } GUILayout.Label("", Prefs.mapAssetBackgroundStyle, GUILayout.ExpandHeight(true)); } GUILayout.EndScrollView(); //function GameObject cp = CurrentPrefab; GUILayout.BeginHorizontal(Prefs.mapAssetBackgroundStyle, GUILayout.Height(28)); { GUILayout.FlexibleSpace(); if (GUILayout.Button("删除", GUILayout.Width(60))) { DeletePrefab(cp); prefabSelected--; MapInspector.FocusOn(CurrentPrefab, MapInspector.SelectionType.Prefab); } } GUILayout.EndHorizontal(); //drag the prefab out switch (Event.current.type) { case EventType.MouseDown: tempPrefabInstance = PrefabUtility.InstantiatePrefab(CurrentPrefab) as GameObject; Debug.Log(tempPrefabInstance); break; case EventType.MouseDrag: if (tempPrefabInstance == null) { break; } Vector3 prefabPos = Vector3.zero; if (mouseOverWindow == null) { validPos = false; } else if (mouseOverWindow.GetType() == typeof(MapManager)) { MapManager mapManager = mouseOverWindow as MapManager; Vector2 guiPos = Event.current.mousePosition + position.position - mapManager.position.position; prefabPos = mapManager.GetElementWorldPos(guiPos, 0); validPos = true; } else if (mouseOverWindow.GetType() == typeof(SceneView)) { SceneView sv = SceneView.lastActiveSceneView; Handles.SetCamera(SceneView.lastActiveSceneView.camera); Vector2 guiPos = Event.current.mousePosition + new Vector2(position.xMin - sv.position.xMin, position.yMax - sv.position.yMax); Ray ray = HandleUtility.GUIPointToWorldRay(guiPos); float yRate = ray.origin.y / ray.direction.y; prefabPos = new Vector3(ray.origin.x - ray.direction.x * yRate, 0, ray.origin.z - ray.direction.z * yRate); validPos = true; } else { validPos = false; } tempPrefabInstance.transform.position = prefabPos; break; case EventType.MouseUp: DestroyImmediate(tempPrefabInstance); break; case EventType.Ignore: if (validPos) { MapManager.SetMapObject(tempPrefabInstance); } else { DestroyImmediate(tempPrefabInstance); } break; } } } else { //地图组预设 if (MapGroupAssets.Count == 0) { //大小为空 GUILayout.Label("……", Prefs.mapAssetBackgroundStyle, GUILayout.ExpandHeight(true)); } else { //selection list scollVector = GUILayout.BeginScrollView(scollVector); { List <string> assetNameList = new List <string>(); for (int i = 0; i < MapGroupAssets.Count; i++) { assetNameList.Add(MapGroupAssets[i].groupName + (MapGroupAssets[i].baked ? "" : Prefs.mapAssetUnbakedStateMark)); } EditorGUI.BeginChangeCheck(); mapAssetSelected = GUILayout.SelectionGrid(mapAssetSelected, assetNameList.ToArray(), 1, Prefs.mapAssetBackgroundStyle, GUILayout.Height(27 * MapGroupAssets.Count)); if (EditorGUI.EndChangeCheck()) { MapInspector.FocusOn(CurrentAsset, MapInspector.SelectionType.GroupAsset); } GUILayout.Label("", Prefs.mapAssetBackgroundStyle, GUILayout.ExpandHeight(true)); } GUILayout.EndScrollView(); //function MapGroupAsset mga = CurrentAsset; GUILayout.BeginHorizontal(Prefs.mapAssetBackgroundStyle, GUILayout.Height(28)); { GUILayout.FlexibleSpace(); if (!mga.baked && GUILayout.Button("烘焙", GUILayout.Width(60))) { MapGroupAssetEditor.BakeGroupAsset(mga); } if (GUILayout.Button("删除", GUILayout.Width(60))) { MapGroupAssetEditor.DeleteGroupAsset(mga); mapAssetSelected = 0; MapInspector.FocusOn(CurrentAsset, MapInspector.SelectionType.GroupAsset); } } GUILayout.EndHorizontal(); } } }
private void CheckSelection() { if (!Active) { return; } selectionChecked = true; //Debug.Log("Selection changed."); //检查选择 if (Selection.activeGameObject == null) { return; } if (Selection.activeGameObject.tag == "MapSystem") { if (Selection.activeGameObject.transform.childCount == 0) { Selection.activeGameObject = null; return; } else { while (counter >= Selection.activeGameObject.transform.childCount) { counter -= Selection.activeGameObject.transform.childCount; } Selection.activeGameObject = Selection.activeGameObject.transform.GetChild(counter).gameObject; counter++; selectionChecked = false; return; } } if (PrefabUtility.GetPrefabType(Selection.activeGameObject) == PrefabType.Prefab) { MapInspector.FocusOn(Selection.activeGameObject, MapInspector.SelectionType.Prefab); return; } //修正选择 Transform parent = Selection.activeGameObject.transform.parent; if (parent != null) { if (parent.tag != "MapSystem") { Selection.activeGameObject = parent.gameObject; selectionChecked = false; return; } else { MapInspector.FocusOn(Selection.activeGameObject, MapInspector.SelectionType.GameObject); } } else { SetMapObject(Selection.activeGameObject); MapInspector.FocusOn(Selection.activeGameObject, MapInspector.SelectionType.GameObject); } }
private void OnGUI() { //激活检测 if (!Active) { GUILayout.Label("地图没打开,地图编辑器未激活!"); if (GUILayout.Button("打开地图")) { EditorSceneManager.OpenScene("Assets/Scenes/In Game.unity"); } return; } if (!selectionChecked) { CheckSelection(); } //场景参数计算 radius = Mathf.Min(EditorGUIUtility.currentViewWidth - Prefs.sideWidth, position.height) / 2 - Prefs.edge; center = new Vector2(radius + Prefs.edge + Prefs.sideWidth, radius + Prefs.edge); Vector3 cameraDir = SceneView.lastActiveSceneView == null ? Vector3.back : SceneView.lastActiveSceneView.camera.transform.forward; Vector2 start = Vector3.right; cameraRotation = Quaternion.LookRotation(Vector3.forward, new Vector3(cameraDir.x, cameraDir.z, 0)); Vector2 mid = Quaternion.Euler(0, 0, -MapSystem.AnglePerGroup / 2) * Vector2.right * radius / 2; //事件交互 switch (Event.current.type) { case EventType.Layout: case EventType.Repaint: { //画底部圆盘 for (int i = 0; i < 3; i++) { float angle = MapSystem.GetAngle(new Vector3(mid.x, 0, -mid.y)); int index = (int)(angle / MapSystem.AnglePerGroup); Color color; if (GroupList[index] == null) { color = Color.gray; } else { color = Prefs.circleGradient.Evaluate(angle / MapSystem.MaxAngle); } float alpha = 1 - Prefs.arcAlphaCurve.Evaluate(Mathf.Abs(MapSystem.SubSigned(MapSystem.currentAngle, angle))); color.a = Mathf.Lerp(0, 1, alpha); Handles.color = color; Handles.DrawSolidArc((Vector3)center + cameraRotation * mid * Mathf.Lerp(Prefs.arcCenterOffsetRateMin, Prefs.arcCenterOffsetRateMax, alpha), Vector3.back, cameraRotation * start, 120, (radius + Mathf.Lerp(-10, 25, (viewScale.value - Prefs.minScale) / (Prefs.maxScale - Prefs.minScale))) * Mathf.Lerp(Prefs.arcMinRadiusRate, 1, alpha)); if (GroupList[index] == null) { Handles.Label((Vector3)center + cameraRotation * mid * Mathf.Lerp(1, Prefs.groupNumDistance, alpha), index.ToString(), Prefs.groupNumStyle); } else { Handles.Label((Vector3)center + cameraRotation * mid * Mathf.Lerp(1, Prefs.groupNumDistance, alpha), index.ToString() + " " + GroupList[index].groupName + (GroupList[index].dirty ? "*" : ""), Prefs.groupNumStyle); } start = rot * start; mid = rot * mid; } Handles.color = Color.white; //中心圆盘 Handles.color = Prefs.centerDiscColor; { Handles.DrawSolidDisc(center, Vector3.back, Prefs.centerDiscRadius); Handles.Label(center - Prefs.centerDiscStyle.CalcSize(new GUIContent(MapSystem.CurrentCircle.ToString())) / 2f, MapSystem.CurrentCircle.ToString(), Prefs.centerDiscStyle); } Handles.color = Color.white; //画侧面刻度 Handles.color = Prefs.sideColor; { Vector2 yRange = new Vector2(Prefs.edge + (1 - Prefs.sideLineLengthRate) * radius, Prefs.edge + (1 + Prefs.sideLineLengthRate) * radius); Vector2 xRange = new Vector2(Prefs.sideWidth - Prefs.sideXOffset + (Prefs.sideFlip ? -Prefs.sideMarkWidth : Prefs.sideMarkWidth), Prefs.sideWidth - Prefs.sideXOffset); Handles.DrawAAPolyLine( Prefs.sideLineWidth, new Vector2(xRange.x, yRange.x), new Vector2(xRange.y, yRange.x), new Vector2(xRange.y, yRange.y), new Vector2(xRange.x, yRange.y) ); xRange.x = Prefs.sideWidth - Prefs.sideXOffset + (Prefs.sideFlip ? -Prefs.sideMarkWidth * Prefs.sideLineLengthRate : Prefs.sideMarkWidth * Prefs.sideLineLengthRate); for (int i = 1; i < Prefs.sideMarkNum; i++) { float yPos = Mathf.Lerp(yRange.x, yRange.y, (float)i / (float)Prefs.sideMarkNum); Handles.DrawLine(new Vector2(xRange.x, yPos), new Vector2(xRange.y, yPos)); } float arrowXOffset = (Prefs.sideFlip ? -1 : 1) * Prefs.sideArrowSize * Mathf.Cos(Prefs.sideArrowAngle * Mathf.PI / 180f); float arrowyPos = Mathf.Lerp(yRange.y, yRange.x, (viewScale.value - Prefs.minScale) / (Prefs.maxScale - Prefs.minScale)); Handles.ArrowHandleCap(0, new Vector2(xRange.x + arrowXOffset, arrowyPos), Quaternion.Euler(0, (Prefs.sideFlip ? 90 : -90) + Prefs.sideArrowAngle, 0), Prefs.sideArrowSize, EventType.Repaint); Handles.Label(new Vector2(xRange.x + arrowXOffset / 2, arrowyPos), "缩放等级", Prefs.sideMarkStyle); } Handles.color = Color.white; //画双击指示 if (doubleClickMarkAlpha.value > 0) { Handles.color = new Color(1, 1, 1, doubleClickMarkAlpha.value); Handles.DrawSolidDisc(cameraRotation * doubleClickMarkRawPos + (Vector3)center, Vector3.back, (1 - doubleClickMarkAlpha.value) * Prefs.doubleClickMarkRadius); Handles.color = Color.white; } //画元素 Handles.color = Prefs.elementOutLineColor; { foreach (GameObject g in Selection.gameObjects) { PrefabType prefabType = PrefabUtility.GetPrefabType(g); if (prefabType != PrefabType.Prefab) { Handles.DrawSolidDisc(GetElementGUIPos(g.transform.position), Vector3.back, Prefs.elementRadius + Prefs.elementOutlineWidth); } } foreach (GameObject g in invalidObjectList) { Handles.color = GetElementColor(g); Handles.DrawSolidDisc(GetElementGUIPos(g.transform.position), Vector3.back, Prefs.elementRadius); } MapGroup group = GroupList[MapSystem.currentGroupIndex]; DrawElement(group, 1); group = GroupList[MapSystem.GetPrevious(MapSystem.currentGroupIndex)]; DrawElement(group, Prefs.elementAlpha); group = GroupList[MapSystem.GetNext(MapSystem.currentGroupIndex)]; DrawElement(group, Prefs.elementAlpha); } Handles.color = Color.white; //控制 MapSystem.SetCurrentAngle(QuaternionToAngle(targetQuaternion.value.eulerAngles.y)); //视图旋转 if (mouseDragAngle.value != 0) { Quaternion deltaRot = Quaternion.Euler(0, mouseDragAngle.value, 0); SceneView.lastActiveSceneView.pivot = deltaRot * SceneView.lastActiveSceneView.pivot; SceneView.lastActiveSceneView.rotation = deltaRot * SceneView.lastActiveSceneView.rotation; } } break; case EventType.MouseDown: SetMouseArea(); switch (mouseArea) { case MouseArea.GroupArea: //双击 if (Event.current.clickCount > 1) { SceneView.lastActiveSceneView.LookAt(GetElementWorldPos(Event.current.mousePosition, 0)); SetDoubleClickMark(); } //选中组 Selection.activeGameObject = null; MapInspector.FocusOn(GroupList[areaIndex], MapInspector.SelectionType.Group); targetQuaternion.target = Quaternion.Euler(0, areaIndex * MapSystem.AnglePerGroup + MapSystem.AnglePerGroup / 2, 0); break; case MouseArea.ObjectArea: //双击 if (Event.current.clickCount > 1) { SceneView.lastActiveSceneView.LookAt(Selection.activeGameObject.transform.position); SetDoubleClickMark(); } //选中物体 GameObject objectSelected = areaIndex >= 0 ? GroupList[MapSystem.currentGroupIndex].transform.GetChild(areaIndex).gameObject : invalidObjectList[-areaIndex - 1]; Selection.activeGameObject = objectSelected; break; case MouseArea.Invalid: case MouseArea.DragArea: case MouseArea.Center: //选中map Selection.activeGameObject = null; MapInspector.FocusOn(null, MapInspector.SelectionType.Map); break; } break; case EventType.MouseDrag: if (Event.current.button == 1) { //right click SceneView.lastActiveSceneView.rotation = Quaternion.Euler(0, Event.current.delta.x / 2, 0) * SceneView.lastActiveSceneView.rotation * Quaternion.Euler(Event.current.delta.y / 2, 0, 0); } else { switch (mouseArea) { case MouseArea.ScollArea: case MouseArea.GroupArea: case MouseArea.DragArea: Vector3 mouseDir = Event.current.mousePosition - center; float mouseDirSqrLength = mouseDir.sqrMagnitude; float dragAngle = -Vector3.Cross(mouseDir, Event.current.delta).z / mouseDirSqrLength * 180 / Mathf.PI * Prefs.dragSensitivity; mouseDragAngle.value = dragAngle; mouseDragAngle.target = 0; break; case MouseArea.ObjectArea: Undo.RecordObject(Selection.activeGameObject.transform, "Moving Object"); Selection.activeGameObject.transform.position = GetElementWorldPos(Event.current.mousePosition, Selection.activeGameObject.transform.position.y); break; } } break; case EventType.MouseUp: switch (mouseArea) { case MouseArea.ObjectArea: SetMapObject(Selection.activeGameObject); break; } break; case EventType.ScrollWheel: viewScale.target = Mathf.Clamp(viewScale.target - Event.current.delta.y * Prefs.scaleSensitivity, Prefs.minScale, Prefs.maxScale); break; } //Debug Message ShowDebugMessage(); }