Skip to content

Commit 934437f

Browse files
authored
Merge pull request #378 from Unity-Technologies/uni-40523-convert-from-asset
Uni 40523 convert from asset
2 parents 867def1 + b8cba16 commit 934437f

10 files changed

+507
-157
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 228 additions & 57 deletions
Large diffs are not rendered by default.

Assets/FbxExporters/Editor/ConvertToPrefabEditorWindow.cs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@ protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
4848
if (ToExport.Length == 1) {
4949
var go = ModelExporter.GetGameObject (ToExport [0]);
5050
// 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;
51+
var mainAsset = ConvertToModel.GetFbxAssetOrNull(go);
52+
if (!mainAsset) {
53+
// Use the game object's name
54+
m_prefabFileName = go.name;
55+
} else {
56+
// Use the asset's name
5357
var mainAssetRelPath = AssetDatabase.GetAssetPath (mainAsset);
5458
// remove Assets/ from beginning of path
5559
mainAssetRelPath = mainAssetRelPath.Substring ("Assets".Length);
5660

5761
m_prefabFileName = System.IO.Path.GetFileNameWithoutExtension (mainAssetRelPath);
5862
ExportSettings.AddFbxSavePath (System.IO.Path.GetDirectoryName (mainAssetRelPath));
5963
}
60-
else{
61-
m_prefabFileName = ToExport [0].name;
62-
}
6364

6465
// if only one object selected, set transfer source/dest to this object
6566
if (go) {
@@ -144,28 +145,17 @@ protected override bool Export ()
144145
if (ToExport.Length == 1) {
145146
var go = ModelExporter.GetGameObject (ToExport [0]);
146147

147-
if (!OverwriteExistingFile (prefabPath)) {
148-
return false;
149-
}
150-
151-
// Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
152-
if(ConvertToModel.IsModelInstance(go)) {
153-
// don't re-export fbx
154-
// create prefab out of model instance in scene, link to existing fbx
155-
var mainAsset = PrefabUtility.GetPrefabParent(go) as GameObject;
156-
var mainAssetRelPath = AssetDatabase.GetAssetPath(mainAsset);
157-
var mainAssetAbsPath = System.IO.Directory.GetParent(Application.dataPath) + "/" + mainAssetRelPath;
158-
var relPrefabPath = ExportSettings.GetProjectRelativePath (prefabPath);
159-
160-
if (string.Equals(System.IO.Path.GetFullPath(fbxPath), System.IO.Path.GetFullPath(mainAssetAbsPath))) {
161-
ConvertToModel.SetupFbxPrefab(go, mainAsset, relPrefabPath, mainAssetAbsPath);
162-
return true;
148+
// Check if we'll be clobbering files. If so, warn the user
149+
// first and let them cancel out.
150+
if (ConvertToModel.WillCreatePrefab(go)) {
151+
if (!OverwriteExistingFile (prefabPath)) {
152+
return false;
163153
}
164154
}
165-
166-
// check if file already exists, give a warning if it does
167-
if (!OverwriteExistingFile (fbxPath)) {
168-
return false;
155+
if (ConvertToModel.WillExportFbx(go)) {
156+
if (!OverwriteExistingFile (fbxPath)) {
157+
return false;
158+
}
169159
}
170160

171161
ConvertToModel.Convert (
@@ -175,6 +165,7 @@ protected override bool Export ()
175165
}
176166

177167
foreach (var obj in ToExport) {
168+
// Convert, automatically choosing a file path that won't clobber any existing files.
178169
var go = ModelExporter.GetGameObject (obj);
179170
ConvertToModel.Convert (
180171
go, fbxDirectoryFullPath: fbxDirPath, prefabDirectoryFullPath: prefabDirPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info

Assets/FbxExporters/Editor/FbxPrefabAutoUpdater.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,18 @@ public static bool OnValidateMenuItem()
246246
/// <summary>
247247
/// Launch the manual update of the linked prefab specified
248248
/// </summary>
249-
public static void UpdateLinkedPrefab(GameObject prefabInstance)
249+
public static void UpdateLinkedPrefab(GameObject prefabOrInstance)
250250
{
251-
GameObject prefab = UnityEditor.PrefabUtility.GetPrefabParent(prefabInstance) as GameObject;
252-
if (!prefab)
253-
{
251+
// Find the prefab, bail if this is neither a prefab nor an instance.
252+
GameObject prefab;
253+
switch(PrefabUtility.GetPrefabType(prefabOrInstance)) {
254+
case PrefabType.Prefab:
255+
prefab = prefabOrInstance;
256+
break;
257+
case PrefabType.PrefabInstance:
258+
prefab = PrefabUtility.GetPrefabParent(prefabOrInstance) as GameObject;
259+
break;
260+
default:
254261
return;
255262
}
256263

0 commit comments

Comments
 (0)