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 ;
8+ using UnityEditor . ProBuilder . Actions ;
79
810namespace UnityEngine . ProBuilder . Addons . FBX
911{
@@ -26,29 +28,7 @@ class FbxOptions
2628 [ InitializeOnLoad ]
2729 static class Fbx
2830 {
29- private static Assembly FbxExporterAssembly
30- {
31- get
32- {
33- try
34- {
35- return Assembly . Load ( "Unity.Formats.Fbx.Editor" ) ;
36- }
37- catch ( System . IO . FileNotFoundException )
38- {
39- return null ;
40- }
41- }
42- }
43-
44- static readonly Type [ ] k_ProBuilderTypes = new Type [ ]
45- {
46- typeof ( BezierShape ) ,
47- typeof ( PolyShape ) ,
48- typeof ( Entity )
49- } ;
50-
51- static FbxOptions m_FbxOptions = new FbxOptions ( ) {
31+ static FbxOptions s_FbxOptions = new FbxOptions ( ) {
5232 quads = true
5333 } ;
5434
@@ -59,52 +39,35 @@ static Fbx()
5939
6040 static void TryLoadFbxSupport ( )
6141 {
62- if ( FbxExporterAssembly == null )
63- {
64- return ;
65- }
66-
67- var modelExporter = FbxExporterAssembly . GetType ( "UnityEditor.Formats.Fbx.Exporter.ModelExporter" ) ;
68- var registerMeshCallback = modelExporter . GetMethods ( BindingFlags . NonPublic | BindingFlags . Static ) . Where ( x => x . Name == "RegisterMeshCallback" ) . First ( x => x . ContainsGenericParameters ) ;
69- registerMeshCallback = registerMeshCallback . MakeGenericMethod ( typeof ( ProBuilderMesh ) ) ;
70-
71- var getMeshForComponent = FbxExporterAssembly . GetTypes ( )
72- . Where ( t => t . BaseType == typeof ( MulticastDelegate ) && t . Name . StartsWith ( "GetMeshForComponent" ) )
73- . First ( t => t . ContainsGenericParameters ) ;
74-
75- getMeshForComponent = getMeshForComponent . MakeGenericType ( typeof ( ProBuilderMesh ) ) ;
76- var meshDelegate = Delegate . CreateDelegate ( getMeshForComponent , typeof ( Fbx ) . GetMethod ( "GetMeshForComponent" , BindingFlags . NonPublic | BindingFlags . Static ) ) ;
77-
78- registerMeshCallback . Invoke ( null , new object [ ] { meshDelegate , true } ) ;
79-
80- 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 ) ;
8144 }
8245
83- static bool GetMeshForComponent ( object exporter , ProBuilderMesh pmesh , object node )
46+ static bool GetMeshForPBComponent ( ModelExporter exporter , ProBuilderMesh pmesh , Autodesk . Fbx . FbxNode node )
8447 {
8548 Mesh mesh = new Mesh ( ) ;
86- MeshUtility . Compile ( pmesh , mesh , m_FbxOptions . quads ? MeshTopology . Quads : MeshTopology . Triangles ) ;
49+ MeshUtility . Compile ( pmesh , mesh , s_FbxOptions . quads ? MeshTopology . Quads : MeshTopology . Triangles ) ;
8750
88- // using reflection to call: exporter.ExportMesh(mesh, node, pmesh.GetComponent<MeshRenderer>().sharedMaterials)
8951 var pMeshRenderer = pmesh . GetComponent < MeshRenderer > ( ) ;
9052 var sharedMaterials = pMeshRenderer ? pMeshRenderer . sharedMaterials : null ;
91- var exportMeshMethod = exporter . GetType ( ) . GetMethod ( "ExportMesh" , BindingFlags . NonPublic | BindingFlags . Instance , null , new Type [ ] { typeof ( Mesh ) , node . GetType ( ) , typeof ( Material [ ] ) } , null ) ;
92- exportMeshMethod . Invoke ( exporter , new object [ ] { mesh , node , sharedMaterials } ) ;
53+
54+ exporter . ExportMesh ( mesh , node , sharedMaterials ) ;
9355
9456 Object . DestroyImmediate ( mesh ) ;
9557
96- // probuilder can't handle mesh assets that may be externally reloaded, just strip pb stuff for now.
97- foreach ( var type in k_ProBuilderTypes )
58+ //Need to have ExportOptions accessible to remove this reflection
59+ var exporterType = exporter . GetType ( ) . GetMethods ( BindingFlags . NonPublic | BindingFlags . Instance )
60+ . First ( x => x . Name == "get_ExportOptions" ) . Invoke ( exporter , null ) . GetType ( ) ;
61+
62+ if ( exporterType == typeof ( ConvertToPrefabSettingsSerialize ) )
9863 {
99- var component = pmesh . GetComponent ( type ) ;
100- if ( component != null )
101- Object . DestroyImmediate ( component ) ;
64+ // probuilder can't handle mesh assets that may be externally reloaded, just strip pb stuff for now.
65+ StripProBuilderScripts . DoStrip ( pmesh ) ;
10266 }
10367
104- pmesh . preserveMeshAssetOnDestroy = true ;
105- Object . DestroyImmediate ( pmesh ) ;
106-
10768 return true ;
10869 }
10970 }
11071}
72+
73+ #endif
0 commit comments