Add: handling of child objects

This commit is contained in:
AnnulusGames 2024-02-16 17:12:59 +09:00
parent a482cb2afd
commit 855d0faa89
2 changed files with 34 additions and 7 deletions

View File

@ -4,6 +4,7 @@ using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine.SceneManagement;
using Alchemy.Hierarchy;
using UnityEngine;
namespace Alchemy.Editor
{
@ -14,23 +15,51 @@ namespace Alchemy.Editor
public void OnProcessScene(Scene scene, BuildReport report)
{
foreach (var obj in scene.GetRootGameObjects()
.Select(x => x.GetComponent<HierarchyObject>())
.SelectMany(x => x.GetComponentsInChildren<HierarchyObject>())
.Where(x => x != null))
{
switch (obj.HierarchyObjectMode)
{
case HierarchyObjectMode.None: break;
case HierarchyObjectMode.RemoveInPlayMode:
obj.OnObjectRemoved();
UnityEngine.Object.DestroyImmediate(obj.gameObject);
OnObjectRemoved(obj);
break;
case HierarchyObjectMode.RemoveInBuild:
if (EditorApplication.isPlaying) break;
obj.OnObjectRemoved();
UnityEngine.Object.DestroyImmediate(obj.gameObject);
OnObjectRemoved(obj);
break;
}
}
}
static void OnObjectRemoved(HierarchyObject obj)
{
Transform root;
while (true)
{
root = obj.transform.parent;
if (root == null) goto LOOP_END;
if (!root.TryGetComponent<HierarchyObject>(out var hierarchyObject)) goto LOOP_END;
switch (hierarchyObject.HierarchyObjectMode)
{
case HierarchyObjectMode.None:
goto LOOP_END;
case HierarchyObjectMode.RemoveInPlayMode:
break;
case HierarchyObjectMode.RemoveInBuild:
if (EditorApplication.isPlaying) goto LOOP_END;
break;
}
}
LOOP_END:
foreach (Transform child in obj.transform)
{
child.SetParent(root);
}
UnityEngine.Object.DestroyImmediate(obj.gameObject);
}
}
}

View File

@ -7,7 +7,5 @@ namespace Alchemy.Hierarchy
{
[SerializeField] HierarchyObjectMode hierarchyObjectMode = HierarchyObjectMode.RemoveInBuild;
public HierarchyObjectMode HierarchyObjectMode => hierarchyObjectMode;
public virtual void OnObjectRemoved() { }
}
}