109 lines
3.0 KiB
C#
109 lines
3.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITKit.GameEditor
|
|
{
|
|
//This is a helper can make scene uncategory assets to be categorized
|
|
public sealed class SceneBasedEnvironmentCategoryWindow : EditorWindow
|
|
{
|
|
private const string EnvironmentCategory = nameof(EnvironmentCategory);
|
|
[MenuItem("Tools/Scenes/Category Window")]
|
|
public static void Open()
|
|
{
|
|
var window = GetWindow<SceneBasedEnvironmentCategoryWindow>();
|
|
window.Show();
|
|
}
|
|
|
|
private Label reportLabel;
|
|
private IGrouping<string,GameObject>[] group;
|
|
private Button categoryButton;
|
|
private void OnEnable()
|
|
{
|
|
Rebuild();
|
|
|
|
}
|
|
|
|
private void Rebuild()
|
|
{
|
|
var button = rootVisualElement.Create<Button>();
|
|
categoryButton = rootVisualElement.Create<Button>();
|
|
var scroll = rootVisualElement.Create<ScrollView>();
|
|
|
|
reportLabel = scroll.Create<Label>();
|
|
reportLabel.text = "0";
|
|
|
|
button.text = "Search";
|
|
button.clicked += Execute;
|
|
|
|
categoryButton.SetEnabled(false);
|
|
categoryButton.text = "Category";
|
|
categoryButton.clicked += Category;
|
|
|
|
scroll.style.paddingLeft = 8;
|
|
scroll.style.paddingRight = 8;
|
|
scroll.style.paddingTop= 8;
|
|
scroll.style.paddingBottom = 8;
|
|
}
|
|
|
|
private void Execute()
|
|
{
|
|
var stringBuilder = new System.Text.StringBuilder();
|
|
//Get All Root GameObject in editor
|
|
|
|
var allRootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
|
|
|
|
var currentSelection = UnityEditor.Selection.activeTransform;
|
|
|
|
group = allRootGameObjects
|
|
.Where(x => PrefabUtility.GetPrefabAssetType(x) is not PrefabAssetType.NotAPrefab)
|
|
.Where(x =>
|
|
{
|
|
if (currentSelection is null || currentSelection.root is not null) return true;
|
|
return x.transform.GetSiblingIndex()>currentSelection.GetSiblingIndex();
|
|
})
|
|
.GroupBy(x => Path.GetFileName(AssetDatabase.GetAssetPath(PrefabUtility.GetCorrespondingObjectFromSource(x))))
|
|
.ToArray();
|
|
|
|
if (currentSelection is not null)
|
|
{
|
|
stringBuilder.AppendLine($"Current Selection: {currentSelection.name},Only Show After This");
|
|
}
|
|
|
|
stringBuilder.AppendLine($"All: {allRootGameObjects.Length}");
|
|
stringBuilder.AppendLine($"Is Prefab: {group.SelectMany(x=>x).Count()}");
|
|
|
|
|
|
foreach (var VARIABLE in group)
|
|
{
|
|
stringBuilder.AppendLine($"{VARIABLE.Key} : {VARIABLE.Count()}");
|
|
}
|
|
|
|
reportLabel.text = stringBuilder.ToString();
|
|
|
|
categoryButton.SetEnabled(group.Length > 0);
|
|
}
|
|
|
|
private void Category()
|
|
{
|
|
var AutoCategory = GameObject.Find(nameof(EnvironmentCategory))??new GameObject(nameof(EnvironmentCategory));
|
|
foreach (var x in group)
|
|
{
|
|
var category = GameObject.Find(x.Key)??new GameObject(x.Key);
|
|
category.transform.SetParent(AutoCategory.transform);
|
|
foreach (var VARIABLE in x)
|
|
{
|
|
VARIABLE.transform.SetParent(category.transform);
|
|
}
|
|
}
|
|
EditorUtility.SetDirty(AutoCategory);
|
|
}
|
|
}
|
|
}
|