private bool Pack(List <GameObject> gameObjects, string name, out GameObject groupRoot) { groupRoot = null; if (gameObjects == null || gameObjects.Count == 0) { return(false); } groupRoot = new GameObject(name); Undo.IncrementCurrentGroup(); var undoGroupIndex = Undo.GetCurrentGroup(); Undo.SetCurrentGroupName("Make prefab asset"); Undo.RegisterCreatedObjectUndo(groupRoot, "Create prefab asset root object"); var groupRect = groupRoot.AddComponent <RectTransform>(); groupRect.transform.SetParent(gameObjects[0].transform.parent); var leftTopPos = new Vector2(99999, -99999); var rightBottomPos = new Vector2(-99999, 99999); foreach (var obj in gameObjects) { var bounds = PrefabUtil.GetBounds(obj); var boundMin = obj.transform.parent.InverseTransformPoint(bounds.min); var boundMax = obj.transform.parent.InverseTransformPoint(bounds.max); leftTopPos.x = Mathf.Min(leftTopPos.x, boundMin.x); leftTopPos.y = Mathf.Max(leftTopPos.y, boundMax.y); rightBottomPos.x = Mathf.Max(rightBottomPos.x, boundMax.x); rightBottomPos.y = Mathf.Min(rightBottomPos.y, boundMin.y); } groupRect.sizeDelta = new Vector2(rightBottomPos.x - leftTopPos.x, leftTopPos.y - rightBottomPos.y); leftTopPos.x += groupRect.sizeDelta.x / 2; leftTopPos.y -= groupRect.sizeDelta.y / 2; groupRect.localPosition = leftTopPos; groupRect.localScale = Vector3.one; //需要先生成好root节点和设置好它的坐标和大小才可以把选中的节点挂进来,注意要先排好序,不然层次就乱了 GameObject[] sorted_objs = gameObjects.OrderBy(x => x.transform.GetSiblingIndex()).ToArray(); for (int i = 0; i < sorted_objs.Length; i++) { Undo.SetTransformParent(sorted_objs[i].transform, groupRect, "Move child item to group"); } Selection.activeGameObject = groupRoot; Undo.CollapseUndoOperations(undoGroupIndex); return(true); }
public override void OnInspectorGUI() { var dummyMono = target as UIDummy; if (dummyMono == null) { return; } if (PrefabUtil.IsPrefab(dummyMono.gameObject)) { EditorGUILayout.LabelField("请前往Prefab实例内编辑锚点"); return; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("锚点列表"); if (GUILayout.Button("添加")) { dummyMono.Dummys.Add(new DummyDefine()); } EditorGUILayout.EndHorizontal(); var pos = new Vector2(0, 20); EditorGUILayout.BeginScrollView(pos); { var dummyArr = new List <DummyDefine>(dummyMono.Dummys.ToArray()); var index = 1; foreach (var define in dummyArr) { define.Offset = EditorGUILayout.Vector2Field("偏移", define.Offset); define.Type = (DummyType)EditorGUILayout.EnumPopup("类型", define.Type); define.AliasName = EditorGUILayout.TextField("别名", define.AliasName); if (GUILayout.Button("删除")) { dummyArr.Remove(define); } EditorGUILayout.Separator(); index++; } dummyMono.Dummys = dummyArr; } EditorGUILayout.EndScrollView(); }
private void Unpack(object arg) { var gameObj = (GameObject)arg; var currTrans = gameObj.transform; var toTrans = gameObj.transform.parent; if (PrefabUtil.IsPrefab(gameObj)) { PrefabUtility.UnpackPrefabInstance(gameObj, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction); } var childrenTransform = gameObj.transform.GetComponentsInChildren <Transform>(true); foreach (var trans in childrenTransform) { if (trans.parent != currTrans || trans == currTrans) { continue; } trans.SetParent(toTrans, true); } GameObject.DestroyImmediate(gameObj); }