Skip to content

Commit 9fd4331

Browse files
authored
Merge pull request #394 from Unity-Technologies/bugfix/1334017-pbshape-fbxexport
Fixing PBShape export using FBX Exporter
2 parents 29f96cb + 6788fc7 commit 9fd4331

3 files changed

Lines changed: 36 additions & 61 deletions

File tree

Addons/Fbx.cs

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
// todo Once we drop support for 2018.3, use optional assembly definitions
2-
using System;
1+
#if FBX_EXPORTER
2+
33
using UnityEditor;
44
using System.Reflection;
55
using System.Linq;
6+
using UnityEditor.Formats.Fbx.Exporter;
67
using UnityEditor.ProBuilder;
8+
using UnityEditor.ProBuilder.Actions;
79

810
namespace 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
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
{
22
"name": "Unity.ProBuilder.AddOns.Editor",
3+
"rootNamespace": "",
34
"references": [
45
"Unity.ProBuilder",
5-
"Unity.ProBuilder.Editor"
6+
"Unity.ProBuilder.Editor",
7+
"Unity.Formats.Fbx.Editor",
8+
"Autodesk.Fbx"
69
],
7-
"optionalUnityReferences": [],
810
"includePlatforms": [
911
"Editor"
1012
],
1113
"excludePlatforms": [],
1214
"allowUnsafeCode": false,
1315
"overrideReferences": false,
1416
"precompiledReferences": [],
15-
"autoReferenced": true
16-
}
17+
"autoReferenced": true,
18+
"defineConstraints": [],
19+
"versionDefines": [
20+
{
21+
"name": "com.unity.formats.fbx",
22+
"expression": "4.0.0",
23+
"define": "FBX_EXPORTER"
24+
}
25+
],
26+
"noEngineReferences": false
27+
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Bug Fixes
1111

12+
- [case: 1334017] Fixed errors while exporting a PBShape using FBX Exporter and cleaning export.
1213
- [case: 1332226] Fixed issue where some Gizmos menu items would be missing in projects that have ProBuilder package installed.
1314
- [case: 1324374] Fixed incorrect vertex/edge/face rect selection when mesh's parent is rotated and/or scaled.
1415

0 commit comments

Comments
 (0)