Skip to content

Instantly share code, notes, and snippets.

@ScruffyRules
Created April 2, 2024 04:55
Show Gist options
  • Save ScruffyRules/7eb4b93dfa9d35a64ae35a9180654840 to your computer and use it in GitHub Desktop.
Save ScruffyRules/7eb4b93dfa9d35a64ae35a9180654840 to your computer and use it in GitHub Desktop.
/*
Copyright 2022 ScruffyRuffles
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if UNITY_EDITOR
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
using System.Linq;
public class FullAnimatorCleanup : Editor
{
[MenuItem("CONTEXT/AnimatorController/Animator Cleanup")]
public static void FAC(MenuCommand command)
{
var animatorController = command.context as AnimatorController;
var assetPath = AssetDatabase.GetAssetPath(animatorController);
var allassets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
List<Object> _subAssets = allassets.Where(x => x != animatorController && x != null).Distinct().ToList();
Debug.Log($"[FAC] SubAssets: {_subAssets.Count}");
HashSet<Object> usedAssets = new HashSet<Object>();
foreach (var layer in animatorController.layers)
{
if (layer.avatarMask != null) usedAssets.Add(layer.avatarMask);
RecursiveCollector(layer.stateMachine, usedAssets);
}
Debug.Log($"[FAC] Assets Used: {usedAssets.Count}");
int removed = 0;
AssetDatabase.StartAssetEditing();
foreach (var item in _subAssets)
{
if (!usedAssets.Contains(item))
{
DestroyImmediate(item, true);
removed++;
}
}
AssetDatabase.StopAssetEditing();
AssetDatabase.SaveAssets();
Debug.Log($"[FAC] Removed: {removed}");
}
public static void BlendTreeGetStates(BlendTree blendTree, HashSet<Object> assets)
{
assets.Add(blendTree);
foreach (var childMotion in blendTree.children)
{
if (childMotion.motion == null) continue;
assets.Add(childMotion.motion);
if (childMotion.motion.GetType() == typeof(BlendTree))
{
BlendTreeGetStates(childMotion.motion as BlendTree, assets);
}
}
}
public static void RecursiveCollector(AnimatorStateMachine stateMachine, HashSet<Object> assets)
{
assets.Add(stateMachine);
foreach (var transition in stateMachine.entryTransitions)
{
assets.Add(transition);
}
foreach (var transition in stateMachine.anyStateTransitions)
{
assets.Add(transition);
}
foreach (var childState in stateMachine.states)
{
assets.Add(childState.state);
assets.Add(childState.state.motion);
if (childState.state.motion != null)
{
if (childState.state.motion.GetType() == typeof(BlendTree))
{
BlendTreeGetStates(childState.state.motion as BlendTree, assets);
}
}
foreach (var transition in childState.state.transitions)
{
assets.Add(transition);
}
foreach (var stateBehaviour in childState.state.behaviours)
{
assets.Add(stateBehaviour);
}
}
foreach (var _stateMachine in stateMachine.stateMachines)
{
foreach (var transition in stateMachine.GetStateMachineTransitions(_stateMachine.stateMachine))
{
assets.Add(transition);
}
foreach (var transition in _stateMachine.stateMachine.GetStateMachineTransitions(stateMachine))
{
assets.Add(transition);
}
RecursiveCollector(_stateMachine.stateMachine, assets);
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment