Skip to content

Commit 1164336

Browse files
committed
Copy the components to the newly converted GameObject
1 parent 98f18b2 commit 1164336

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ private static List<GameObject> OnConvertInPlace ()
5757

5858
GameObject [] unityActiveGOs = Selection.GetFiltered<GameObject> (SelectionMode.Editable | SelectionMode.TopLevel);
5959

60+
// Ensure that the root GameObjects retain their relative order after export
61+
System.Array.Sort (unityActiveGOs, delegate(GameObject x, GameObject y) {
62+
return x.transform.GetSiblingIndex().CompareTo(y.transform.GetSiblingIndex());
63+
});
64+
6065
// find common ancestor root & filePath;
6166
string filePath = "";
6267
string dirPath = Path.Combine (Application.dataPath, "Objects");
@@ -109,6 +114,19 @@ private static List<GameObject> OnConvertInPlace ()
109114

110115
unityGO.transform.SetSiblingIndex (siblingIndex);
111116

117+
// copy the components over, assuming that the hierarchy order is unchanged
118+
if (unityActiveGOs.Length == 1) {
119+
CopyComponentsRecursive (unityActiveGOs [0], unityGO);
120+
} else {
121+
if (unityActiveGOs.Length != unityGO.transform.childCount) {
122+
Debug.LogWarning (string.Format ("Warning: Exported {0} objects, but only imported {1}",
123+
unityActiveGOs.Length, unityGO.transform.childCount));
124+
}
125+
for (int i = 0; i < unityGO.transform.childCount; i++) {
126+
CopyComponentsRecursive (unityActiveGOs [i], unityGO.transform.GetChild (i).gameObject);
127+
}
128+
}
129+
112130
result.Add (unityObj as GameObject);
113131

114132
// remove (now redundant) gameobjects
@@ -132,6 +150,36 @@ private static List<GameObject> OnConvertInPlace ()
132150

133151
return result;
134152
}
153+
154+
private static void CopyComponentsRecursive(GameObject from, GameObject to){
155+
if (!to.name.StartsWith(from.name) || from.transform.childCount != to.transform.childCount) {
156+
Debug.LogError (string.Format("Error: hierarchies don't match (From: {0}, To: {1})", from.name, to.name));
157+
return;
158+
}
159+
160+
CopyComponents (from, to);
161+
for (int i = 0; i < from.transform.childCount; i++) {
162+
CopyComponentsRecursive(from.transform.GetChild(i).gameObject, to.transform.GetChild(i).gameObject);
163+
}
164+
}
165+
166+
private static void CopyComponents(GameObject from, GameObject to){
167+
var components = from.GetComponents<Component> ();
168+
for(int i = 0; i < components.Length; i++){
169+
// if to already has this component, then skip it
170+
if(to.GetComponent(components[i].GetType()) != null){
171+
continue;
172+
}
173+
bool success = UnityEditorInternal.ComponentUtility.CopyComponent (components[i]);
174+
if (success) {
175+
success = UnityEditorInternal.ComponentUtility.PasteComponentAsNew (to);
176+
}
177+
if (!success) {
178+
Debug.LogWarning (string.Format ("Warning: Failed to copy component {0} from {1} to {2}",
179+
components[i].GetType().Name, from.name, to.name));
180+
}
181+
}
182+
}
135183
}
136184
}
137185
}

0 commit comments

Comments
 (0)