Skip to content

Commit 5cc18f9

Browse files
authored
Merge pull request #345 from Unity-Technologies/UNI-41933-convert-UI-dont-overwrite-existing-fbx
Uni 41933 convert ui dont overwrite existing fbx
2 parents 459b19b + ffcfebf commit 5cc18f9

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,7 @@ public static GameObject Convert (
108108
)
109109
{
110110
// Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
111-
// Children of model prefab instances will also have "model prefab instance"
112-
// as their prefab type, so it is important that it is the root that is selected.
113-
//
114-
// e.g. If I have the following hierarchy:
115-
// Cube
116-
// -- Sphere
117-
//
118-
// Both the Cube and Sphere will have ModelPrefabInstance as their prefab type.
119-
// However, when selecting the Sphere to convert, we don't want to connect it to the
120-
// existing FBX but create a new FBX containing just the sphere.
121-
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(toConvert);
122-
if (unityPrefabType == PrefabType.ModelPrefabInstance && toConvert.Equals(PrefabUtility.FindPrefabRoot(toConvert))) {
111+
if(IsModelInstance(toConvert)){
123112
// don't re-export fbx
124113
// create prefab out of model instance in scene, link to existing fbx
125114
var mainAsset = PrefabUtility.GetPrefabParent(toConvert) as GameObject;
@@ -223,6 +212,26 @@ public static void SetupFbxPrefab(GameObject toConvert, GameObject unityMainAsse
223212
}
224213
}
225214

215+
/// <summary>
216+
/// Determines if the given GameObject is a model instance.
217+
/// </summary>
218+
/// <returns><c>true</c> if go is a model instance; otherwise, <c>false</c>.</returns>
219+
/// <param name="go">Go.</param>
220+
public static bool IsModelInstance(GameObject go){
221+
// Children of model prefab instances will also have "model prefab instance"
222+
// as their prefab type, so it is important that it is the root that is selected.
223+
//
224+
// e.g. If I have the following hierarchy:
225+
// Cube
226+
// -- Sphere
227+
//
228+
// Both the Cube and Sphere will have ModelPrefabInstance as their prefab type.
229+
// However, when selecting the Sphere to convert, we don't want to connect it to the
230+
// existing FBX but create a new FBX containing just the sphere.
231+
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(go);
232+
return unityPrefabType == PrefabType.ModelPrefabInstance && go.Equals (PrefabUtility.FindPrefabRoot (go));
233+
}
234+
226235
/// <summary>
227236
/// Check if the file exists, and if it does, then increment the name.
228237
/// e.g. if filename is Sphere.fbx and it already exists, change it to Sphere 1.fbx.

Assets/FbxExporters/Editor/ConvertToPrefabEditorWindow.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,23 @@ protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
4545

4646
TransferAnimationSource = null;
4747
TransferAnimationDest = null;
48-
4948
if (ToExport.Length == 1) {
50-
m_prefabFileName = ToExport [0].name;
49+
var go = ModelExporter.GetGameObject (ToExport [0]);
50+
// check if the GameObject is a model instance, use as default filename and path if it is
51+
if(ConvertToModel.IsModelInstance(go)) {
52+
var mainAsset = PrefabUtility.GetPrefabParent (go) as GameObject;
53+
var mainAssetRelPath = AssetDatabase.GetAssetPath (mainAsset);
54+
// remove Assets/ from beginning of path
55+
mainAssetRelPath = mainAssetRelPath.Substring ("Assets".Length);
56+
57+
m_prefabFileName = System.IO.Path.GetFileNameWithoutExtension (mainAssetRelPath);
58+
ExportSettings.AddFbxSavePath (System.IO.Path.GetDirectoryName (mainAssetRelPath));
59+
}
60+
else{
61+
m_prefabFileName = ToExport [0].name;
62+
}
5163

5264
// if only one object selected, set transfer source/dest to this object
53-
var go = ModelExporter.GetGameObject (ToExport [0]);
5465
if (go) {
5566
TransferAnimationSource = go.transform;
5667
TransferAnimationDest = go.transform;
@@ -79,18 +90,38 @@ protected override void Export ()
7990
var prefabDirPath = ExportSettings.GetPrefabAbsoluteSavePath ();
8091
var prefabPath = System.IO.Path.Combine (prefabDirPath, m_prefabFileName + ".prefab");
8192

82-
// check if file already exists, give a warning if it does
83-
if (!OverwriteExistingFile (fbxPath) || !OverwriteExistingFile (prefabPath)) {
84-
return;
85-
}
86-
8793
if (ToExport == null) {
8894
Debug.LogError ("FbxExporter: missing object for conversion");
8995
return;
9096
}
9197

9298
if (ToExport.Length == 1) {
9399
var go = ModelExporter.GetGameObject (ToExport [0]);
100+
101+
if (!OverwriteExistingFile (prefabPath)) {
102+
return;
103+
}
104+
105+
// Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
106+
if(ConvertToModel.IsModelInstance(go)) {
107+
// don't re-export fbx
108+
// create prefab out of model instance in scene, link to existing fbx
109+
var mainAsset = PrefabUtility.GetPrefabParent(go) as GameObject;
110+
var mainAssetRelPath = AssetDatabase.GetAssetPath(mainAsset);
111+
var mainAssetAbsPath = System.IO.Directory.GetParent(Application.dataPath) + "/" + mainAssetRelPath;
112+
var relPrefabPath = ExportSettings.GetProjectRelativePath (prefabPath);
113+
114+
if (string.Equals(System.IO.Path.GetFullPath(fbxPath), System.IO.Path.GetFullPath(mainAssetAbsPath))) {
115+
ConvertToModel.SetupFbxPrefab(go, mainAsset, relPrefabPath, mainAssetAbsPath);
116+
return;
117+
}
118+
}
119+
120+
// check if file already exists, give a warning if it does
121+
if (!OverwriteExistingFile (fbxPath)) {
122+
return;
123+
}
124+
94125
ConvertToModel.Convert (
95126
go, fbxFullPath: fbxPath, prefabFullPath: prefabPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info
96127
);

0 commit comments

Comments
 (0)