public static void ReplaceComponents(Transform src, Transform dst, ComponentUtility.IsDesiredComponent filter = null) { if (filter == null) { filter = component => component.GetType() != typeof(Transform); } ComponentUtility.ReplaceComponentsIfDifferent(src.gameObject, dst.gameObject, filter); var scrComs = src.GetComponents <Component>(); var dstComs = dst.GetComponents <Component>(); for (int i = 0; i < scrComs.Length; i++) { var scrCom = scrComs[i]; if (!filter(scrCom)) { continue; } var dstCom = dst.GetComponent(scrCom.GetType()); if (dstCom == null) { return; } CopyComponentObjectReference(scrCom, dstCom); } }
public static void DestroyComponentsMatching(GameObject dst, ComponentUtility.IsDesiredComponent componentFilter) { List <Component> list = new List <Component>(); dst.GetComponents <Component>(list); list.RemoveAll((Component x) => !componentFilter(x)); ComponentUtility.DestroyComponents(list); }
public static void ReplaceComponentsIfDifferent(GameObject src, GameObject dst, ComponentUtility.IsDesiredComponent componentFilter) { List <Component> list = new List <Component>(); src.GetComponents <Component>(list); list.RemoveAll((Component x) => !componentFilter(x)); List <Component> list2 = new List <Component>(); dst.GetComponents <Component>(list2); list2.RemoveAll((Component x) => !componentFilter(x)); if (!ComponentUtility.CompareComponentOrderAndTypes(list, list2)) { ComponentUtility.DestroyComponents(list2); list2.Clear(); for (int num = 0; num != list.Count; num++) { Component item = dst.AddComponent(list[num].GetType()); list2.Add(item); } } for (int num2 = 0; num2 != list.Count; num2++) { EditorUtility.CopySerializedIfDifferent(list[num2], list2[num2]); } }