using System.Collections.Generic; using System.Linq; using nadena.dev.modular_avatar.core.editor; using NUnit.Framework; using UnityEditor; using UnityEditor.Animations; using UnityEngine; using VRC.SDK3.Avatars.Components; namespace modular_avatar_tests { public class TestBase { private List objects; private const string MinimalAvatarGuid = "60d3416d1f6af4a47bf9056aefc38333"; [SetUp] public virtual void Setup() { objects = new List(); } [TearDown] public virtual void Teardown() { foreach (var obj in objects) { Object.DestroyImmediate(obj); } Util.DeleteTemporaryAssets(); } protected GameObject CreateRoot(string name) { var path = AssetDatabase.GUIDToAssetPath(MinimalAvatarGuid); var go = GameObject.Instantiate(AssetDatabase.LoadAssetAtPath(path)); objects.Add(go); return go; } protected GameObject CreateChild(GameObject parent, string name) { var go = new GameObject(name); go.transform.parent = parent.transform; objects.Add(go); return go; } protected GameObject CreatePrefab(string relPath) { var prefab = LoadAsset(relPath); var go = Object.Instantiate(prefab); objects.Add(go); return go; } protected T LoadAsset(string relPath) where T : UnityEngine.Object { var root = "Assets/_ModularAvatar/EditModeTests/" + GetType().Name + "/"; var path = root + relPath; var obj = AssetDatabase.LoadAssetAtPath(path); Assert.NotNull(obj, "Missing test asset {0}", path); return obj; } protected static AnimationClip findFxClip(GameObject prefab, string layerName) { var motion = findFxMotion(prefab, layerName) as AnimationClip; Assert.NotNull(motion); return motion; } protected static Motion findFxMotion(GameObject prefab, string layerName) { var layer = findFxLayer(prefab, layerName); var state = layer.stateMachine.states[0].state; Assert.NotNull(state); return state.motion; } protected static AnimatorState FindStateInLayer(AnimatorControllerLayer layer, string stateName) { foreach (var state in layer.stateMachine.states) { if (state.state.name == stateName) return state.state; } return null; } protected static AnimatorControllerLayer findFxLayer(GameObject prefab, string layerName) { var fx = prefab.GetComponent().baseAnimationLayers .FirstOrDefault(l => l.type == VRCAvatarDescriptor.AnimLayerType.FX); Assert.NotNull(fx); var ac = fx.animatorController as AnimatorController; Assert.NotNull(ac); Assert.False(fx.isDefault); var layer = ac.layers.FirstOrDefault(l => l.name == layerName); Assert.NotNull(layer); return layer; } } }