Skip to content

Commit a0fda6e

Browse files
committed
clean up code + set the transform after import
set the transform to be the same as it was before (just copy the transform of the original GameObject)
1 parent 25c5853 commit a0fda6e

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,10 @@ private static List<GameObject> OnConvertInPlace ()
103103
if (unityObj != null)
104104
{
105105
GameObject unityGO = unityObj as GameObject;
106-
Transform unityGOTransform = unityGO.transform;
107-
Transform origGOTransform = gosToExport [i].transform;
108106

109-
// Set the name to be the name of the instantiated asset.
110-
// This will get rid of the "(Clone)" if it's added
111-
unityGO.name = unityMainAsset.name;
107+
SetupImportedGameObject (gosToExport [i], unityGO);
112108

113-
// configure transform and maintain local pose
114-
unityGOTransform.SetParent (origGOTransform.parent, false);
115-
116-
unityGOTransform.SetSiblingIndex (origGOTransform.GetSiblingIndex());
117-
118-
// copy the components over, assuming that the hierarchy order is unchanged
119-
if (origGOTransform.hierarchyCount != unityGOTransform.hierarchyCount) {
120-
Debug.LogWarning (string.Format ("Warning: Exported {0} objects, but only imported {1}",
121-
origGOTransform.hierarchyCount, unityGOTransform.hierarchyCount));
122-
}
123-
CopyComponentsRecursive (gosToExport[i], unityGO);
124-
125-
result.Add (unityObj as GameObject);
109+
result.Add (unityGO);
126110

127111
// remove (now redundant) gameobject
128112
#if UNI_19965
@@ -144,6 +128,37 @@ private static List<GameObject> OnConvertInPlace ()
144128
return result;
145129
}
146130

131+
private static void SetupImportedGameObject(GameObject orig, GameObject imported)
132+
{
133+
Transform importedTransform = imported.transform;
134+
Transform origTransform = orig.transform;
135+
136+
// Set the name to be the name of the instantiated asset.
137+
// This will get rid of the "(Clone)" if it's added
138+
imported.name = orig.name;
139+
140+
// configure transform and maintain local pose
141+
importedTransform.SetParent (origTransform.parent, false);
142+
importedTransform.SetSiblingIndex (origTransform.GetSiblingIndex());
143+
144+
// set the transform to be the same as before
145+
bool success = UnityEditorInternal.ComponentUtility.CopyComponent (origTransform);
146+
if (success) {
147+
success = UnityEditorInternal.ComponentUtility.PasteComponentValues(importedTransform);
148+
}
149+
if (!success) {
150+
Debug.LogWarning (string.Format ("Warning: Failed to copy component Transform from {0} to {1}",
151+
imported.name, origTransform.name));
152+
}
153+
154+
// copy the components over, assuming that the hierarchy order is unchanged
155+
if (origTransform.hierarchyCount != importedTransform.hierarchyCount) {
156+
Debug.LogWarning (string.Format ("Warning: Exported {0} objects, but only imported {1}",
157+
origTransform.hierarchyCount, importedTransform.hierarchyCount));
158+
}
159+
CopyComponentsRecursive (orig, imported);
160+
}
161+
147162
private static void CopyComponentsRecursive(GameObject from, GameObject to){
148163
if (!to.name.StartsWith(from.name) || from.transform.childCount != to.transform.childCount) {
149164
Debug.LogError (string.Format("Error: hierarchies don't match (From: {0}, To: {1})", from.name, to.name));

0 commit comments

Comments
 (0)