|
| 1 | +using FbxExporters.Editor; |
| 2 | +using FbxExporters.EditorTools; |
| 3 | +using System; |
| 4 | +using System.Linq; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Reflection; |
| 7 | +using Unity.FbxSdk; |
| 8 | +using UnityEngine; |
| 9 | +using UnityEngine.Timeline; |
| 10 | + |
| 11 | +namespace FbxExporters.UnitTests |
| 12 | +{ |
| 13 | + public static class ModelExporterReflection |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + // This class allows accessing the ModelExporter methods that this |
| 17 | + // unit test needs to validate |
| 18 | + /// </summary> |
| 19 | + |
| 20 | + ///////////// |
| 21 | + // Properties |
| 22 | + ///////////// |
| 23 | + public static Material DefaultMaterial |
| 24 | + { |
| 25 | + get |
| 26 | + { |
| 27 | + return (Material)GetStaticProperty("DefaultMaterial"); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + ///////////// |
| 32 | + // Fields |
| 33 | + ///////////// |
| 34 | + public static float UnitScaleFactor |
| 35 | + { |
| 36 | + get |
| 37 | + { |
| 38 | + return (float)GetStaticField("UnitScaleFactor"); |
| 39 | + } |
| 40 | + } |
| 41 | + public static Dictionary<System.Type, KeyValuePair<System.Type,ModelExporter.FbxNodeRelationType>> MapsToFbxObject |
| 42 | + { |
| 43 | + get |
| 44 | + { |
| 45 | + return (Dictionary<System.Type, KeyValuePair<System.Type,ModelExporter.FbxNodeRelationType>>) GetStaticField("MapsToFbxObject"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + ///////////// |
| 50 | + // Methods |
| 51 | + ///////////// |
| 52 | + public static GameObject GetGameObject(UnityEngine.Object obj) |
| 53 | + { |
| 54 | + return (GameObject) InvokeMethod("GetGameObject", new object[] { obj }); |
| 55 | + } |
| 56 | + |
| 57 | + public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine.Object> unityExportSet) |
| 58 | + { |
| 59 | + return (HashSet<GameObject>) InvokeMethod("RemoveRedundantObjects", new object[] {unityExportSet}); |
| 60 | + } |
| 61 | + |
| 62 | + public static string GetVersionFromReadme() |
| 63 | + { |
| 64 | + return (string)InvokeMethod("GetVersionFromReadme", null); |
| 65 | + } |
| 66 | + |
| 67 | + public static string ConvertToValidFilename(string filename) |
| 68 | + { |
| 69 | + return (string)InvokeMethod("ConvertToValidFilename", new object[] {filename}); |
| 70 | + } |
| 71 | + |
| 72 | + public static Vector3 FindCenter(IEnumerable<GameObject> gameObjects) |
| 73 | + { |
| 74 | + return (Vector3)InvokeMethod("FindCenter", new object[] {gameObjects}); |
| 75 | + } |
| 76 | + |
| 77 | + public static Vector3 GetRecenteredTranslation(Transform t, Vector3 center) |
| 78 | + { |
| 79 | + return (Vector3)InvokeMethod("GetRecenteredTranslation", new object[] {t, center}); |
| 80 | + } |
| 81 | + |
| 82 | + public static FbxLayer GetOrCreateLayer(FbxMesh fbxMesh, int layer = 0 /* default layer */) |
| 83 | + { |
| 84 | + return (FbxLayer)InvokeMethod("GetOrCreateLayer", new object[] {fbxMesh,layer}); |
| 85 | + } |
| 86 | + |
| 87 | + public static FbxVector4 ConvertToRightHanded(Vector3 leftHandedVector, float unitScale = 1f) |
| 88 | + { |
| 89 | + return (FbxVector4)InvokeMethod("ConvertToRightHanded", new object[] {leftHandedVector,unitScale}); |
| 90 | + } |
| 91 | + |
| 92 | + public static bool ExportMaterial(ModelExporter instance, Material unityMaterial, FbxScene fbxScene, FbxNode fbxNode) |
| 93 | + { |
| 94 | + return (bool)InvokeMethod("ExportMaterial", new object[] {unityMaterial,fbxScene,fbxNode},instance); |
| 95 | + } |
| 96 | + |
| 97 | + // Redefinition of the internal delegate. There might be a way to re-use the one in ModelExporter |
| 98 | + public static void RegisterMeshObjectCallback(ModelExporter.GetMeshForObject callback) |
| 99 | + { |
| 100 | + InvokeMethod("RegisterMeshObjectCallback", new object[] {callback}); |
| 101 | + } |
| 102 | + |
| 103 | + public static void RegisterMeshCallback<T>(ModelExporter.GetMeshForComponent<T> callback, bool replace = false) |
| 104 | + where T : UnityEngine.MonoBehaviour |
| 105 | + { |
| 106 | + // Get the template method first (we assume there is only one |
| 107 | + // templated RegisterMeshCallback method in ModelExporter |
| 108 | + var methods = from methodInfo in typeof(ModelExporter).GetMethods(BindingFlags.Static | BindingFlags.NonPublic) |
| 109 | + where methodInfo.Name == "RegisterMeshCallback" |
| 110 | + && methodInfo.IsGenericMethodDefinition |
| 111 | + select methodInfo; |
| 112 | + |
| 113 | + MethodInfo templateMethod = methods.Single(); |
| 114 | + MethodInfo genericMethod = templateMethod.MakeGenericMethod(new Type[]{typeof(T)}); |
| 115 | + genericMethod.Invoke(null, new object[]{callback, replace}); |
| 116 | + } |
| 117 | + |
| 118 | + public static void UnRegisterMeshCallback<T>() |
| 119 | + { |
| 120 | + // Get the template method first (we assume there is only one |
| 121 | + // templated RegisterMeshCallback method in ModelExporter |
| 122 | + var methods = from methodInfo in typeof(ModelExporter).GetMethods(BindingFlags.Static | BindingFlags.NonPublic) |
| 123 | + where methodInfo.Name == "UnRegisterMeshCallback" |
| 124 | + && methodInfo.IsGenericMethodDefinition |
| 125 | + select methodInfo; |
| 126 | + |
| 127 | + MethodInfo templateMethod = methods.Single(); |
| 128 | + MethodInfo genericMethod = templateMethod.MakeGenericMethod(new Type[]{typeof(T)}); |
| 129 | + genericMethod.Invoke(null, null); |
| 130 | + } |
| 131 | + |
| 132 | + public static void UnRegisterMeshCallback(ModelExporter.GetMeshForObject callback) |
| 133 | + { |
| 134 | + InvokeMethodOverload("UnRegisterMeshCallback", |
| 135 | + new object[] {callback}, |
| 136 | + new Type[] {typeof(ModelExporter.GetMeshForObject)}); |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + public static bool ExportTexture(ModelExporter instance, Material unityMaterial, string unityPropName, |
| 141 | + FbxSurfaceMaterial fbxMaterial, string fbxPropName) |
| 142 | + { |
| 143 | + return (bool)InvokeMethod("ExportTexture", |
| 144 | + new object[] {unityMaterial,unityPropName,fbxMaterial,fbxPropName}, |
| 145 | + instance); |
| 146 | + } |
| 147 | + |
| 148 | + public static FbxDouble3 ConvertQuaternionToXYZEuler(Quaternion q) |
| 149 | + { |
| 150 | + return (FbxDouble3)InvokeMethodOverload("ConvertQuaternionToXYZEuler", |
| 151 | + new object[] {q}, |
| 152 | + new Type[] {typeof(Quaternion)}); |
| 153 | + } |
| 154 | + |
| 155 | + public static bool ExportMesh(ModelExporter instance, Mesh mesh, FbxNode fbxNode, Material[] materials = null) |
| 156 | + { |
| 157 | + return (bool)InvokeMethodOverload("ExportMesh", |
| 158 | + new object[] {mesh, fbxNode, materials}, |
| 159 | + new Type[] {typeof(Mesh), typeof(FbxNode), typeof(Material [])}, |
| 160 | + instance); |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + public static void ExportSingleTimelineClip(TimelineClip timelineClipSelected, GameObject animationTrackGObject, string filePath = null) |
| 165 | + { |
| 166 | + InvokeMethod("ExportSingleTimelineClip", |
| 167 | + new object[] {timelineClipSelected, animationTrackGObject, filePath}); |
| 168 | + } |
| 169 | + |
| 170 | + public static void ExportAllTimelineClips(GameObject objectWithPlayableDirector, string folderPath, IExportOptions exportOptions = null) |
| 171 | + { |
| 172 | + InvokeMethod("ExportAllTimelineClips", |
| 173 | + new object[] {objectWithPlayableDirector, folderPath, exportOptions}); |
| 174 | + } |
| 175 | + |
| 176 | + /////////// Helpers /////////// |
| 177 | + private static object InvokeMethod(string methodName, object[] argsToPass, ModelExporter instance = null) |
| 178 | + { |
| 179 | + // Use reflection to call the internal ModelExporter.GetGameObject static method |
| 180 | + var internalMethod = typeof(ModelExporter).GetMethod(methodName, |
| 181 | + (instance == null ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.NonPublic); |
| 182 | + return internalMethod.Invoke(instance, argsToPass); |
| 183 | + } |
| 184 | + |
| 185 | + // Same as InvokeMethod, but for a specific overload |
| 186 | + private static object InvokeMethodOverload(string methodName, |
| 187 | + object[] argsToPass, |
| 188 | + Type[] overloadArgTypes, |
| 189 | + ModelExporter instance = null) |
| 190 | + { |
| 191 | + var internalMethod = typeof(ModelExporter).GetMethod(methodName, |
| 192 | + (instance == null ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.NonPublic, |
| 193 | + null, |
| 194 | + overloadArgTypes, |
| 195 | + null); |
| 196 | + return internalMethod.Invoke(instance, argsToPass); |
| 197 | + } |
| 198 | + |
| 199 | + private static object GetStaticProperty(string propertyName) |
| 200 | + { |
| 201 | + // Use reflection to get the internal property |
| 202 | + var internalProperty = typeof(ModelExporter).GetProperty(propertyName, |
| 203 | + BindingFlags.NonPublic | BindingFlags.Static); |
| 204 | + return internalProperty.GetValue(null,null); |
| 205 | + } |
| 206 | + |
| 207 | + private static object GetStaticField(string fieldName) |
| 208 | + { |
| 209 | + // Use reflection to get the internal property |
| 210 | + var internalField = typeof(ModelExporter).GetField(fieldName, |
| 211 | + BindingFlags.NonPublic | BindingFlags.Static); |
| 212 | + return internalField.GetValue(null); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments