ConvertToNestedPrefab.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Linq;
  6. using System.IO;
  7. using System.Runtime.Serialization;
  8. using System.Security.Permissions;
  9. namespace UnityEditor.Formats.Fbx.Exporter
  10. {
  11. [System.Serializable]
  12. internal class ConvertToNestedPrefabException : System.Exception
  13. {
  14. public ConvertToNestedPrefabException()
  15. {
  16. }
  17. public ConvertToNestedPrefabException(string message)
  18. : base(message)
  19. {
  20. }
  21. public ConvertToNestedPrefabException(string message, System.Exception inner)
  22. : base(message, inner)
  23. {
  24. }
  25. protected ConvertToNestedPrefabException(SerializationInfo info, StreamingContext context)
  26. : base(info, context)
  27. {
  28. }
  29. }
  30. internal static class ConvertToNestedPrefab
  31. {
  32. const string GameObjectMenuItemName = "GameObject/Convert To FBX Prefab Variant...";
  33. const string AssetsMenuItemName = "Assets/Convert To FBX Prefab Variant...";
  34. const string UndoConversionGroup = "Convert {0} to FBX Prefab Variant";
  35. internal const string UndoConversionCreateObject = "Convert to FBX Prefab Variant";
  36. /// <summary>
  37. /// OnContextItem is called either:
  38. /// * when the user selects the menu item via the top menu (with a null MenuCommand), or
  39. /// * when the user selects the menu item via the context menu (in which case there's a context)
  40. ///
  41. /// OnContextItem gets called once per selected object (if the
  42. /// parent and child are selected, then OnContextItem will only be
  43. /// called on the parent)
  44. /// </summary>
  45. [MenuItem(GameObjectMenuItemName, false, 30)]
  46. static void OnGameObjectContextItem(MenuCommand command)
  47. {
  48. OnContextItem(command, SelectionMode.Editable | SelectionMode.TopLevel);
  49. }
  50. [MenuItem(AssetsMenuItemName, false, 30)]
  51. static void OnAssetsContextItem(MenuCommand command)
  52. {
  53. OnContextItem(command, SelectionMode.Assets);
  54. }
  55. static void OnContextItem(MenuCommand command, SelectionMode mode)
  56. {
  57. GameObject[] selection = null;
  58. if (command == null || command.context == null)
  59. {
  60. // We were actually invoked from the top GameObject menu, so use the selection.
  61. selection = Selection.GetFiltered<GameObject>(mode);
  62. }
  63. else
  64. {
  65. // We were invoked from the right-click menu, so use the context of the context menu.
  66. var selected = command.context as GameObject;
  67. if (selected)
  68. {
  69. selection = new GameObject[] { selected };
  70. }
  71. }
  72. if (selection == null || selection.Length == 0)
  73. {
  74. ModelExporter.DisplayNoSelectionDialog();
  75. return;
  76. }
  77. Selection.objects = CreateInstantiatedModelPrefab(selection);
  78. }
  79. internal static void DisplayInvalidSelectionDialog(GameObject toConvert, string message = "")
  80. {
  81. UnityEditor.EditorUtility.DisplayDialog(
  82. string.Format("{0} Warning", "FBX Exporter"),
  83. string.Format("Failed to Convert: {0}\n{1}", toConvert.name, message),
  84. "Ok");
  85. }
  86. /// <summary>
  87. // Validate the menu items defined above.
  88. /// </summary>
  89. [MenuItem(GameObjectMenuItemName, true, 30)]
  90. [MenuItem(AssetsMenuItemName, true, 30)]
  91. public static bool OnValidateMenuItem()
  92. {
  93. return true;
  94. }
  95. /// <summary>
  96. /// Gets the export settings.
  97. /// </summary>
  98. public static ExportSettings ExportSettings
  99. {
  100. get { return ExportSettings.instance; }
  101. }
  102. /// <summary>
  103. /// Return true if the given set contains only prefab assets on disk,
  104. /// and nothing from the scene.
  105. /// </summary>
  106. /// <returns></returns>
  107. internal static bool SetContainsOnlyPrefabAssets(Object[] toConvert)
  108. {
  109. foreach (var obj in toConvert)
  110. {
  111. var go = ModelExporter.GetGameObject(obj);
  112. if (go != null && !PrefabUtility.IsPartOfPrefabAsset(go))
  113. {
  114. // return as soon as we find something that is not part of a prefab asset
  115. // on disk
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. /// <summary>
  122. /// Create instantiated model prefabs from a selection of objects.
  123. ///
  124. /// Every hierarchy in the selection will be exported, under the name of the root.
  125. ///
  126. /// If an object and one of its descendents are both selected, the descendent is not promoted to be a prefab -- we only export the root.
  127. /// </summary>
  128. /// <returns>list of instanced Model Prefabs</returns>
  129. /// <param name="unityGameObjectsToConvert">Unity game objects to convert to Model Prefab instances</param>
  130. /// <param name="path">Path to save Model Prefab; use FbxExportSettings if null</param>
  131. [SecurityPermission(SecurityAction.LinkDemand)]
  132. public static GameObject[] CreateInstantiatedModelPrefab(
  133. GameObject[] unityGameObjectsToConvert)
  134. {
  135. var toExport = ModelExporter.RemoveRedundantObjects(unityGameObjectsToConvert);
  136. if (ExportSettings.instance.ShowConvertToPrefabDialog)
  137. {
  138. if (toExport.Count == 1)
  139. {
  140. var go = toExport.First();
  141. if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go) && !PrefabUtility.IsOutermostPrefabInstanceRoot(go))
  142. {
  143. DisplayInvalidSelectionDialog(go,
  144. "Children of a Prefab instance cannot be converted.\nYou can open the Prefab in Prefab Mode or unpack the Prefab instance to convert it's children");
  145. return null;
  146. }
  147. if (PrefabUtility.IsPartOfPrefabAsset(go) && go.transform.parent != null)
  148. {
  149. DisplayInvalidSelectionDialog(go,
  150. "Children of a Prefab Asset cannot be converted.\nYou can open the Prefab in Prefab Mode or unpack the Prefab instance to convert it's children");
  151. return null;
  152. }
  153. // can't currently handle converting root of prefab in prefab preview scene
  154. if (SceneManagement.EditorSceneManager.IsPreviewSceneObject(go) && go.transform.parent == null)
  155. {
  156. DisplayInvalidSelectionDialog(go,
  157. "Cannot convert Prefab root in the Prefab Preview Scene.\nYou can convert a Prefab Instance or convert the Prefab Asset directly in the Project view");
  158. return null;
  159. }
  160. }
  161. ConvertToPrefabEditorWindow.Init(toExport);
  162. return toExport.ToArray();
  163. }
  164. bool onlyPrefabAssets = ConvertToNestedPrefab.SetContainsOnlyPrefabAssets(unityGameObjectsToConvert);
  165. int groupIndex = -1;
  166. // If only Prefab Assets on disk are selected (nothing in the scene), then do not
  167. // try to undo as modifications on disk cannot be undone.
  168. if (!onlyPrefabAssets)
  169. {
  170. Undo.IncrementCurrentGroup();
  171. groupIndex = Undo.GetCurrentGroup();
  172. Undo.SetCurrentGroupName(UndoConversionCreateObject);
  173. }
  174. var converted = new List<GameObject>();
  175. var exportOptions = ExportSettings.instance.ConvertToPrefabSettings.info;
  176. foreach (var go in toExport)
  177. {
  178. var convertedGO = Convert(go, exportOptions: exportOptions);
  179. if (convertedGO != null)
  180. {
  181. converted.Add(convertedGO);
  182. }
  183. }
  184. if (!onlyPrefabAssets && groupIndex >= 0)
  185. {
  186. Undo.CollapseUndoOperations(groupIndex);
  187. Undo.IncrementCurrentGroup();
  188. }
  189. return converted.ToArray();
  190. }
  191. /// <summary>
  192. /// For all scene objects holding a reference to origObj, replaces the references to newObj.
  193. ///
  194. /// If one of the scene objects is toConvertRoot or a child of it, then do not fix its references as it
  195. /// will be deleted after conversion.
  196. /// </summary>
  197. /// <param name="origObj"></param>
  198. /// <param name="newObj"></param>
  199. /// <param name="toConvertRoot"></param>
  200. internal static void FixSceneReferences(Object origObj, Object newObj, GameObject toConvertRoot)
  201. {
  202. var sceneObjs = GetSceneReferencesToObject(origObj);
  203. // try to fix references on each component of each scene object, if applicable
  204. foreach(var sceneObj in sceneObjs)
  205. {
  206. var go = ModelExporter.GetGameObject(sceneObj);
  207. if (go && go.transform.IsChildOf(toConvertRoot.transform))
  208. {
  209. // if this is a child of what we are converting, don't update its references.
  210. continue;
  211. }
  212. var components = sceneObj.GetComponents<Component>();
  213. foreach(var component in components)
  214. {
  215. var serializedComponent = new SerializedObject(component);
  216. var property = serializedComponent.GetIterator();
  217. property.Next(true); // skip generic field
  218. // For SkinnedMeshRenderer, the bones array doesn't have visible children, but may have references that need to be fixed.
  219. // For everything else, filtering by visible children in the while loop and then copying properties that don't have visible children,
  220. // ensures that only the leaf properties are copied over. Copying other properties is not usually necessary and may break references that
  221. // were not meant to be copied.
  222. while (property.Next((component is SkinnedMeshRenderer) ? property.hasChildren : property.hasVisibleChildren))
  223. {
  224. if (!property.hasVisibleChildren)
  225. {
  226. // with Undo operations, copying m_Father reference causes issues. Also, it is not required as the reference is fixed when
  227. // the transform is parented under the correct hierarchy (which happens before this).
  228. if (property.propertyType == SerializedPropertyType.ObjectReference && property.propertyPath != "m_GameObject" &&
  229. property.propertyPath != "m_Father" && property.objectReferenceValue &&
  230. (property.objectReferenceValue == origObj))
  231. {
  232. property.objectReferenceValue = newObj;
  233. serializedComponent.ApplyModifiedProperties();
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// Helper for getting a property from an instance with reflection.
  242. /// </summary>
  243. /// <param name="instance"></param>
  244. /// <param name="propertyName"></param>
  245. /// <param name="isPublic"></param>
  246. /// <returns></returns>
  247. private static object GetPropertyReflection(object instance, string propertyName, bool isPublic)
  248. {
  249. return instance.GetType().GetProperty(propertyName, (isPublic ? System.Reflection.BindingFlags.Public : System.Reflection.BindingFlags.NonPublic) |
  250. System.Reflection.BindingFlags.Instance).GetValue(instance, null);
  251. }
  252. /// <summary>
  253. /// Returns a list of GameObjects in the scene that contain references to the given object.
  254. /// </summary>
  255. /// <param name="obj"></param>
  256. /// <returns></returns>
  257. internal static List<GameObject> GetSceneReferencesToObject(Object obj)
  258. {
  259. var sceneHierarchyWindowType = typeof(UnityEditor.SearchableEditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
  260. // bug 1242332: don't grab the focus!
  261. // The arguments aren't actually optional so they must all be named.
  262. //
  263. // todo: We should cache all the window-getting and reflection so it
  264. // happens not once per object but once per convert.
  265. var sceneHierarchyWindow = EditorWindow.GetWindow(
  266. t: sceneHierarchyWindowType,
  267. utility: false,
  268. title: null,
  269. focus: false);
  270. var instanceID = obj.GetInstanceID();
  271. var idFormat = "ref:{0}:";
  272. var sceneHierarchy = GetPropertyReflection(sceneHierarchyWindow, "sceneHierarchy", isPublic: true);
  273. var previousSearchFilter = sceneHierarchy.GetType().GetField("m_SearchFilter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sceneHierarchy);
  274. // Set the search filter to find all references in the scene to the given object
  275. var setSearchFilterMethod = sceneHierarchyWindowType.GetMethod("SetSearchFilter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
  276. setSearchFilterMethod.Invoke(sceneHierarchyWindow, new object[] { string.Format(idFormat, instanceID), SearchableEditorWindow.SearchMode.All, true, false });
  277. // Get objects from list of instance IDs of currently visible objects
  278. var treeView = GetPropertyReflection(sceneHierarchy, "treeView", isPublic: false);
  279. var data = GetPropertyReflection(treeView, "data", isPublic: true);
  280. var getRows = data.GetType().GetMethod("GetRows");
  281. var rows = getRows.Invoke(data, null) as IEnumerable;
  282. var sceneObjects = new List<GameObject>();
  283. foreach (var row in rows)
  284. {
  285. var id = (int)GetPropertyReflection(row, "id", isPublic: true);
  286. var gameObject = EditorUtility.InstanceIDToObject(id) as GameObject;
  287. if (gameObject)
  288. {
  289. sceneObjects.Add(gameObject);
  290. }
  291. }
  292. // remove the filter when done
  293. setSearchFilterMethod.Invoke(sceneHierarchyWindow, new object[] { previousSearchFilter, SearchableEditorWindow.SearchMode.Name, true, false });
  294. return sceneObjects;
  295. }
  296. /// <summary>
  297. /// Convert one object (and the hierarchy below it) to a prefab variant of a model prefab.
  298. ///
  299. /// Returns the prefab asset that's linked to the fbx.
  300. ///
  301. /// If 'toConvert' is:
  302. /// <list>
  303. /// <item>An object in the scene, then the hierarchy will be exported
  304. /// and a new prefab variant created pointing to the new fbx.</item>
  305. /// <item>The root of an fbx asset, or the root of an instance of an
  306. /// fbx asset, then a new prefab variant will be created
  307. /// pointing to the existing fbx.</item>
  308. /// <item>A prefab asset,
  309. /// then a new fbx asset will be exported and a new prefab variant created
  310. /// pointing to the fbx.</item>
  311. /// </list>
  312. /// </summary>
  313. /// <returns>The prefab variant linked to an fbx file.</returns>
  314. /// <param name="toConvert">Object to convert.</param>
  315. /// <param name="fbxFullPath">Absolute platform-specific path to
  316. /// the fbx file. If the file already exists, it will be overwritten.
  317. /// May be null, in which case we construct a unique filename.
  318. /// Ignored if 'toConvert' is an fbx asset or is an instance of
  319. /// one.</param>
  320. /// <param name="fbxDirectoryFullPath">Absolute platform-specific
  321. /// path to a directory in which to put the fbx file under a unique
  322. /// filename. May be null, in which case we use the export settings.
  323. /// Ignored if 'fbxFullPath' is specified. Ignored if 'toConvert' is
  324. /// an fbx asset or an instance of one.</param>
  325. /// <param name="prefabFullPath">Absolute platform-specific path to
  326. /// the prefab file. If the file already exists, it will be
  327. /// overwritten. May be null, in which case we construct a unique
  328. /// filename. Ignored if 'toConvert' is a prefab asset.</param>
  329. /// <param name="prefabDirectoryFullPath">Absolute
  330. /// platform-specific path to a directory in which to put the prefab
  331. /// file under a unique filename. May be null, in which case we use
  332. /// the export settings. Ignored if 'prefabFullPath' is specified.
  333. /// Ignored if 'toConvert' is a prefab asset.</param>
  334. [SecurityPermission(SecurityAction.LinkDemand)]
  335. public static GameObject Convert(
  336. GameObject toConvert,
  337. string fbxDirectoryFullPath = null,
  338. string fbxFullPath = null,
  339. string prefabDirectoryFullPath = null,
  340. string prefabFullPath = null,
  341. ConvertToPrefabSettingsSerialize exportOptions = null)
  342. {
  343. if (toConvert == null)
  344. {
  345. throw new System.ArgumentNullException("toConvert");
  346. }
  347. if (PrefabUtility.IsPartOfNonAssetPrefabInstance(toConvert) && !PrefabUtility.IsOutermostPrefabInstanceRoot(toConvert))
  348. {
  349. return null; // cannot convert in this scenario
  350. }
  351. // can't currently handle converting root of prefab in prefab preview scene
  352. if (SceneManagement.EditorSceneManager.IsPreviewSceneObject(toConvert) && toConvert.transform.parent == null)
  353. {
  354. return null;
  355. }
  356. // If we selected the something that's already backed by an
  357. // FBX, don't export.
  358. var mainAsset = GetOrCreateFbxAsset(toConvert, fbxDirectoryFullPath, fbxFullPath, exportOptions);
  359. // if toConvert is part of a prefab asset and not an instance, make it an instance in a preview scene
  360. // so that we can unpack it and avoid issues with nested prefab references.
  361. bool isPrefabAsset = false;
  362. UnityEngine.SceneManagement.Scene? previewScene = null;
  363. if(PrefabUtility.IsPartOfPrefabAsset(toConvert) && PrefabUtility.GetPrefabInstanceStatus(toConvert) == PrefabInstanceStatus.NotAPrefab)
  364. {
  365. previewScene = SceneManagement.EditorSceneManager.NewPreviewScene();
  366. toConvert = PrefabUtility.InstantiatePrefab(toConvert, previewScene.Value) as GameObject;
  367. isPrefabAsset = true;
  368. }
  369. // don't need to undo if we are converting a prefab asset
  370. if (!isPrefabAsset)
  371. {
  372. Undo.IncrementCurrentGroup();
  373. Undo.SetCurrentGroupName(string.Format(UndoConversionGroup, toConvert.name));
  374. }
  375. // if root is a prefab instance, unpack it. Unpack everything below as well
  376. if (PrefabUtility.GetPrefabInstanceStatus(toConvert) == PrefabInstanceStatus.Connected)
  377. {
  378. Undo.RegisterFullObjectHierarchyUndo(toConvert, "unpack prefab instance");
  379. PrefabUtility.UnpackPrefabInstance(toConvert, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
  380. }
  381. // create prefab variant from the fbx
  382. var fbxInstance = PrefabUtility.InstantiatePrefab(mainAsset) as GameObject;
  383. // replace hierarchy in the scene
  384. if (!isPrefabAsset && toConvert != null)
  385. {
  386. // don't worry about keeping the world position in the prefab, as we will fix the transform on the instance root
  387. fbxInstance.transform.SetParent(toConvert.transform.parent, worldPositionStays: false);
  388. fbxInstance.transform.SetSiblingIndex(toConvert.transform.GetSiblingIndex());
  389. }
  390. // copy components over
  391. UpdateFromSourceRecursive(fbxInstance, toConvert);
  392. // make sure we have a path for the prefab
  393. if (string.IsNullOrEmpty(prefabFullPath))
  394. {
  395. // Generate a unique filename.
  396. if (string.IsNullOrEmpty(prefabDirectoryFullPath))
  397. {
  398. prefabDirectoryFullPath = UnityEditor.Formats.Fbx.Exporter.ExportSettings.PrefabAbsoluteSavePath;
  399. }
  400. else
  401. {
  402. prefabDirectoryFullPath = Path.GetFullPath(prefabDirectoryFullPath);
  403. }
  404. var prefabBasename = ModelExporter.ConvertToValidFilename(toConvert.name + ".prefab");
  405. prefabFullPath = Path.Combine(prefabDirectoryFullPath, prefabBasename);
  406. if (File.Exists(prefabFullPath))
  407. {
  408. prefabFullPath = IncrementFileName(prefabDirectoryFullPath, prefabFullPath);
  409. }
  410. }
  411. // make sure the directory structure exists
  412. var dirName = Path.GetDirectoryName(prefabFullPath);
  413. if (!Directory.Exists(dirName))
  414. {
  415. Directory.CreateDirectory(dirName);
  416. }
  417. var prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(fbxInstance, ExportSettings.GetProjectRelativePath(prefabFullPath), InteractionMode.AutomatedAction);
  418. // replace hierarchy in the scene
  419. if (!isPrefabAsset && toConvert != null)
  420. {
  421. Undo.DestroyObjectImmediate(toConvert);
  422. Undo.RegisterCreatedObjectUndo(fbxInstance, UndoConversionCreateObject);
  423. SceneManagement.EditorSceneManager.MarkSceneDirty(fbxInstance.scene);
  424. Undo.IncrementCurrentGroup();
  425. return fbxInstance;
  426. }
  427. else
  428. {
  429. Undo.ClearUndo(toConvert);
  430. Undo.ClearUndo(fbxInstance);
  431. Object.DestroyImmediate(fbxInstance);
  432. Object.DestroyImmediate(toConvert);
  433. }
  434. if (previewScene.HasValue)
  435. {
  436. SceneManagement.EditorSceneManager.ClosePreviewScene(previewScene.Value);
  437. }
  438. return prefab;
  439. }
  440. /// <summary>
  441. /// Check whether <see>Convert</see> will be exporting an fbx file,
  442. /// or reusing one.
  443. /// </summary>
  444. public static bool WillExportFbx(GameObject toConvert)
  445. {
  446. return GetFbxAssetOrNull(toConvert) == null;
  447. }
  448. /// <summary>
  449. /// Return an FBX asset that corresponds to 'toConvert'.
  450. ///
  451. /// If 'toConvert' is the root of an FBX asset, return it.
  452. ///
  453. /// If it's an instance in a scene the points to the root of an FBX
  454. /// asset, return that asset.
  455. ///
  456. /// Otherwise, export according to the paths and options, and
  457. /// return the new asset.
  458. /// </summary>
  459. /// <param name="toConvert">GameObject for which we want an fbx asset</param>
  460. /// <param name="fbxDirectoryFullPath">Export will choose an
  461. /// appropriate filename in this directory. Ignored if fbxFullPath is
  462. /// set. Ignored if toConvert is an fbx asset or an instance of an
  463. /// fbx.</param>
  464. /// <param name="fbxDirectoryFullPath">Export will create this
  465. /// file. Overrides fbxDirectoryFullPath. Ignored if toConvert is an
  466. /// fbx asset or an instance of an fbx.</param>
  467. /// <returns>The root of a model prefab asset.</returns>
  468. internal static GameObject GetOrCreateFbxAsset(GameObject toConvert,
  469. string fbxDirectoryFullPath = null,
  470. string fbxFullPath = null,
  471. ConvertToPrefabSettingsSerialize exportOptions = null)
  472. {
  473. if (toConvert == null)
  474. {
  475. throw new System.ArgumentNullException("toConvert");
  476. }
  477. var mainAsset = GetFbxAssetOrNull(toConvert);
  478. if (mainAsset)
  479. {
  480. return mainAsset;
  481. }
  482. if (string.IsNullOrEmpty(fbxFullPath))
  483. {
  484. // Generate a unique filename.
  485. if (string.IsNullOrEmpty(fbxDirectoryFullPath))
  486. {
  487. fbxDirectoryFullPath = UnityEditor.Formats.Fbx.Exporter.ExportSettings.FbxAbsoluteSavePath;
  488. }
  489. else
  490. {
  491. fbxDirectoryFullPath = Path.GetFullPath(fbxDirectoryFullPath);
  492. }
  493. var fbxBasename = ModelExporter.ConvertToValidFilename(toConvert.name + ".fbx");
  494. fbxFullPath = Path.Combine(fbxDirectoryFullPath, fbxBasename);
  495. if (File.Exists(fbxFullPath))
  496. {
  497. fbxFullPath = IncrementFileName(fbxDirectoryFullPath, fbxFullPath);
  498. }
  499. }
  500. var projectRelativePath = ExportSettings.GetProjectRelativePath(fbxFullPath);
  501. // Make sure that the object names in the hierarchy are unique.
  502. // The import back in to Unity would do this automatically but
  503. // we prefer to control it so that the Maya artist can see the
  504. // same names as exist in Unity.
  505. EnforceUniqueNames(new GameObject[] { toConvert });
  506. // Export to FBX. It refreshes the database.
  507. {
  508. var fbxActualPath = ModelExporter.ExportObject(
  509. fbxFullPath, toConvert,
  510. exportOptions != null ? exportOptions : new ConvertToPrefabSettingsSerialize()
  511. );
  512. if (fbxActualPath != fbxFullPath)
  513. {
  514. throw new ConvertToNestedPrefabException("Failed to convert " + toConvert.name);
  515. }
  516. }
  517. // Replace w Model asset. LoadMainAssetAtPath wants a path
  518. // relative to the project, not relative to the assets folder.
  519. var unityMainAsset = AssetDatabase.LoadMainAssetAtPath(projectRelativePath) as GameObject;
  520. if (!unityMainAsset)
  521. {
  522. throw new ConvertToNestedPrefabException("Failed to convert " + toConvert.name);
  523. }
  524. return unityMainAsset;
  525. }
  526. /// <summary>
  527. /// Returns the fbx asset on disk corresponding to the same hierarchy as is selected.
  528. ///
  529. /// Returns go if go is the root of a model prefab.
  530. /// Returns the prefab parent of go if it's the root of a model prefab.
  531. /// Returns null in all other circumstances.
  532. /// </summary>
  533. /// <returns>The root of a model prefab asset, or null.</returns>
  534. /// <param name="go">A gameobject either in the scene or in the assets folder.</param>
  535. internal static GameObject GetFbxAssetOrNull(GameObject go)
  536. {
  537. // Children of model prefab instances will also have "model prefab instance"
  538. // as their prefab type, so it is important that it is the root that is selected.
  539. //
  540. // e.g. If I have the following hierarchy:
  541. // Cube
  542. // -- Sphere
  543. //
  544. // Both the Cube and Sphere will have ModelPrefab as their prefab type.
  545. // However, when selecting the Sphere to convert, we don't want to connect it to the
  546. // existing FBX but create a new FBX containing just the sphere.
  547. if (!PrefabUtility.IsPartOfModelPrefab(go))
  548. {
  549. return null;
  550. }
  551. PrefabInstanceStatus prefabStatus = PrefabUtility.GetPrefabInstanceStatus(go);
  552. switch (prefabStatus)
  553. {
  554. case PrefabInstanceStatus.Connected:
  555. // this is a prefab instance, get the object from source
  556. if (PrefabUtility.IsOutermostPrefabInstanceRoot(go))
  557. {
  558. return PrefabUtility.GetCorrespondingObjectFromSource(go) as GameObject;
  559. }
  560. else
  561. {
  562. return null;
  563. }
  564. case PrefabInstanceStatus.NotAPrefab:
  565. // a prefab asset
  566. if(go.transform.root.gameObject == go)
  567. {
  568. return go;
  569. }
  570. else
  571. {
  572. return null;
  573. }
  574. default:
  575. return null;
  576. }
  577. }
  578. /// <summary>
  579. /// Check if the file exists, and if it does, then increment the name.
  580. /// e.g. if filename is Sphere.fbx and it already exists, change it to Sphere 1.fbx.
  581. /// </summary>
  582. /// <returns>new file name.</returns>
  583. /// <param name="filename">Filename.</param>
  584. public static string IncrementFileName(string path, string filename)
  585. {
  586. string fileWithoutExt = Path.GetFileNameWithoutExtension(filename);
  587. string ext = Path.GetExtension(filename);
  588. // file, space, number, extension.
  589. string format = "{0} {1}{2}";
  590. int index = 1;
  591. // try extracting the current index from the name and incrementing it
  592. var result = System.Text.RegularExpressions.Regex.Match(fileWithoutExt, @"\d+$");
  593. if (result != null)
  594. {
  595. var number = result.Value;
  596. // Parse the number.
  597. int tempIndex;
  598. if (int.TryParse(number, out tempIndex))
  599. {
  600. fileWithoutExt = fileWithoutExt.Remove(fileWithoutExt.LastIndexOf(number));
  601. // Change the format to remove the extra space we'd add
  602. // if there weren't already a number. Also, try to use the
  603. // same width (so Cube001 increments to Cube002, not Cube2).
  604. format = "{0}{1:D" + number.Length + "}{2}"; // file, number with padding, extension
  605. index = tempIndex + 1;
  606. }
  607. }
  608. string file = null;
  609. do
  610. {
  611. file = string.Format(format, fileWithoutExt, index, ext);
  612. file = Path.Combine(path, file);
  613. index++;
  614. } while (File.Exists(file));
  615. return file;
  616. }
  617. /// <summary>
  618. /// Enforces that all object names be unique before exporting.
  619. /// If an object with a duplicate name is found, then it is incremented.
  620. /// e.g. Sphere becomes Sphere 1
  621. /// </summary>
  622. /// <param name="exportSet">Export set.</param>
  623. public static void EnforceUniqueNames(IEnumerable<GameObject> exportSet)
  624. {
  625. Dictionary<string, int> NameToIndexMap = new Dictionary<string, int>();
  626. string format = "{0} {1}";
  627. Queue<GameObject> queue = new Queue<GameObject>(exportSet);
  628. while (queue.Count > 0)
  629. {
  630. var go = queue.Dequeue();
  631. var name = go.name;
  632. if (NameToIndexMap.ContainsKey(name))
  633. {
  634. go.name = string.Format(format, name, NameToIndexMap[name]);
  635. NameToIndexMap[name]++;
  636. }
  637. else
  638. {
  639. NameToIndexMap[name] = 1;
  640. }
  641. foreach (Transform child in go.transform)
  642. {
  643. queue.Enqueue(child.gameObject);
  644. }
  645. }
  646. }
  647. /// <summary>
  648. /// Updates the meshes and materials of the exported GameObjects
  649. /// to link to those imported from the FBX.
  650. /// </summary>
  651. /// <param name="dest">GameObject to update.</param>
  652. /// <param name="source">Source to update from.</param>
  653. internal static void UpdateFromSourceRecursive(GameObject dest, GameObject source)
  654. {
  655. // recurse over orig, for each transform finding the corresponding transform in the FBX
  656. // and copying the meshes and materials over from the FBX
  657. var goDict = MapNameToSourceRecursive(source, dest);
  658. var q = new Queue<Transform>();
  659. q.Enqueue(source.transform);
  660. while (q.Count > 0)
  661. {
  662. var t = q.Dequeue();
  663. if (goDict[t.name] == null)
  664. {
  665. Debug.LogWarning(string.Format("Warning: Could not find Object {0} in FBX", t.name));
  666. continue;
  667. }
  668. var destGO = goDict[t.name];
  669. var sourceGO = t.gameObject;
  670. if (PrefabUtility.GetPrefabInstanceStatus(sourceGO) == PrefabInstanceStatus.Connected)
  671. {
  672. Undo.RegisterFullObjectHierarchyUndo(sourceGO, "unpack prefab instance");
  673. PrefabUtility.UnpackPrefabInstance(sourceGO, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
  674. }
  675. FixSceneReferences(sourceGO, destGO, source);
  676. CopyComponents(destGO, sourceGO, source, goDict);
  677. // also make sure GameObject properties, such as tag and layer
  678. // are copied over as well
  679. destGO.SetActive(sourceGO.activeSelf);
  680. destGO.isStatic = sourceGO.isStatic;
  681. destGO.layer = sourceGO.layer;
  682. destGO.tag = sourceGO.tag;
  683. GameObjectUtility.SetStaticEditorFlags(destGO, GameObjectUtility.GetStaticEditorFlags(sourceGO));
  684. // set icon
  685. System.Reflection.BindingFlags bindingFlags = System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic;
  686. var sourceIcon = typeof(EditorGUIUtility).InvokeMember("GetIconForObject", bindingFlags, null, null, new object[] { sourceGO });
  687. object[] args = new object[] { destGO, sourceIcon };
  688. typeof(EditorGUIUtility).InvokeMember("SetIconForObject", bindingFlags, null, null, args);
  689. foreach (Transform child in t)
  690. {
  691. q.Enqueue(child);
  692. }
  693. }
  694. }
  695. /// <summary>
  696. /// Gets a dictionary linking dest GameObject name to source game object.
  697. ///
  698. /// Before export we ensure that the hierarchy has unique names, so that we can map by name afterwards.
  699. /// </summary>
  700. /// <returns>Dictionary containing the name to source game object.</returns>
  701. /// <param name="dest">Destination GameObject.</param>
  702. /// <param name="source">Source GameObject.</param>
  703. internal static Dictionary<string, GameObject> MapNameToSourceRecursive(GameObject dest, GameObject source)
  704. {
  705. var nameToGO = new Dictionary<string, GameObject>();
  706. var q = new Queue<Transform>();
  707. q.Enqueue(dest.transform);
  708. while (q.Count > 0)
  709. {
  710. var t = q.Dequeue();
  711. nameToGO[t.name] = null;
  712. foreach (Transform child in t)
  713. {
  714. q.Enqueue(child);
  715. }
  716. }
  717. nameToGO[dest.name] = source;
  718. var fbxQ = new Queue<Transform>();
  719. foreach (Transform child in source.transform)
  720. {
  721. fbxQ.Enqueue(child);
  722. }
  723. while (fbxQ.Count > 0)
  724. {
  725. var t = fbxQ.Dequeue();
  726. if (!nameToGO.ContainsKey(t.name))
  727. {
  728. Debug.LogWarning(string.Format("Warning: {0} in FBX but not in converted hierarchy", t.name));
  729. continue;
  730. }
  731. nameToGO[t.name] = t.gameObject;
  732. foreach (Transform child in t)
  733. {
  734. fbxQ.Enqueue(child);
  735. }
  736. }
  737. return nameToGO;
  738. }
  739. /// <summary>
  740. /// Copy the object reference from fromProperty to the matching property on serializedObject.
  741. /// Use nameMap to find the correct object reference to use.
  742. /// </summary>
  743. /// <param name="serializedObject"></param>
  744. /// <param name="fromProperty"></param>
  745. /// <param name="nameMap"></param>
  746. internal static void CopySerializedProperty(SerializedObject serializedObject, SerializedProperty fromProperty, Dictionary<string, GameObject> nameMap)
  747. {
  748. var toProperty = serializedObject.FindProperty(fromProperty.propertyPath);
  749. GameObject value;
  750. if (nameMap.TryGetValue(fromProperty.objectReferenceValue.name, out value))
  751. {
  752. if (fromProperty.objectReferenceValue is GameObject)
  753. {
  754. toProperty.objectReferenceValue = value;
  755. }
  756. else
  757. {
  758. toProperty.objectReferenceValue = value.GetComponent(fromProperty.objectReferenceValue.GetType());
  759. }
  760. serializedObject.ApplyModifiedProperties();
  761. }
  762. else
  763. {
  764. // try to make sure any references in the scene are maintained for prefab instances
  765. toProperty.objectReferenceValue = fromProperty.objectReferenceValue;
  766. serializedObject.ApplyModifiedProperties();
  767. }
  768. }
  769. /// <summary>
  770. /// Copy components on the 'from' object which is the object being converted,
  771. /// over to the 'to' object which is the FBX.
  772. ///
  773. /// Copy over everything except meshes and materials, since these
  774. /// are already in the FBX.
  775. ///
  776. /// The 'from' hierarchy is not modified.
  777. ///
  778. /// Note: 'root' is the root object that is being converted
  779. /// </summary>
  780. internal static void CopyComponents(GameObject to, GameObject from, GameObject root, Dictionary<string, GameObject> nameMap)
  781. {
  782. // copy components on "from" to "to". Don't want to copy over meshes and materials that were exported
  783. var originalComponents = new List<Component>(from.GetComponents<Component>());
  784. var destinationComponents = new List<Component>(to.GetComponents<Component>());
  785. foreach(var fromComponent in originalComponents)
  786. {
  787. // ignore missing components
  788. if (fromComponent == null)
  789. {
  790. continue;
  791. }
  792. // ignore MeshFilter and Transform, but still ensure scene references are maintained.
  793. // Don't need to copy regular transform (except for the root object) as the values should already be correct in the FBX.
  794. // Furthermore, copying transform values may result in overrides in the prefab, which is undesired as if
  795. // the transform is updated in the FBX, it won't be in the prefab.
  796. if (fromComponent is MeshFilter || (fromComponent is Transform && from != root))
  797. {
  798. FixSceneReferences(fromComponent, to.GetComponent(fromComponent.GetType()), root);
  799. continue;
  800. }
  801. // ignore FbxPrefab (when converting LinkedPrefabs)
  802. // Also ignore RectTransform, since it is not currently possible to switch transforms
  803. // in a prefab.
  804. if (fromComponent is UnityEngine.Formats.Fbx.Exporter.FbxPrefab ||
  805. fromComponent is RectTransform)
  806. {
  807. continue;
  808. }
  809. var json = EditorJsonUtility.ToJson(fromComponent);
  810. if (string.IsNullOrEmpty(json))
  811. {
  812. // this happens for missing scripts
  813. continue;
  814. }
  815. System.Type expectedType = fromComponent.GetType();
  816. Component toComponent = null;
  817. // Find the component to copy to.
  818. for (int i = 0, n = destinationComponents.Count; i < n; i++)
  819. {
  820. // ignore missing components
  821. if (destinationComponents[i] == null)
  822. {
  823. continue;
  824. }
  825. if (destinationComponents[i].GetType() == expectedType)
  826. {
  827. // We have found the component we are looking for,
  828. // remove it so we don't try to copy to it again
  829. toComponent = destinationComponents[i];
  830. destinationComponents.RemoveAt(i);
  831. break;
  832. }
  833. }
  834. // If it's a particle system renderer, then check to see if it hasn't already
  835. // been added when adding the particle system.
  836. // An object can have only one ParticleSystem so there shouldn't be an issue of the renderer
  837. // belonging to a different ParticleSystem.
  838. if(!toComponent && fromComponent is ParticleSystemRenderer)
  839. {
  840. toComponent = to.GetComponent<ParticleSystemRenderer>();
  841. }
  842. if (!toComponent)
  843. {
  844. toComponent = to.AddComponent(fromComponent.GetType());
  845. }
  846. if (!toComponent)
  847. {
  848. // Failed to add component
  849. Debug.LogWarningFormat("{0}: Failed to add component of type {1} to converted object", ModelExporter.PACKAGE_UI_NAME, fromComponent.GetType().Name);
  850. continue;
  851. }
  852. FixSceneReferences(fromComponent, toComponent, root);
  853. // SkinnedMeshRenderer also stores the mesh.
  854. // Make sure this is not copied over when the SkinnedMeshRenderer is updated,
  855. // as we want to keep the mesh from the FBX not the scene.
  856. if (fromComponent is SkinnedMeshRenderer)
  857. {
  858. var skinnedMesh = toComponent as SkinnedMeshRenderer;
  859. var mesh = skinnedMesh.sharedMesh;
  860. EditorJsonUtility.FromJsonOverwrite(json, toComponent);
  861. skinnedMesh.sharedMesh = mesh;
  862. }
  863. else
  864. {
  865. EditorJsonUtility.FromJsonOverwrite(json, toComponent);
  866. }
  867. if(fromComponent is MeshCollider)
  868. {
  869. // UNI-27534: This fixes the issue where the mesh collider would not update to point to the mesh in the fbx after export
  870. // Point the mesh included in the mesh collider to the mesh in the FBX file, which is the same as the one in mesh filter
  871. var fromMeshCollider = from.GetComponent<MeshCollider>();
  872. var fromMeshFilter = from.GetComponent<MeshFilter>();
  873. // if the mesh collider isn't pointing to the same mesh as in the current mesh filter then don't
  874. // do anything as it's probably pointing to a mesh in a different fbx
  875. if (fromMeshCollider && fromMeshFilter && fromMeshCollider.sharedMesh == fromMeshFilter.sharedMesh)
  876. {
  877. var toFilter = to.GetComponent<MeshFilter>();
  878. if (toFilter)
  879. {
  880. var toMeshCollider = toComponent as MeshCollider;
  881. toMeshCollider.sharedMesh = toFilter.sharedMesh;
  882. }
  883. }
  884. }
  885. var serializedFromComponent = new SerializedObject(fromComponent);
  886. var serializedToComponent = new SerializedObject(toComponent);
  887. var fromProperty = serializedFromComponent.GetIterator();
  888. fromProperty.Next(true); // skip generic field
  889. // For SkinnedMeshRenderer, the bones array doesn't have visible children, but still needs to be copied over.
  890. // For everything else, filtering by visible children in the while loop and then copying properties that don't have visible children,
  891. // ensures that only the leaf properties are copied over. Copying other properties is not usually necessary and may break references that
  892. // were not meant to be copied.
  893. while (fromProperty.Next((fromComponent is SkinnedMeshRenderer)? fromProperty.hasChildren : fromProperty.hasVisibleChildren))
  894. {
  895. if (!fromProperty.hasVisibleChildren)
  896. {
  897. // with Undo operations, copying m_Father reference causes issues. Also, it is not required as the reference is fixed when
  898. // the transform is parented under the correct hierarchy (which happens before this).
  899. if (fromProperty.propertyType == SerializedPropertyType.ObjectReference && fromProperty.propertyPath != "m_GameObject" &&
  900. fromProperty.propertyPath != "m_Father" && fromProperty.objectReferenceValue &&
  901. (fromProperty.objectReferenceValue is GameObject || fromProperty.objectReferenceValue is Component))
  902. {
  903. CopySerializedProperty(serializedToComponent, fromProperty, nameMap);
  904. }
  905. }
  906. }
  907. }
  908. }
  909. }
  910. }