12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.Linq;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Security.Permissions;
- namespace UnityEditor.Formats.Fbx.Exporter
- {
- [System.Serializable]
- internal class ConvertToNestedPrefabException : System.Exception
- {
- public ConvertToNestedPrefabException()
- {
- }
- public ConvertToNestedPrefabException(string message)
- : base(message)
- {
- }
- public ConvertToNestedPrefabException(string message, System.Exception inner)
- : base(message, inner)
- {
- }
- protected ConvertToNestedPrefabException(SerializationInfo info, StreamingContext context)
- : base(info, context)
- {
- }
- }
- internal static class ConvertToNestedPrefab
- {
- const string GameObjectMenuItemName = "GameObject/Convert To FBX Prefab Variant...";
- const string AssetsMenuItemName = "Assets/Convert To FBX Prefab Variant...";
- const string UndoConversionGroup = "Convert {0} to FBX Prefab Variant";
- internal const string UndoConversionCreateObject = "Convert to FBX Prefab Variant";
-
-
-
-
-
-
-
-
-
- [MenuItem(GameObjectMenuItemName, false, 30)]
- static void OnGameObjectContextItem(MenuCommand command)
- {
- OnContextItem(command, SelectionMode.Editable | SelectionMode.TopLevel);
- }
- [MenuItem(AssetsMenuItemName, false, 30)]
- static void OnAssetsContextItem(MenuCommand command)
- {
- OnContextItem(command, SelectionMode.Assets);
- }
- static void OnContextItem(MenuCommand command, SelectionMode mode)
- {
- GameObject[] selection = null;
- if (command == null || command.context == null)
- {
-
- selection = Selection.GetFiltered<GameObject>(mode);
- }
- else
- {
-
- var selected = command.context as GameObject;
- if (selected)
- {
- selection = new GameObject[] { selected };
- }
- }
- if (selection == null || selection.Length == 0)
- {
- ModelExporter.DisplayNoSelectionDialog();
- return;
- }
- Selection.objects = CreateInstantiatedModelPrefab(selection);
- }
- internal static void DisplayInvalidSelectionDialog(GameObject toConvert, string message = "")
- {
- UnityEditor.EditorUtility.DisplayDialog(
- string.Format("{0} Warning", "FBX Exporter"),
- string.Format("Failed to Convert: {0}\n{1}", toConvert.name, message),
- "Ok");
- }
-
-
-
- [MenuItem(GameObjectMenuItemName, true, 30)]
- [MenuItem(AssetsMenuItemName, true, 30)]
- public static bool OnValidateMenuItem()
- {
- return true;
- }
-
-
-
- public static ExportSettings ExportSettings
- {
- get { return ExportSettings.instance; }
- }
-
-
-
-
-
- internal static bool SetContainsOnlyPrefabAssets(Object[] toConvert)
- {
- foreach (var obj in toConvert)
- {
- var go = ModelExporter.GetGameObject(obj);
- if (go != null && !PrefabUtility.IsPartOfPrefabAsset(go))
- {
-
-
- return false;
- }
- }
- return true;
- }
-
-
-
-
-
-
-
-
-
-
- [SecurityPermission(SecurityAction.LinkDemand)]
- public static GameObject[] CreateInstantiatedModelPrefab(
- GameObject[] unityGameObjectsToConvert)
- {
- var toExport = ModelExporter.RemoveRedundantObjects(unityGameObjectsToConvert);
- if (ExportSettings.instance.ShowConvertToPrefabDialog)
- {
- if (toExport.Count == 1)
- {
- var go = toExport.First();
- if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go) && !PrefabUtility.IsOutermostPrefabInstanceRoot(go))
- {
- DisplayInvalidSelectionDialog(go,
- "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");
- return null;
- }
- if (PrefabUtility.IsPartOfPrefabAsset(go) && go.transform.parent != null)
- {
- DisplayInvalidSelectionDialog(go,
- "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");
- return null;
- }
-
- if (SceneManagement.EditorSceneManager.IsPreviewSceneObject(go) && go.transform.parent == null)
- {
- DisplayInvalidSelectionDialog(go,
- "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");
- return null;
- }
- }
- ConvertToPrefabEditorWindow.Init(toExport);
- return toExport.ToArray();
- }
- bool onlyPrefabAssets = ConvertToNestedPrefab.SetContainsOnlyPrefabAssets(unityGameObjectsToConvert);
- int groupIndex = -1;
-
-
- if (!onlyPrefabAssets)
- {
- Undo.IncrementCurrentGroup();
- groupIndex = Undo.GetCurrentGroup();
- Undo.SetCurrentGroupName(UndoConversionCreateObject);
- }
- var converted = new List<GameObject>();
- var exportOptions = ExportSettings.instance.ConvertToPrefabSettings.info;
- foreach (var go in toExport)
- {
- var convertedGO = Convert(go, exportOptions: exportOptions);
- if (convertedGO != null)
- {
- converted.Add(convertedGO);
- }
- }
- if (!onlyPrefabAssets && groupIndex >= 0)
- {
- Undo.CollapseUndoOperations(groupIndex);
- Undo.IncrementCurrentGroup();
- }
- return converted.ToArray();
- }
-
-
-
-
-
-
-
-
-
-
- internal static void FixSceneReferences(Object origObj, Object newObj, GameObject toConvertRoot)
- {
- var sceneObjs = GetSceneReferencesToObject(origObj);
-
- foreach(var sceneObj in sceneObjs)
- {
- var go = ModelExporter.GetGameObject(sceneObj);
- if (go && go.transform.IsChildOf(toConvertRoot.transform))
- {
-
- continue;
- }
- var components = sceneObj.GetComponents<Component>();
- foreach(var component in components)
- {
- var serializedComponent = new SerializedObject(component);
- var property = serializedComponent.GetIterator();
- property.Next(true);
-
-
-
-
- while (property.Next((component is SkinnedMeshRenderer) ? property.hasChildren : property.hasVisibleChildren))
- {
- if (!property.hasVisibleChildren)
- {
-
-
- if (property.propertyType == SerializedPropertyType.ObjectReference && property.propertyPath != "m_GameObject" &&
- property.propertyPath != "m_Father" && property.objectReferenceValue &&
- (property.objectReferenceValue == origObj))
- {
- property.objectReferenceValue = newObj;
- serializedComponent.ApplyModifiedProperties();
- }
- }
- }
- }
- }
- }
-
-
-
-
-
-
-
- private static object GetPropertyReflection(object instance, string propertyName, bool isPublic)
- {
- return instance.GetType().GetProperty(propertyName, (isPublic ? System.Reflection.BindingFlags.Public : System.Reflection.BindingFlags.NonPublic) |
- System.Reflection.BindingFlags.Instance).GetValue(instance, null);
- }
-
-
-
-
-
- internal static List<GameObject> GetSceneReferencesToObject(Object obj)
- {
- var sceneHierarchyWindowType = typeof(UnityEditor.SearchableEditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
-
-
-
-
-
- var sceneHierarchyWindow = EditorWindow.GetWindow(
- t: sceneHierarchyWindowType,
- utility: false,
- title: null,
- focus: false);
- var instanceID = obj.GetInstanceID();
- var idFormat = "ref:{0}:";
- var sceneHierarchy = GetPropertyReflection(sceneHierarchyWindow, "sceneHierarchy", isPublic: true);
- var previousSearchFilter = sceneHierarchy.GetType().GetField("m_SearchFilter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(sceneHierarchy);
-
- var setSearchFilterMethod = sceneHierarchyWindowType.GetMethod("SetSearchFilter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
- setSearchFilterMethod.Invoke(sceneHierarchyWindow, new object[] { string.Format(idFormat, instanceID), SearchableEditorWindow.SearchMode.All, true, false });
-
- var treeView = GetPropertyReflection(sceneHierarchy, "treeView", isPublic: false);
- var data = GetPropertyReflection(treeView, "data", isPublic: true);
- var getRows = data.GetType().GetMethod("GetRows");
- var rows = getRows.Invoke(data, null) as IEnumerable;
- var sceneObjects = new List<GameObject>();
- foreach (var row in rows)
- {
- var id = (int)GetPropertyReflection(row, "id", isPublic: true);
- var gameObject = EditorUtility.InstanceIDToObject(id) as GameObject;
- if (gameObject)
- {
- sceneObjects.Add(gameObject);
- }
- }
-
- setSearchFilterMethod.Invoke(sceneHierarchyWindow, new object[] { previousSearchFilter, SearchableEditorWindow.SearchMode.Name, true, false });
- return sceneObjects;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [SecurityPermission(SecurityAction.LinkDemand)]
- public static GameObject Convert(
- GameObject toConvert,
- string fbxDirectoryFullPath = null,
- string fbxFullPath = null,
- string prefabDirectoryFullPath = null,
- string prefabFullPath = null,
- ConvertToPrefabSettingsSerialize exportOptions = null)
- {
- if (toConvert == null)
- {
- throw new System.ArgumentNullException("toConvert");
- }
- if (PrefabUtility.IsPartOfNonAssetPrefabInstance(toConvert) && !PrefabUtility.IsOutermostPrefabInstanceRoot(toConvert))
- {
- return null;
- }
-
- if (SceneManagement.EditorSceneManager.IsPreviewSceneObject(toConvert) && toConvert.transform.parent == null)
- {
- return null;
- }
-
-
- var mainAsset = GetOrCreateFbxAsset(toConvert, fbxDirectoryFullPath, fbxFullPath, exportOptions);
-
-
- bool isPrefabAsset = false;
- UnityEngine.SceneManagement.Scene? previewScene = null;
- if(PrefabUtility.IsPartOfPrefabAsset(toConvert) && PrefabUtility.GetPrefabInstanceStatus(toConvert) == PrefabInstanceStatus.NotAPrefab)
- {
- previewScene = SceneManagement.EditorSceneManager.NewPreviewScene();
- toConvert = PrefabUtility.InstantiatePrefab(toConvert, previewScene.Value) as GameObject;
- isPrefabAsset = true;
- }
-
- if (!isPrefabAsset)
- {
- Undo.IncrementCurrentGroup();
- Undo.SetCurrentGroupName(string.Format(UndoConversionGroup, toConvert.name));
- }
-
- if (PrefabUtility.GetPrefabInstanceStatus(toConvert) == PrefabInstanceStatus.Connected)
- {
- Undo.RegisterFullObjectHierarchyUndo(toConvert, "unpack prefab instance");
- PrefabUtility.UnpackPrefabInstance(toConvert, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
- }
-
- var fbxInstance = PrefabUtility.InstantiatePrefab(mainAsset) as GameObject;
-
- if (!isPrefabAsset && toConvert != null)
- {
-
- fbxInstance.transform.SetParent(toConvert.transform.parent, worldPositionStays: false);
- fbxInstance.transform.SetSiblingIndex(toConvert.transform.GetSiblingIndex());
- }
-
- UpdateFromSourceRecursive(fbxInstance, toConvert);
-
- if (string.IsNullOrEmpty(prefabFullPath))
- {
-
- if (string.IsNullOrEmpty(prefabDirectoryFullPath))
- {
- prefabDirectoryFullPath = UnityEditor.Formats.Fbx.Exporter.ExportSettings.PrefabAbsoluteSavePath;
- }
- else
- {
- prefabDirectoryFullPath = Path.GetFullPath(prefabDirectoryFullPath);
- }
- var prefabBasename = ModelExporter.ConvertToValidFilename(toConvert.name + ".prefab");
- prefabFullPath = Path.Combine(prefabDirectoryFullPath, prefabBasename);
- if (File.Exists(prefabFullPath))
- {
- prefabFullPath = IncrementFileName(prefabDirectoryFullPath, prefabFullPath);
- }
- }
-
- var dirName = Path.GetDirectoryName(prefabFullPath);
- if (!Directory.Exists(dirName))
- {
- Directory.CreateDirectory(dirName);
- }
- var prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(fbxInstance, ExportSettings.GetProjectRelativePath(prefabFullPath), InteractionMode.AutomatedAction);
-
- if (!isPrefabAsset && toConvert != null)
- {
- Undo.DestroyObjectImmediate(toConvert);
- Undo.RegisterCreatedObjectUndo(fbxInstance, UndoConversionCreateObject);
- SceneManagement.EditorSceneManager.MarkSceneDirty(fbxInstance.scene);
-
- Undo.IncrementCurrentGroup();
- return fbxInstance;
- }
- else
- {
- Undo.ClearUndo(toConvert);
- Undo.ClearUndo(fbxInstance);
- Object.DestroyImmediate(fbxInstance);
- Object.DestroyImmediate(toConvert);
- }
- if (previewScene.HasValue)
- {
- SceneManagement.EditorSceneManager.ClosePreviewScene(previewScene.Value);
- }
- return prefab;
- }
-
-
-
-
- public static bool WillExportFbx(GameObject toConvert)
- {
- return GetFbxAssetOrNull(toConvert) == null;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- internal static GameObject GetOrCreateFbxAsset(GameObject toConvert,
- string fbxDirectoryFullPath = null,
- string fbxFullPath = null,
- ConvertToPrefabSettingsSerialize exportOptions = null)
- {
- if (toConvert == null)
- {
- throw new System.ArgumentNullException("toConvert");
- }
- var mainAsset = GetFbxAssetOrNull(toConvert);
- if (mainAsset)
- {
- return mainAsset;
- }
- if (string.IsNullOrEmpty(fbxFullPath))
- {
-
- if (string.IsNullOrEmpty(fbxDirectoryFullPath))
- {
- fbxDirectoryFullPath = UnityEditor.Formats.Fbx.Exporter.ExportSettings.FbxAbsoluteSavePath;
- }
- else
- {
- fbxDirectoryFullPath = Path.GetFullPath(fbxDirectoryFullPath);
- }
- var fbxBasename = ModelExporter.ConvertToValidFilename(toConvert.name + ".fbx");
- fbxFullPath = Path.Combine(fbxDirectoryFullPath, fbxBasename);
- if (File.Exists(fbxFullPath))
- {
- fbxFullPath = IncrementFileName(fbxDirectoryFullPath, fbxFullPath);
- }
- }
- var projectRelativePath = ExportSettings.GetProjectRelativePath(fbxFullPath);
-
-
-
-
- EnforceUniqueNames(new GameObject[] { toConvert });
-
- {
- var fbxActualPath = ModelExporter.ExportObject(
- fbxFullPath, toConvert,
- exportOptions != null ? exportOptions : new ConvertToPrefabSettingsSerialize()
- );
- if (fbxActualPath != fbxFullPath)
- {
- throw new ConvertToNestedPrefabException("Failed to convert " + toConvert.name);
- }
- }
-
-
- var unityMainAsset = AssetDatabase.LoadMainAssetAtPath(projectRelativePath) as GameObject;
- if (!unityMainAsset)
- {
- throw new ConvertToNestedPrefabException("Failed to convert " + toConvert.name);
- }
- return unityMainAsset;
- }
-
-
-
-
-
-
-
-
-
- internal static GameObject GetFbxAssetOrNull(GameObject go)
- {
-
-
-
-
-
-
-
-
-
-
- if (!PrefabUtility.IsPartOfModelPrefab(go))
- {
- return null;
- }
- PrefabInstanceStatus prefabStatus = PrefabUtility.GetPrefabInstanceStatus(go);
- switch (prefabStatus)
- {
- case PrefabInstanceStatus.Connected:
-
- if (PrefabUtility.IsOutermostPrefabInstanceRoot(go))
- {
- return PrefabUtility.GetCorrespondingObjectFromSource(go) as GameObject;
- }
- else
- {
- return null;
- }
- case PrefabInstanceStatus.NotAPrefab:
-
- if(go.transform.root.gameObject == go)
- {
- return go;
- }
- else
- {
- return null;
- }
- default:
- return null;
- }
- }
-
-
-
-
-
-
- public static string IncrementFileName(string path, string filename)
- {
- string fileWithoutExt = Path.GetFileNameWithoutExtension(filename);
- string ext = Path.GetExtension(filename);
-
- string format = "{0} {1}{2}";
- int index = 1;
-
- var result = System.Text.RegularExpressions.Regex.Match(fileWithoutExt, @"\d+$");
- if (result != null)
- {
- var number = result.Value;
-
- int tempIndex;
- if (int.TryParse(number, out tempIndex))
- {
- fileWithoutExt = fileWithoutExt.Remove(fileWithoutExt.LastIndexOf(number));
-
-
-
- format = "{0}{1:D" + number.Length + "}{2}";
- index = tempIndex + 1;
- }
- }
- string file = null;
- do
- {
- file = string.Format(format, fileWithoutExt, index, ext);
- file = Path.Combine(path, file);
- index++;
- } while (File.Exists(file));
- return file;
- }
-
-
-
-
-
-
- public static void EnforceUniqueNames(IEnumerable<GameObject> exportSet)
- {
- Dictionary<string, int> NameToIndexMap = new Dictionary<string, int>();
- string format = "{0} {1}";
- Queue<GameObject> queue = new Queue<GameObject>(exportSet);
- while (queue.Count > 0)
- {
- var go = queue.Dequeue();
- var name = go.name;
- if (NameToIndexMap.ContainsKey(name))
- {
- go.name = string.Format(format, name, NameToIndexMap[name]);
- NameToIndexMap[name]++;
- }
- else
- {
- NameToIndexMap[name] = 1;
- }
- foreach (Transform child in go.transform)
- {
- queue.Enqueue(child.gameObject);
- }
- }
- }
-
-
-
-
-
-
- internal static void UpdateFromSourceRecursive(GameObject dest, GameObject source)
- {
-
-
- var goDict = MapNameToSourceRecursive(source, dest);
- var q = new Queue<Transform>();
- q.Enqueue(source.transform);
- while (q.Count > 0)
- {
- var t = q.Dequeue();
- if (goDict[t.name] == null)
- {
- Debug.LogWarning(string.Format("Warning: Could not find Object {0} in FBX", t.name));
- continue;
- }
- var destGO = goDict[t.name];
- var sourceGO = t.gameObject;
- if (PrefabUtility.GetPrefabInstanceStatus(sourceGO) == PrefabInstanceStatus.Connected)
- {
- Undo.RegisterFullObjectHierarchyUndo(sourceGO, "unpack prefab instance");
- PrefabUtility.UnpackPrefabInstance(sourceGO, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
- }
- FixSceneReferences(sourceGO, destGO, source);
- CopyComponents(destGO, sourceGO, source, goDict);
-
-
- destGO.SetActive(sourceGO.activeSelf);
- destGO.isStatic = sourceGO.isStatic;
- destGO.layer = sourceGO.layer;
- destGO.tag = sourceGO.tag;
- GameObjectUtility.SetStaticEditorFlags(destGO, GameObjectUtility.GetStaticEditorFlags(sourceGO));
-
- System.Reflection.BindingFlags bindingFlags = System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic;
- var sourceIcon = typeof(EditorGUIUtility).InvokeMember("GetIconForObject", bindingFlags, null, null, new object[] { sourceGO });
- object[] args = new object[] { destGO, sourceIcon };
- typeof(EditorGUIUtility).InvokeMember("SetIconForObject", bindingFlags, null, null, args);
- foreach (Transform child in t)
- {
- q.Enqueue(child);
- }
- }
- }
-
-
-
-
-
-
-
-
- internal static Dictionary<string, GameObject> MapNameToSourceRecursive(GameObject dest, GameObject source)
- {
- var nameToGO = new Dictionary<string, GameObject>();
- var q = new Queue<Transform>();
- q.Enqueue(dest.transform);
- while (q.Count > 0)
- {
- var t = q.Dequeue();
- nameToGO[t.name] = null;
- foreach (Transform child in t)
- {
- q.Enqueue(child);
- }
- }
- nameToGO[dest.name] = source;
- var fbxQ = new Queue<Transform>();
- foreach (Transform child in source.transform)
- {
- fbxQ.Enqueue(child);
- }
- while (fbxQ.Count > 0)
- {
- var t = fbxQ.Dequeue();
- if (!nameToGO.ContainsKey(t.name))
- {
- Debug.LogWarning(string.Format("Warning: {0} in FBX but not in converted hierarchy", t.name));
- continue;
- }
- nameToGO[t.name] = t.gameObject;
- foreach (Transform child in t)
- {
- fbxQ.Enqueue(child);
- }
- }
- return nameToGO;
- }
-
-
-
-
-
-
-
-
- internal static void CopySerializedProperty(SerializedObject serializedObject, SerializedProperty fromProperty, Dictionary<string, GameObject> nameMap)
- {
- var toProperty = serializedObject.FindProperty(fromProperty.propertyPath);
- GameObject value;
- if (nameMap.TryGetValue(fromProperty.objectReferenceValue.name, out value))
- {
- if (fromProperty.objectReferenceValue is GameObject)
- {
- toProperty.objectReferenceValue = value;
- }
- else
- {
- toProperty.objectReferenceValue = value.GetComponent(fromProperty.objectReferenceValue.GetType());
- }
- serializedObject.ApplyModifiedProperties();
- }
- else
- {
-
- toProperty.objectReferenceValue = fromProperty.objectReferenceValue;
- serializedObject.ApplyModifiedProperties();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- internal static void CopyComponents(GameObject to, GameObject from, GameObject root, Dictionary<string, GameObject> nameMap)
- {
-
- var originalComponents = new List<Component>(from.GetComponents<Component>());
- var destinationComponents = new List<Component>(to.GetComponents<Component>());
- foreach(var fromComponent in originalComponents)
- {
-
- if (fromComponent == null)
- {
- continue;
- }
-
-
-
-
- if (fromComponent is MeshFilter || (fromComponent is Transform && from != root))
- {
- FixSceneReferences(fromComponent, to.GetComponent(fromComponent.GetType()), root);
- continue;
- }
-
-
-
- if (fromComponent is UnityEngine.Formats.Fbx.Exporter.FbxPrefab ||
- fromComponent is RectTransform)
- {
- continue;
- }
- var json = EditorJsonUtility.ToJson(fromComponent);
- if (string.IsNullOrEmpty(json))
- {
-
- continue;
- }
- System.Type expectedType = fromComponent.GetType();
- Component toComponent = null;
-
- for (int i = 0, n = destinationComponents.Count; i < n; i++)
- {
-
- if (destinationComponents[i] == null)
- {
- continue;
- }
- if (destinationComponents[i].GetType() == expectedType)
- {
-
-
- toComponent = destinationComponents[i];
- destinationComponents.RemoveAt(i);
- break;
- }
- }
-
-
-
-
- if(!toComponent && fromComponent is ParticleSystemRenderer)
- {
- toComponent = to.GetComponent<ParticleSystemRenderer>();
- }
- if (!toComponent)
- {
- toComponent = to.AddComponent(fromComponent.GetType());
- }
- if (!toComponent)
- {
-
- Debug.LogWarningFormat("{0}: Failed to add component of type {1} to converted object", ModelExporter.PACKAGE_UI_NAME, fromComponent.GetType().Name);
- continue;
- }
- FixSceneReferences(fromComponent, toComponent, root);
-
-
-
-
- if (fromComponent is SkinnedMeshRenderer)
- {
- var skinnedMesh = toComponent as SkinnedMeshRenderer;
- var mesh = skinnedMesh.sharedMesh;
- EditorJsonUtility.FromJsonOverwrite(json, toComponent);
- skinnedMesh.sharedMesh = mesh;
- }
- else
- {
- EditorJsonUtility.FromJsonOverwrite(json, toComponent);
- }
- if(fromComponent is MeshCollider)
- {
-
-
- var fromMeshCollider = from.GetComponent<MeshCollider>();
- var fromMeshFilter = from.GetComponent<MeshFilter>();
-
-
- if (fromMeshCollider && fromMeshFilter && fromMeshCollider.sharedMesh == fromMeshFilter.sharedMesh)
- {
- var toFilter = to.GetComponent<MeshFilter>();
- if (toFilter)
- {
- var toMeshCollider = toComponent as MeshCollider;
- toMeshCollider.sharedMesh = toFilter.sharedMesh;
- }
- }
- }
- var serializedFromComponent = new SerializedObject(fromComponent);
- var serializedToComponent = new SerializedObject(toComponent);
- var fromProperty = serializedFromComponent.GetIterator();
- fromProperty.Next(true);
-
-
-
-
- while (fromProperty.Next((fromComponent is SkinnedMeshRenderer)? fromProperty.hasChildren : fromProperty.hasVisibleChildren))
- {
- if (!fromProperty.hasVisibleChildren)
- {
-
-
- if (fromProperty.propertyType == SerializedPropertyType.ObjectReference && fromProperty.propertyPath != "m_GameObject" &&
- fromProperty.propertyPath != "m_Father" && fromProperty.objectReferenceValue &&
- (fromProperty.objectReferenceValue is GameObject || fromProperty.objectReferenceValue is Component))
- {
- CopySerializedProperty(serializedToComponent, fromProperty, nameMap);
- }
- }
- }
- }
- }
- }
- }
|