Skip to content

Commit 8585ce7

Browse files
committed
don't ask to overwrite fbx if it's a model instance
- also show the original model files name and path in the UI by default
1 parent 9271efb commit 8585ce7

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

Assets/FbxExporters/Editor/ConvertToPrefabEditorWindow.cs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,20 @@ protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
3232
m_toConvert = toConvert.OrderBy (go => go.name).ToArray ();
3333

3434
if (m_toConvert.Length == 1) {
35-
m_prefabFileName = m_toConvert [0].name;
35+
var go = m_toConvert [0];
36+
// check if the GameObject is a model instance, use as default filename and path if it is
37+
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(go);
38+
if (unityPrefabType == PrefabType.ModelPrefabInstance && go.Equals (PrefabUtility.FindPrefabRoot (go))) {
39+
var mainAsset = PrefabUtility.GetPrefabParent (go) as GameObject;
40+
var mainAssetRelPath = AssetDatabase.GetAssetPath (mainAsset);
41+
// remove Assets/ from beginning of path
42+
mainAssetRelPath = mainAssetRelPath.Substring ("Assets".Length);
43+
44+
m_prefabFileName = System.IO.Path.GetFileNameWithoutExtension (mainAssetRelPath);
45+
ExportSettings.AddFbxSavePath (System.IO.Path.GetDirectoryName (mainAssetRelPath));
46+
} else {
47+
m_prefabFileName = go.name;
48+
}
3649
} else if (m_toConvert.Length > 1) {
3750
m_prefabFileName = "(automatic)";
3851
}
@@ -56,19 +69,51 @@ protected override void Export ()
5669
var prefabDirPath = ExportSettings.GetPrefabAbsoluteSavePath ();
5770
var prefabPath = System.IO.Path.Combine (prefabDirPath, m_prefabFileName + ".prefab");
5871

59-
// check if file already exists, give a warning if it does
60-
if (!OverwriteExistingFile (fbxPath) || !OverwriteExistingFile (prefabPath)) {
61-
return;
62-
}
63-
6472
if (m_toConvert == null) {
6573
Debug.LogError ("FbxExporter: missing object for conversion");
6674
return;
6775
}
6876

6977
if (m_toConvert.Length == 1) {
78+
var go = m_toConvert [0];
79+
80+
if (!OverwriteExistingFile (prefabPath)) {
81+
return;
82+
}
83+
84+
// Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
85+
// Children of model prefab instances will also have "model prefab instance"
86+
// as their prefab type, so it is important that it is the root that is selected.
87+
//
88+
// e.g. If I have the following hierarchy:
89+
// Cube
90+
// -- Sphere
91+
//
92+
// Both the Cube and Sphere will have ModelPrefabInstance as their prefab type.
93+
// However, when selecting the Sphere to convert, we don't want to connect it to the
94+
// existing FBX but create a new FBX containing just the sphere.
95+
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(go);
96+
if (unityPrefabType == PrefabType.ModelPrefabInstance && go.Equals(PrefabUtility.FindPrefabRoot(go))) {
97+
// don't re-export fbx
98+
// create prefab out of model instance in scene, link to existing fbx
99+
var mainAsset = PrefabUtility.GetPrefabParent(go) as GameObject;
100+
var mainAssetRelPath = AssetDatabase.GetAssetPath(mainAsset);
101+
var mainAssetAbsPath = System.IO.Directory.GetParent(Application.dataPath) + "/" + mainAssetRelPath;
102+
var relPrefabPath = ExportSettings.GetProjectRelativePath (prefabPath);
103+
104+
if (string.Equals(System.IO.Path.GetFullPath(fbxPath), System.IO.Path.GetFullPath(mainAssetAbsPath))) {
105+
ConvertToModel.SetupFbxPrefab(go, mainAsset, relPrefabPath, mainAssetAbsPath);
106+
return;
107+
}
108+
}
109+
110+
// check if file already exists, give a warning if it does
111+
if (!OverwriteExistingFile (fbxPath)) {
112+
return;
113+
}
114+
70115
ConvertToModel.Convert (
71-
m_toConvert[0], fbxFullPath: fbxPath, prefabFullPath: prefabPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info
116+
go, fbxFullPath: fbxPath, prefabFullPath: prefabPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info
72117
);
73118
return;
74119
}

0 commit comments

Comments
 (0)