bool IsPositionedHorizontallyInParents(GUILayoutCell cell) { if (cell != null) { GUILayouter topLayouter = cell.transform.parent.GetComponent <GUILayouter>(); if (topLayouter != null) { if (topLayouter.Type == GUILayouterType.Horizontal) { return(true); } else { return(IsPositionedHorizontallyInParents(GetParentCell(topLayouter))); } } } return(false); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); GUILayoutCell targetLayouterCell = (GUILayoutCell)target; GUILayout.Space(10); EditorGUILayout.Separator(); GUILayout.Space(10); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Create VerticalLayouter ", GUILayout.MinWidth(20))) { GameObject newLayouterObj = new GameObject("VerticalLayout"); GUILayouter newLayoter = newLayouterObj.AddComponent <GUILayouter> (); newLayoter.Type = GUILayouterType.Vertical; newLayouterObj.transform.parent = targetLayouterCell.CachedTransform; newLayouterObj.transform.localPosition = Vector3.zero; newLayouterObj.layer = targetLayouterCell.gameObject.layer; targetLayouterCell.LayoutHandlerObjects.Add(newLayouterObj); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Create HorizontalLayouter ", GUILayout.MinWidth(20))) { GameObject newLayouterObj = new GameObject("HorizontalLayout"); GUILayouter newLayoter = newLayouterObj.AddComponent <GUILayouter> (); newLayoter.Type = GUILayouterType.Horizontal; newLayouterObj.transform.parent = targetLayouterCell.CachedTransform; newLayouterObj.transform.localPosition = Vector3.zero; newLayouterObj.layer = targetLayouterCell.gameObject.layer; targetLayouterCell.LayoutHandlerObjects.Add(newLayouterObj); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Create TextMesh ", GUILayout.MinWidth(20))) { GameObject newLayouterObj = new GameObject("Label"); newLayouterObj.AddComponent <tk2dTextMesh> (); newLayouterObj.transform.parent = targetLayouterCell.CachedTransform; newLayouterObj.transform.localPosition = Vector3.zero; newLayouterObj.layer = targetLayouterCell.gameObject.layer; targetLayouterCell.LayoutHandlerObjects.Add(newLayouterObj); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Create Independent Sprite ", GUILayout.MinWidth(20))) { GameObject newLayouterObj = new GameObject("Sprite"); newLayouterObj.AddComponent <tk2dSprite> (); newLayouterObj.transform.parent = targetLayouterCell.CachedTransform; newLayouterObj.transform.localPosition = Vector3.zero; newLayouterObj.layer = targetLayouterCell.gameObject.layer; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Create Fill Sprite ", GUILayout.MinWidth(20))) { GameObject newLayouterObj = new GameObject("Sprite"); newLayouterObj.AddComponent <tk2dSprite> (); newLayouterObj.transform.parent = targetLayouterCell.CachedTransform; newLayouterObj.transform.localPosition = Vector3.zero; newLayouterObj.layer = targetLayouterCell.gameObject.layer; targetLayouterCell.LayoutHandlerObjects.Add(newLayouterObj); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Create SlicedSprite ", GUILayout.MinWidth(20))) { GameObject newLayouterObj = new GameObject("SlicedSprite"); newLayouterObj.AddComponent <tk2dSlicedSprite> (); newLayouterObj.transform.parent = targetLayouterCell.CachedTransform; newLayouterObj.transform.localPosition = Vector3.zero; newLayouterObj.layer = targetLayouterCell.gameObject.layer; targetLayouterCell.LayoutHandlerObjects.Add(newLayouterObj); } EditorGUILayout.EndHorizontal(); }
public void UpdateLayout() { if (cells == null || cells.Length == 0 || cells[0] == null) { return; } int occupiedFixedSize = 0; float flexibleAreasTotalWeight = 0; bool hasFlexibleAreas = false; for (int i = 0; i < cells.Length; i++) { GUILayoutCell cell = cells[i]; if (cell.Type == GUILayoutCellType.FixedSize) { occupiedFixedSize += (int)cell.SizeValue; } else if (cell.Type == GUILayoutCellType.RelativeFixedSize) { occupiedFixedSize += (int)(cell.SizeValue * (type == GUILayouterType.Horizontal ? occupiedPixels.GetValueOrDefault().width : occupiedPixels.GetValueOrDefault().height)); } else if (cell.Type == GUILayoutCellType.Flexible) { hasFlexibleAreas = true; flexibleAreasTotalWeight += cell.SizeValue; } } int availiblePixels = (int)(type == GUILayouterType.Horizontal ? occupiedPixels.GetValueOrDefault().width : occupiedPixels.GetValueOrDefault().height); int unoccupiedArea = availiblePixels - occupiedFixedSize; if (unoccupiedArea < 0) { // CustomDebug.LogWarning(gameObject.name + ": Can't fit " + occupiedFixedSize + " pixels in " + occupiedPixels + " pixels"); } if (unoccupiedArea > 0 && flexibleAreasTotalWeight < float.Epsilon && hasFlexibleAreas) { CustomDebug.LogError(gameObject.name + ": Weights for flexible cells is 0!"); } //setting cells sizes int currentPosition = isInversed ? availiblePixels : 0; for (int i = 0; i < cells.Length; i++) { GUILayoutCell cell = cells[i]; Rect cellRect = new Rect(); if (type == GUILayouterType.Horizontal) { if (cell.Type == GUILayoutCellType.FixedSize) { cellRect = new Rect(0, 0, cell.SizeValue, occupiedPixels.GetValueOrDefault().height); } else if (cell.Type == GUILayoutCellType.RelativeFixedSize) { cellRect = new Rect(0, 0, cell.SizeValue * (type == GUILayouterType.Horizontal ? ScreenDimentions.Width : ScreenDimentions.Height), occupiedPixels.GetValueOrDefault().height); } else if (cell.Type == GUILayoutCellType.Flexible) { cellRect = new Rect(0, 0, cell.SizeValue * (float)unoccupiedArea / (float)flexibleAreasTotalWeight, occupiedPixels.GetValueOrDefault().height); } if (isInversed) { cellRect.center = new Vector3(currentPosition - cellRect.width * 0.5f, 0, 0); currentPosition -= (int)cellRect.width; } else { cellRect.center = new Vector3(currentPosition + cellRect.width * 0.5f, 0, 0); currentPosition += (int)cellRect.width; } } else if (type == GUILayouterType.Vertical) { if (cell.Type == GUILayoutCellType.FixedSize) { cellRect = new Rect(0, cell.SizeValue * 0.5f, occupiedPixels.GetValueOrDefault().width, cell.SizeValue); } else if (cell.Type == GUILayoutCellType.RelativeFixedSize) { cellRect = new Rect(0, 0, occupiedPixels.GetValueOrDefault().width, cell.SizeValue * (type == GUILayouterType.Horizontal ? ScreenDimentions.Width : ScreenDimentions.Height)); } else if (cell.Type == GUILayoutCellType.Flexible) { cellRect = new Rect(0, 0, occupiedPixels.GetValueOrDefault().width, cell.SizeValue * (float)unoccupiedArea / (float)flexibleAreasTotalWeight); } if (isInversed) { cellRect.center = new Vector3(0, currentPosition - cellRect.height * 0.5f, 0); currentPosition -= (int)cellRect.height; } else { cellRect.center = new Vector3(0, currentPosition + cellRect.height * 0.5f, 0); currentPosition += (int)cellRect.height; } } cell.Reposition(type, cellRect); } }
public override void OnInspectorGUI() { base.OnInspectorGUI(); GUILayouter targetLayouter = (GUILayouter)target; GUILayout.Space(10); EditorGUILayout.Separator(); GUILayout.Space(10); CheckRootLayouter(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Create\nFlexible cell", GUILayout.MinWidth(20))) { GameObject newCellObj = new GameObject("__FlexSpace"); GUILayoutCell newCell = newCellObj.AddComponent <GUILayoutCell> (); newCell.Type = GUILayoutCellType.Flexible; newCell.SizeValue = 1; newCellObj.transform.parent = targetLayouter.CachedTransform; newCellObj.transform.localPosition = Vector3.zero; } if (GUILayout.Button("Create\nFixedSize cell", GUILayout.MinWidth(20))) { GameObject newCellObj = new GameObject("__FixedSpace"); GUILayoutCell newCell = newCellObj.AddComponent <GUILayoutCell> (); newCell.Type = GUILayoutCellType.FixedSize; newCell.SizeValue = 0; newCellObj.transform.parent = targetLayouter.CachedTransform; newCellObj.transform.localPosition = Vector3.zero; } if (GUILayout.Button("Create\nRelativeFixed cell", GUILayout.MinWidth(20))) { GameObject newCellObj = new GameObject("__RelativeFixedSpace"); GUILayoutCell newCell = newCellObj.AddComponent <GUILayoutCell> (); newCell.Type = GUILayoutCellType.RelativeFixedSize; newCell.SizeValue = 0; newCellObj.transform.parent = targetLayouter.CachedTransform; newCellObj.transform.localPosition = Vector3.zero; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Verify Hierarchy", GUILayout.MinWidth(20))) { bool isHierarchyCorrupted = false; GUILayoutCell[] allCells = targetLayouter.CachedTransform.GetComponentsInChildren <GUILayoutCell> (); foreach (var cell in allCells) { foreach (var handlerObj in cell.LayoutHandlerObjects) { if (handlerObj == null) { Debug.LogError("NULL Handler Found!"); Selection.activeGameObject = cell.gameObject; isHierarchyCorrupted = true; break; } } if (isHierarchyCorrupted) { break; } } if (!isHierarchyCorrupted) { Debug.Log("NO NULL Handlers Found!"); } } EditorGUILayout.EndHorizontal(); }