/// <summary>Takes direct children and links them to the parent transform or global.</summary> /// <param name="strippingMode">Stripping mode to apply.</param> /// <param name="capitalizeFolderName"> /// Whether to capitalize the folder name when replacing it with a separator. /// Applies only if <paramref name="strippingMode"/> is <see cref="StrippingMode.ReplaceWithSeparator"/> /// </param> public void Flatten(StrippingMode strippingMode, bool capitalizeFolderName) { if (strippingMode == StrippingMode.DoNothing) { return; } MoveChildrenOut(strippingMode); HandleSelf(strippingMode, capitalizeFolderName); }
private void MoveChildrenOut(StrippingMode strippingMode) { int index = this.transform.GetSiblingIndex(); // keep components in logical order foreach (var child in GetComponentsInChildren <Transform>(includeInactive: true)) { // gather only first-level children if (child.parent != this.transform) { continue; } if (strippingMode == StrippingMode.PrependWithFolderName) { child.name = $"{this.name}/{child.name}"; } child.SetParent(this.transform.parent, true); child.SetSiblingIndex(++index); } }
private void HandleSelf(StrippingMode strippingMode, bool capitalizeFolderName) { if (strippingMode == StrippingMode.ReplaceWithSeparator) { // If the folder name is already a separator, don't change it. if (!name.StartsWith("--- ")) { name = $"--- {(capitalizeFolderName ? name.ToUpper() : name)} ---"; } return; } if (Application.isPlaying) { Destroy(this.gameObject); } else { DestroyImmediate(this.gameObject); } }
private static void StripFoldersFromPrefab(string prefabPath, StrippingMode strippingMode) { using (var temp = new EditPrefabContentsScope(prefabPath)) { var folders = temp.PrefabContentsRoot.GetComponentsInChildren <Folder>(); foreach (Folder folder in folders) { if (folder.gameObject == temp.PrefabContentsRoot) { Debug.LogWarning( $"Hierarchy will not flatten for {prefabPath} because its root is a folder. " + "It's advised to make the root an empty game object."); Object.DestroyImmediate(folder); } else { folder.Flatten(strippingMode, StripSettings.CapitalizeName); } } } }