Skip to content

Commit

Permalink
✨ feat(usharp-prefab-upgrade-helper): Add filter
Browse files Browse the repository at this point in the history
  • Loading branch information
esnya committed Feb 7, 2023
1 parent 46cedde commit 00f9ba2
Showing 1 changed file with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ namespace InariUdon.EditorTools
{
public class USharpPrefabUpgradeHelper : EditorWindow
{
[Flags]
public enum Filter {
UpgradeRequired = 0x01,
Upgraded = 0x02,
SceneUpgraded = 0x04,
PrefabUpgraded = 0x08,
All = -1,
}
private class AssetEditingScope : IDisposable
{
public AssetEditingScope()
Expand All @@ -24,6 +32,7 @@ public void Dispose()
AssetDatabase.StopAssetEditing();
}
}

private static Enum GetBehaviourVersion(UdonBehaviour udon)
{
return (Enum)typeof(UdonSharpEditorUtility).GetMethod("GetBehaviourVersion", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, new[] { udon });
Expand All @@ -32,6 +41,10 @@ private static bool HasSceneBehaviourUpgradeFlag(UdonBehaviour udon)
{
return (bool)typeof(UdonSharpEditorUtility).GetMethod("HasSceneBehaviourUpgradeFlag", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, new[] { udon });
}
private static void SetSceneBehaviourUpgraded(UdonBehaviour udon)
{
typeof(UdonSharpEditorUtility).GetMethod("SetSceneBehaviourUpgraded", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, new[] { udon });
}

private static void UpgradePrefabs(IEnumerable<GameObject> prefabRootEnumerable)
{
Expand All @@ -53,6 +66,15 @@ private static bool IsUdonSharpUdonBehaviour(UdonBehaviour udon)
return udon.programSource is UdonSharpProgramAsset;
}

private bool MatchFilter(CacheItem item)
{
if ((filter & Filter.Upgraded) != 0 && item.version.Equals(2)) return true;
if ((filter & Filter.UpgradeRequired) != 0 && !item.version.Equals(2)) return true;
if ((filter & Filter.SceneUpgraded) != 0 && item.sceneUpgraded) return true;
if ((filter & Filter.PrefabUpgraded) != 0 && !item.sceneUpgraded) return true;
return false;
}

[MenuItem("InariUdon/U# Prefab Upgrade Helper")]
private static void ShowWindow()
{
Expand All @@ -72,11 +94,12 @@ private struct CacheItem

public Vector2 scrollPosition;
public bool includeChildren = true;

public Filter filter = Filter.All;
private CacheItem[] cache;

private void Scan()
{
AssetDatabase.Refresh();
cache = Selection.gameObjects
.SelectMany(o => includeChildren ? o.GetComponentsInChildren<UdonBehaviour>(true) : o.GetComponents<UdonBehaviour>())
.Where(IsUdonSharpUdonBehaviour)
Expand All @@ -93,7 +116,9 @@ private void Scan()
expanded = false,
};
})
.Where(MatchFilter)
.ToArray();
Repaint();
}

private void UpgradePrefabs()
Expand All @@ -118,6 +143,7 @@ private void ClearBehaviourVariables()
foreach (var udon in udons)
{
ClearBehaviourVariables(udon, false);
SetSceneBehaviourUpgraded(udon);
EditorUtility.SetDirty(udon);
}
}
Expand All @@ -141,6 +167,7 @@ private void OnGUI()
using (var check = new EditorGUI.ChangeCheckScope())
{
includeChildren = EditorGUILayout.Toggle("Include Children", includeChildren);
filter = (Filter)EditorGUILayout.EnumFlagsField("Filter", filter);
if (check.changed) Scan();
}

Expand Down

0 comments on commit 00f9ba2

Please sign in to comment.