/// <summary> /// Add a new Location object and script to the scene. Append the location /// </summary> /// <param name="landscape"></param> /// <param name="lbGroupLocation"></param> /// <param name="showErrors"></param> /// <returns></returns> public static LBGroupLocationItem AddNewLocationToScene(LBLandscape landscape, LBGroup lbGroupLocation, Vector2 mousePosition, Camera svCamera, bool showErrors) { LBGroupLocationItem lbGroupLocationItem = null; string methodName = "LBGroupLocationItem.AddNewLocationToScene"; // Basic validation if (landscape == null) { if (showErrors) { Debug.LogWarning("ERROR: " + methodName + " landscape is null"); } } else if (lbGroupLocation == null) { if (showErrors) { Debug.LogWarning("ERROR: " + methodName + " lbGroupLocation is null"); } } else { Vector3 locationPoint = Vector3.zero; locationPoint = LBEditorHelper.GetLandscapePositionFromMouse(landscape, mousePosition, false, true); if (locationPoint.x != 0 && locationPoint.z != 0) { //Debug.Log("[DEBUG] locationPoint " + locationPoint + " mousePos: " + mousePosition); Material locationMaterial = null; locationMaterial = LBEditorHelper.GetMaterialFromAssets(LBSetup.materialsFolder, "LBLocation.mat"); lbGroupLocationItem = CreateLocationItemInScene(landscape, lbGroupLocation, locationMaterial, locationPoint, 0f, lbGroupLocation.positionList.Count + 1, showErrors); if (lbGroupLocationItem != null) { lbGroupLocationItem.lbGroup.positionList.Add(locationPoint - landscape.start); if (lbGroupLocationItem.lbGroup.isFixedRotation) { lbGroupLocationItem.lbGroup.rotationYList.Add(0f); } LBEditorHelper.RepaintLBW(); } } } return(lbGroupLocationItem); }
void Update() { if (EditorApplication.isPlayingOrWillChangePlaymode) { return; } if (Selection.activeTransform != null) { // Has the user selected this prefabinstance or a child of the prefab? // NOTE: This could be a little slow if user selects the "wrong" object in hierarchy if (Selection.activeTransform.IsChildOf(this.transform)) { // If user selected a child of the prefab, select the root of the prefab. if (Selection.activeTransform != this.transform) { Selection.activeTransform = this.transform; } // Was this previously unselected in the last frame? if (!isSelected) { isSelected = true; // Auto shrink all other members if (lbGroupDesigner != null && lbGroupMember != null) { if (lbGroupDesigner.lbGroup != null) { lbGroupDesigner.lbGroup.GroupMemberListExpand(false); lbGroupMember.showInEditor = true; LBEditorHelper.RepaintLBW(); } } } } else { isSelected = false; } } }
private void OnSceneGUI() { if (EditorApplication.isPlayingOrWillChangePlaymode) { return; } // Don't process scene requests for Group Designer items when the ObjPathDesigner is enabled else if (lbGroupDesignerItem.lbGroupDesigner != null && lbGroupDesignerItem.lbGroupDesigner.isObjDesignerEnabled) { lbGroupDesignerItem.transform.position = prevPosition; lbGroupDesignerItem.transform.rotation = lbGroupDesignerItem.rotation; lbGroupDesignerItem.transform.localScale = lbGroupDesignerItem.scale; Selection.activeGameObject = null; return; } Event current = Event.current; //Debug.Log("DesignerItemEditor " + lbGroupDesignerItem.name); if (current != null) { // Get the group member. If we can't, and this isn't a SubGroup item, get out. if (lbGroupMember == null) { lbGroupMember = lbGroupDesignerItem.lbGroupMember; if (lbGroupMember == null && !lbGroupDesignerItem.isSubGroup) { return; } } bool isInSubGroup = !string.IsNullOrEmpty(lbGroupDesignerItem.SubGroupGUID); if (lbGroupDesignerItem.isObjPathMember || isInSubGroup) { // If user attempts to move/rotate or scale an ObjPath member, SubGroup member, or a SubGroup, reset it lbGroupDesignerItem.transform.position = prevPosition; lbGroupDesignerItem.transform.rotation = lbGroupDesignerItem.rotation; lbGroupDesignerItem.transform.localScale = lbGroupDesignerItem.scale; } else { #region Check if Scale is overridden // If Override is off, scaling is at the group level, not the member level, so don't allow scaling. if (!lbGroupMember.isGroupOverride && Tools.current == Tool.Scale) { Tools.current = Tool.None; } #if UNITY_2017_3_OR_NEWER else if (Tools.current == Tool.Scale || Tools.current == Tool.Transform) #else else if (Tools.current == Tool.Scale) #endif { // If using Transform tool in U2017.3+, may need to reset scaling back to pre-scaled value. if (!lbGroupMember.isGroupOverride) { lbGroupDesignerItem.transform.localScale = lbGroupDesignerItem.scale; } else { // Equally scale all axis float maxScale = 0f; Vector3 localScale = lbGroupDesignerItem.transform.localScale; // Get the max scale amount of any of the axis if (Mathf.Abs(localScale.x) > maxScale) { maxScale = localScale.x; } if (Mathf.Abs(localScale.y) > maxScale) { maxScale = localScale.y; } if (Mathf.Abs(localScale.z) > maxScale) { maxScale = localScale.z; } // Make each axis the same localScale = maxScale * Vector3.one; // Clamp scaling to between 0.1 and 10 if (localScale.x < 0.1f) { localScale = Vector3.one * 0.1f; } if (localScale.x > 10f) { localScale = Vector3.one * 10f; } lbGroupDesignerItem.transform.localScale = localScale; } } #endregion #region Lock Y rotation if randomise is enabled #if UNITY_2017_3_OR_NEWER if ((Tools.current == Tool.Rotate || Tools.current == Tool.Transform) && lbGroupMember.randomiseRotationY) #else if (Tools.current == Tool.Rotate && lbGroupMember.randomiseRotationY) #endif { // Pivotmode of center can cause issues with some prefabs that aren't centred correctly. // Prevent x,z movement of prefab when only y rotation change is attempted if (Tools.pivotMode == PivotMode.Center) { Tools.pivotMode = PivotMode.Pivot; } lbGroupDesignerItem.transform.eulerAngles = new Vector3(lbGroupDesignerItem.transform.eulerAngles.x, lbGroupDesignerItem.rotation.eulerAngles.y, lbGroupDesignerItem.transform.eulerAngles.z); } #endregion #region Clamp MinOffsetX,Z #if UNITY_2017_3_OR_NEWER if (Tools.current == Tool.Move || Tools.current == Tool.Transform) #else if (Tools.current == Tool.Move) #endif { if (lbGroupMember.isPlacedInCentre) { if (lbGroup != null && lbGroupDesignerItem.lbGroupDesigner != null) { // If the user attempts to move it outside the clearing radius, lock the position to the last known position Vector3 newPos = lbGroupDesignerItem.transform.position; float distanceToCentre = Vector2.Distance(lbGroupDesignerItem.lbGroupDesigner.grpBasePlaneCentre2D, new Vector2(newPos.x, newPos.z)); if (distanceToCentre > lbGroup.maxClearingRadius) { lbGroupDesignerItem.transform.position = prevPosition; } } } else if (!lbGroupMember.randomiseOffsetY) { // Don't allow movement on x,z axis for items that aren't offset from the Centre of the clearing AND don't use randomiseOffsetY. lbGroupDesignerItem.transform.position = new Vector3(lbGroupDesignerItem.position.x, lbGroupDesignerItem.transform.position.y, lbGroupDesignerItem.position.z); } else { // Don't allow movement on any axis for items that aren't offset from the Centre of the clearing. lbGroupDesignerItem.transform.position = lbGroupDesignerItem.position; } // Update last known position prevPosition = lbGroupDesignerItem.transform.position; } #endregion } bool isLeftButton = (current.button == 0); bool isRightButton = (current.button == 1); // ISSUE (ignore if vertex snapping is not enabled [v key held down]) current.keyCode != KeyCode.V // Record the starting positions if (!lbGroupDesignerItem.isObjPathMember && current.type == EventType.MouseDown && isLeftButton) { Tools.hidden = false; //Debug.Log("Left Btn Down"); lbGroupDesignerItem.position = lbGroupDesignerItem.transform.position; lbGroupDesignerItem.rotation = lbGroupDesignerItem.transform.rotation; lbGroupDesignerItem.scale = lbGroupDesignerItem.transform.localScale; } else if (!lbGroupDesignerItem.isObjPathMember && !isInSubGroup && current.type == EventType.MouseUp && lbGroupMember != null && isLeftButton) { #region Move #if UNITY_2017_3_OR_NEWER if (Tools.current == Tool.Move || Tools.current == Tool.Transform) #else if (Tools.current == Tool.Move) #endif { if (lbGroupMember.isPlacedInCentre) { lbGroupMember.minOffsetX = lbGroupDesignerItem.transform.position.x; lbGroupMember.minOffsetZ = lbGroupDesignerItem.transform.position.z; if (!lbGroupMember.randomiseOffsetY) { lbGroupMember.minOffsetY = lbGroupDesignerItem.transform.position.y - lbGroupDesignerItem.lbGroupDesigner.BasePlaneOffsetY; } } else if (!lbGroupMember.randomiseOffsetY) { lbGroupMember.minOffsetY = lbGroupDesignerItem.transform.position.y - lbGroupDesignerItem.lbGroupDesigner.BasePlaneOffsetY; lbGroupMember.maxOffsetY = lbGroupMember.minOffsetY; // Update all the instances of this member in the Designer if (lbGroupDesignerItem.lbGroupDesigner != null) { lbGroupDesignerItem.lbGroupDesigner.UpdateGroupMember(lbGroupMember); } } } #endregion #region Rotate #if UNITY_2017_3_OR_NEWER if (Tools.current == Tool.Rotate || Tools.current == Tool.Transform) #else if (Tools.current == Tool.Rotate) #endif { Vector3 newRotation = lbGroupDesignerItem.transform.rotation.eulerAngles; lbGroupMember.rotationX = newRotation.x; lbGroupMember.rotationZ = newRotation.z; if (!lbGroupMember.randomiseRotationY) { lbGroupMember.startRotationY = newRotation.y; lbGroupMember.endRotationY = newRotation.y; if (!lbGroupMember.isPlacedInCentre && lbGroupDesignerItem.lbGroupDesigner != null) { // Update all the instances of this member in the Designer lbGroupDesignerItem.lbGroupDesigner.UpdateGroupMember(lbGroupMember); } } } #endregion #region Scale #if UNITY_2017_3_OR_NEWER if ((Tools.current == Tool.Scale || Tools.current == Tool.Transform) && lbGroupMember.isGroupOverride) #else if (Tools.current == Tool.Scale && lbGroupMember.isGroupOverride) #endif { //Debug.Log("Scale start:" + lbGroupDesignerItem.scale + " mouseup:" + lbGroupDesignerItem.transform.localScale); lbGroupMember.minScale = lbGroupDesignerItem.transform.localScale.x; lbGroupMember.maxScale = lbGroupMember.minScale; if (!lbGroupMember.isPlacedInCentre && lbGroupDesignerItem.lbGroupDesigner != null) { // Update all the instances of this member in the Designer lbGroupDesignerItem.lbGroupDesigner.UpdateGroupMember(lbGroupMember); } } #endregion // Update the LB Editor Windows LBEditorHelper.RepaintEditorWindow(typeof(LandscapeBuilderWindow)); //Debug.Log("Prefab: " + this.name + " start pos:" + lbGroupDesignerItem.position + " end pos:" + lbGroupDesignerItem.transform.position); } //if (current.keyCode == KeyCode.V && current.type == EventType.KeyUp) //if (current.keyCode != KeyCode.V) //{ // //LBIntegration.ReflectionOutputFields(typeof(Tools), true, true); // bool isVertexDragging = false; // try // { // isVertexDragging = LBIntegration.ReflectionGetValue<bool>(typeof(Tools), "vertexDragging", null, true, true); // } // catch (System.Exception ex) // { // Debug.LogWarning("LBGroupDesignerItemEditor could not find VertexDragging - PLEASE REPORT " + ex.Message); // } // Debug.Log("Vertex Snapping enabled..." + Time.realtimeSinceStartup + " vertexDragging: " + isVertexDragging); //} #region Display the Context-sensitive menu else if (current.type == EventType.MouseDown && isRightButton) { bool isCheckProximity = (lbGroupDesignerItem.lbGroupDesigner == null ? true : lbGroupDesignerItem.lbGroupDesigner.isCheckProximity); GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("Close Designer"), false, CloseGroupDesigner); menu.AddItem(new GUIContent("Refresh Designer"), false, () => RefreshDesigner(true)); menu.AddItem(new GUIContent("Check Proximity"), isCheckProximity, CheckProximity, !isCheckProximity); menu.AddItem(new GUIContent("Auto Refresh"), lbGroupDesignerItem.lbGroupDesigner.GetAutoRefresh, () => { lbGroupDesignerItem.lbGroupDesigner.SetAutoRefresh(!lbGroupDesignerItem.lbGroupDesigner.GetAutoRefresh); }); // The following context menu items only apply to GroupMembers. // Also exclude members in a subgroup within the Clearing Group if (!lbGroupDesignerItem.isSubGroup && !isInSubGroup) { menu.AddSeparator(""); if (lbGroupDesignerItem.lbGroupDesigner.showZones) { menu.AddItem(new GUIContent("Add/"), false, () => { }); menu.AddItem(new GUIContent("Add/Zone under Object"), false, AddZoneToObject); } menu.AddItem(new GUIContent("Reset/"), false, () => { }); menu.AddItem(new GUIContent("Reset/Reset Rotation"), false, ResetRotation); menu.AddItem(new GUIContent("Reset/Reset Position"), false, ResetPosition); if (lbGroupMember.isGroupOverride) { menu.AddItem(new GUIContent("Reset/Reset Scale"), false, ResetScale); } menu.AddItem(new GUIContent("Snap/"), false, () => { }); menu.AddItem(new GUIContent("Snap/Pivot to Ground"), false, () => SnapToGround(false)); menu.AddItem(new GUIContent("Snap/Model to Ground"), false, () => SnapToGround(true)); if (!lbGroupDesignerItem.isObjPathMember) { menu.AddItem(new GUIContent("Place In Centre +offset"), lbGroupMember.isPlacedInCentre, TogglePlaceInCentre); } menu.AddItem(new GUIContent("Override Group"), lbGroupMember.isGroupOverride, ToggleOverrideGroupDefaults); menu.AddItem(new GUIContent("Rotation/Face 2 Group Centre"), lbGroupMember.rotationType == LBGroupMember.LBRotationType.Face2GroupCentre, SetRotationType, LBGroupMember.LBRotationType.Face2GroupCentre); menu.AddItem(new GUIContent("Rotation/Face 2 Zone Centre"), lbGroupMember.rotationType == LBGroupMember.LBRotationType.Face2ZoneCentre, SetRotationType, LBGroupMember.LBRotationType.Face2ZoneCentre); menu.AddItem(new GUIContent("Rotation/Group Space"), lbGroupMember.rotationType == LBGroupMember.LBRotationType.GroupSpace, SetRotationType, "GroupSpace"); menu.AddItem(new GUIContent("Rotation/World Space"), lbGroupMember.rotationType == LBGroupMember.LBRotationType.WorldSpace, SetRotationType, "WorldSpace"); menu.AddItem(new GUIContent("Rotation/"), false, () => { }); menu.AddItem(new GUIContent("Rotation/Randomise Y"), lbGroupMember.randomiseRotationY, () => { lbGroupMember.randomiseRotationY = !lbGroupMember.randomiseRotationY; lbGroupMember.showtabInEditor = (int)LBGroupMember.LBMemberEditorTab.XYZ; LBEditorHelper.RepaintEditorWindow(typeof(LandscapeBuilderWindow)); lbGroupDesignerItem.lbGroupDesigner.UpdateGroupMember(lbGroupMember); }); } menu.AddSeparator(""); if (lbGroupDesignerItem.isObjPathMember && !isInSubGroup) { menu.AddItem(new GUIContent("Show Object Path"), false, () => { if (!string.IsNullOrEmpty(lbGroupDesignerItem.objPathGroupMemberGUID) && lbGroupDesignerItem.lbGroupDesigner.lbGroup != null) { LBGroupMember objPathGroupMember = lbGroupDesignerItem.lbGroupDesigner.lbGroup.GetMemberByGUID(lbGroupDesignerItem.objPathGroupMemberGUID, false); if (objPathGroupMember != null) { lbGroupDesignerItem.lbGroupDesigner.lbGroup.GroupMemberListExpand(false); lbGroupDesignerItem.lbGroupDesigner.lbGroup.showGroupMembersInEditor = true; objPathGroupMember.showInEditor = true; LBEditorHelper.RepaintLBW(); } ; } ; }); } menu.AddItem(new GUIContent("Zoom Out"), false, () => { lbGroupDesignerItem.lbGroupDesigner.ZoomExtent(SceneView.lastActiveSceneView); }); menu.AddItem(new GUIContent("Zoom In"), false, () => { lbGroupDesignerItem.lbGroupDesigner.ZoomIn(SceneView.lastActiveSceneView); }); menu.AddItem(new GUIContent("Display/Group Extent"), lbGroupDesignerItem.lbGroupDesigner.showGroupExtent, () => { lbGroupDesignerItem.lbGroupDesigner.showGroupExtent = !lbGroupDesignerItem.lbGroupDesigner.showGroupExtent; }); menu.AddItem(new GUIContent("Display/SubGroup Extents"), lbGroupDesignerItem.lbGroupDesigner.showSubGroupExtent, () => { lbGroupDesignerItem.lbGroupDesigner.showSubGroupExtent = !lbGroupDesignerItem.lbGroupDesigner.showSubGroupExtent; }); menu.AddItem(new GUIContent("Display/Member Extent Proximity"), lbGroupDesignerItem.lbGroupDesigner.showProximity, () => { lbGroupDesignerItem.lbGroupDesigner.showProximity = !lbGroupDesignerItem.lbGroupDesigner.showProximity; if (lbGroupDesignerItem.lbGroupDesigner.showProximity) { lbGroupMember.showtabInEditor = (int)LBGroupMember.LBMemberEditorTab.Proximity; } LBEditorHelper.RepaintEditorWindow(typeof(LandscapeBuilderWindow)); }); menu.AddItem(new GUIContent("Display/Member Tree Proximity"), lbGroupDesignerItem.lbGroupDesigner.showTreeProximity, () => { lbGroupDesignerItem.lbGroupDesigner.showTreeProximity = !lbGroupDesignerItem.lbGroupDesigner.showTreeProximity; if (lbGroupDesignerItem.lbGroupDesigner.showTreeProximity) { lbGroupMember.showtabInEditor = (int)LBGroupMember.LBMemberEditorTab.Proximity; } LBEditorHelper.RepaintEditorWindow(typeof(LandscapeBuilderWindow)); }); menu.AddItem(new GUIContent("Display/Member Flatten Area"), lbGroupDesignerItem.lbGroupDesigner.showFlattenArea, () => { lbGroupDesignerItem.lbGroupDesigner.showFlattenArea = !lbGroupDesignerItem.lbGroupDesigner.showFlattenArea; if (lbGroupDesignerItem.lbGroupDesigner.showFlattenArea) { lbGroupMember.showtabInEditor = (int)LBGroupMember.LBMemberEditorTab.General; } LBEditorHelper.RepaintEditorWindow(typeof(LandscapeBuilderWindow)); }); menu.AddItem(new GUIContent("Display/Zones"), lbGroupDesignerItem.lbGroupDesigner.showZones, () => { lbGroupDesignerItem.lbGroupDesigner.showZones = !lbGroupDesignerItem.lbGroupDesigner.showZones; if (lbGroupDesignerItem.lbGroupDesigner.showZones) { lbGroupMember.showtabInEditor = (int)LBGroupMember.LBMemberEditorTab.Zone; } LBEditorHelper.RepaintEditorWindow(typeof(LandscapeBuilderWindow)); }); // Cannot directly delete items: // 1. in an Object Path // 2. in a subgroup within the current Group // 3. a whole subgroup within the current Group if (!lbGroupDesignerItem.isObjPathMember && !isInSubGroup && !lbGroupDesignerItem.isSubGroup) { menu.AddSeparator(""); menu.AddItem(new GUIContent("Delete"), false, DeleteMember); } menu.AddSeparator(""); menu.AddItem(new GUIContent("Unselect"), false, () => { Selection.activeObject = null; }); // The Cancel option is not really necessary as use can just click anywhere else. However, it may help some users. menu.AddItem(new GUIContent("Cancel"), false, () => { }); menu.ShowAsContext(); current.Use(); } #endregion } }