|
| 1 | +using UnityEngine; |
| 2 | +using UnityEditor; |
| 3 | +using NUnit.Framework; |
| 4 | +using System.Collections; |
| 5 | +using FbxExporters.Editor; |
| 6 | + |
| 7 | +namespace FbxExporters.UnitTests |
| 8 | +{ |
| 9 | + public class AnimationComponentTestDataClass |
| 10 | + { |
| 11 | + static float[] m_keydata1 = new float [3 * 2] {1.0f, 0f, 2.0f, 100.0f, 3.0f, 0.0f}; |
| 12 | + |
| 13 | + public static IEnumerable TestCases { |
| 14 | + get { |
| 15 | + yield return new TestCaseData (m_keydata1, typeof(Transform), "m_LocalPosition.x").Returns (1); |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + [TestFixture] |
| 21 | + public class FbxAnimationTest : ExporterTestBase |
| 22 | + { |
| 23 | + protected static void DebugLogAnimCurve (AnimationCurve animCurve) |
| 24 | + { |
| 25 | + int idx = 0; |
| 26 | + foreach (var key in animCurve.keys) { |
| 27 | + Debug.Log (string.Format ("key[{0}] {1} {2}", idx++, key.time, key.value)); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + [Test, TestCaseSource (typeof (AnimationComponentTestDataClass), "TestCases")] |
| 32 | + public int SinglePropertyLegacyAnimTest (float [] keydata, System.Type propertyType, string propertyName ) |
| 33 | + { |
| 34 | + string filePath = GetRandomFbxFilePath (); |
| 35 | + GameObject go = new GameObject (); |
| 36 | + go.name = "orig_"+propertyName; |
| 37 | + Animation animOrig = go.AddComponent (typeof (Animation)) as Animation; |
| 38 | + |
| 39 | + int expectedNumKeys = keydata.Length / 2; |
| 40 | + |
| 41 | + // initialize keys |
| 42 | + Keyframe [] keys = new Keyframe [expectedNumKeys]; |
| 43 | + |
| 44 | + for (int idx = 0; idx < expectedNumKeys; idx++) |
| 45 | + { |
| 46 | + keys[idx].time = keydata [(idx*2)+0]; |
| 47 | + keys[idx].value = keydata [(idx*2)+1]; |
| 48 | + } |
| 49 | + |
| 50 | + AnimationCurve animCurveOrig = new AnimationCurve (keys); |
| 51 | + |
| 52 | + AnimationClip animClipOrig = new AnimationClip (); |
| 53 | + |
| 54 | + animClipOrig.legacy = true; |
| 55 | + |
| 56 | + animClipOrig.SetCurve ("", propertyType, propertyName, animCurveOrig); |
| 57 | + |
| 58 | + animOrig.AddClip (animClipOrig, "anim_" + propertyName ); |
| 59 | + |
| 60 | + //export the object |
| 61 | + var exportedFilePath = ModelExporter.ExportObject (filePath, go); |
| 62 | + Assert.AreEqual (exportedFilePath, filePath); |
| 63 | + |
| 64 | + //acquire imported object from exported file |
| 65 | + Object[] goAssetImported = AssetDatabase.LoadAllAssetsAtPath (filePath); |
| 66 | + Assert.IsNotNull (goAssetImported); |
| 67 | + |
| 68 | + // TODO : configure object so that it imports w Legacy Animation |
| 69 | + |
| 70 | + AnimationClip animClipImported = null; |
| 71 | + foreach (Object o in goAssetImported) |
| 72 | + { |
| 73 | + animClipImported = o as AnimationClip; |
| 74 | + if (animClipImported) break; |
| 75 | + } |
| 76 | + Assert.IsNotNull (animClipImported); |
| 77 | + |
| 78 | + // TODO : configure import settings so we don't need to force legacy |
| 79 | + animClipImported.legacy = true; |
| 80 | + Assert.AreEqual (animClipImported.legacy, true); |
| 81 | + |
| 82 | + { |
| 83 | + var go2 = Object.Instantiate (goAssetImported [0]) as GameObject; |
| 84 | + Assert.IsNotNull (go2); |
| 85 | + go2.name = "imported_" + propertyName; |
| 86 | + Animation anim2 = go2.AddComponent (typeof (Animation)) as Animation; |
| 87 | + anim2.AddClip (animClipImported, "anim2_" + propertyName ); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + // TODO : check clip properties match |
| 92 | + |
| 93 | + // check animCurve & keys |
| 94 | + int result = 0; |
| 95 | + |
| 96 | + foreach (EditorCurveBinding curveBinding in AnimationUtility.GetCurveBindings (animClipImported)) |
| 97 | + { |
| 98 | + AnimationCurve animCurveImported = AnimationUtility.GetEditorCurve (animClipImported, curveBinding); |
| 99 | + Assert.IsNotNull (animCurveImported); |
| 100 | + |
| 101 | + DebugLogAnimCurve (animCurveImported); |
| 102 | + |
| 103 | + Assert.AreEqual (expectedNumKeys, animCurveImported.length); |
| 104 | + |
| 105 | + //check imported animation against original |
| 106 | + int idx = 0; |
| 107 | + foreach (var key in animCurveImported.keys) |
| 108 | + { |
| 109 | + Assert.AreEqual (key.time, keydata [(idx * 2) + 0]); |
| 110 | + Assert.AreEqual (key.value, keydata [(idx * 2) + 1]); |
| 111 | + |
| 112 | + idx++; |
| 113 | + } |
| 114 | + |
| 115 | + result++; |
| 116 | + } |
| 117 | + |
| 118 | + return result; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments