1- // todo Once we drop support for 2018.3, use optional assembly definitions
2- using System ;
1+ #if FBX_EXPORTER
2+
33using UnityEditor ;
44using System . Reflection ;
55using System . Linq ;
6+ using UnityEditor . Formats . Fbx . Exporter ;
67using UnityEditor . ProBuilder ;
78using UnityEditor . ProBuilder . Actions ;
89
@@ -27,22 +28,7 @@ class FbxOptions
2728 [ InitializeOnLoad ]
2829 static class Fbx
2930 {
30- private static Assembly FbxExporterAssembly
31- {
32- get
33- {
34- try
35- {
36- return Assembly . Load ( "Unity.Formats.Fbx.Editor" ) ;
37- }
38- catch ( System . IO . FileNotFoundException )
39- {
40- return null ;
41- }
42- }
43- }
44-
45- static FbxOptions m_FbxOptions = new FbxOptions ( ) {
31+ static FbxOptions s_FbxOptions = new FbxOptions ( ) {
4632 quads = true
4733 } ;
4834
@@ -53,46 +39,27 @@ static Fbx()
5339
5440 static void TryLoadFbxSupport ( )
5541 {
56- if ( FbxExporterAssembly == null )
57- {
58- return ;
59- }
60-
61- var modelExporter = FbxExporterAssembly . GetType ( "UnityEditor.Formats.Fbx.Exporter.ModelExporter" ) ;
62- var registerMeshCallback = modelExporter . GetMethods ( BindingFlags . NonPublic | BindingFlags . Static ) . Where ( x => x . Name == "RegisterMeshCallback" ) . First ( x => x . ContainsGenericParameters ) ;
63- registerMeshCallback = registerMeshCallback . MakeGenericMethod ( typeof ( ProBuilderMesh ) ) ;
64-
65- var getMeshForComponent = FbxExporterAssembly . GetTypes ( )
66- . Where ( t => t . BaseType == typeof ( MulticastDelegate ) && t . Name . StartsWith ( "GetMeshForComponent" ) )
67- . First ( t => t . ContainsGenericParameters ) ;
68-
69- getMeshForComponent = getMeshForComponent . MakeGenericType ( typeof ( ProBuilderMesh ) ) ;
70- var meshDelegate = Delegate . CreateDelegate ( getMeshForComponent , typeof ( Fbx ) . GetMethod ( "GetMeshForComponent" , BindingFlags . NonPublic | BindingFlags . Static ) ) ;
71-
72- registerMeshCallback . Invoke ( null , new object [ ] { meshDelegate , true } ) ;
73-
74- m_FbxOptions . quads = ProBuilderSettings . Get < bool > ( "Export::m_FbxQuads" , SettingsScope . User , true ) ;
42+ ModelExporter . RegisterMeshCallback < ProBuilderMesh > ( GetMeshForPBComponent , true ) ;
43+ s_FbxOptions . quads = ProBuilderSettings . Get < bool > ( "Export::m_FbxQuads" , SettingsScope . User , true ) ;
7544 }
7645
77- static bool GetMeshForComponent ( object exporter , ProBuilderMesh pmesh , object node )
46+ static bool GetMeshForPBComponent ( ModelExporter exporter , ProBuilderMesh pmesh , Autodesk . Fbx . FbxNode node )
7847 {
7948 Mesh mesh = new Mesh ( ) ;
80- MeshUtility . Compile ( pmesh , mesh , m_FbxOptions . quads ? MeshTopology . Quads : MeshTopology . Triangles ) ;
49+ MeshUtility . Compile ( pmesh , mesh , s_FbxOptions . quads ? MeshTopology . Quads : MeshTopology . Triangles ) ;
8150
82- // using reflection to call: exporter.ExportMesh(mesh, node, pmesh.GetComponent<MeshRenderer>().sharedMaterials)
8351 var pMeshRenderer = pmesh . GetComponent < MeshRenderer > ( ) ;
8452 var sharedMaterials = pMeshRenderer ? pMeshRenderer . sharedMaterials : null ;
85- var exportMeshMethod = exporter . GetType ( ) . GetMethod ( "ExportMesh" , BindingFlags . NonPublic | BindingFlags . Instance , null , new Type [ ] { typeof ( Mesh ) , node . GetType ( ) , typeof ( Material [ ] ) } , null ) ;
86- exportMeshMethod . Invoke ( exporter , new object [ ] { mesh , node , sharedMaterials } ) ;
53+
54+ exporter . ExportMesh ( mesh , node , sharedMaterials ) ;
8755
8856 Object . DestroyImmediate ( mesh ) ;
8957
58+ //Need to have ExportOptions accessible to remove this reflection
9059 var exporterType = exporter . GetType ( ) . GetMethods ( BindingFlags . NonPublic | BindingFlags . Instance )
9160 . First ( x => x . Name == "get_ExportOptions" ) . Invoke ( exporter , null ) . GetType ( ) ;
9261
93- var prefabExporterType = FbxExporterAssembly . GetType ( "UnityEditor.Formats.Fbx.Exporter.ConvertToPrefabSettingsSerialize" ) ;
94-
95- if ( exporterType == prefabExporterType )
62+ if ( exporterType == typeof ( ConvertToPrefabSettingsSerialize ) )
9663 {
9764 // probuilder can't handle mesh assets that may be externally reloaded, just strip pb stuff for now.
9865 StripProBuilderScripts . DoStrip ( pmesh ) ;
@@ -102,3 +69,5 @@ static bool GetMeshForComponent(object exporter, ProBuilderMesh pmesh, object no
10269 }
10370 }
10471}
72+
73+ #endif
0 commit comments