using System.Collections.Generic; using UnityEditor; using UnityEngine; namespace Aliciza.UXTool { public static class UIBuilderUtil { public static void DuplicateSelectedObjects() { GameObject[] selectedObjects = UXSelectionUtil.gameObjects; List duplicateObjects = new List(); foreach (var orginObject in selectedObjects) { GameObject duplicate = Object.Instantiate(orginObject, orginObject.transform.parent); duplicateObjects.Add(duplicate); } Undo.RecordObjects(duplicateObjects.ToArray(), "Duplicate Objects"); } /// /// 创建并设置 UI 对象的工具方法。 /// /// 可以是预制体或现有对象 /// 指定父级 Transform /// 是否设置为全屏填充 /// 是否按照原始大小显示 /// 自定义缩放比例(可选) /// 是否强制使用预制体模式(默认为 true,如果传入的是预制体,将在父节点下实例化) /// 返回创建的 GameObject public static GameObject CreateUIObject(GameObject source, Transform parent, bool fullScreen = false, bool useOriginalSize = false, Vector3? customScale = null, bool usePrefabMode = true) { // 如果 source 是 null,抛出异常 if (source == null) { Debug.LogError("传入的对象为空!无法创建 UI 对象。"); return null; } GameObject instance; // 判断是否需要使用预制体模式 if (usePrefabMode && IsPrefab(source)) { // 如果是预制体,则在父节点下实例化 instance = Object.Instantiate(source, parent); instance.name = source.name; } else { // 否则直接使用传入的对象 instance = source; instance.transform.SetParent(parent); } // 设置 RectTransform RectTransform rectTransform = instance.GetComponent(); if (rectTransform == null) { Debug.LogError("传入的对象没有 RectTransform 组件,无法作为 UI 对象使用!"); return null; } rectTransform.localScale = customScale ?? Vector3.one; // 使用自定义缩放,默认为 1,1,1 rectTransform.localPosition = Vector3.zero; // 默认位置为 (0, 0) // 设置锚点和偏移量 if (fullScreen) { rectTransform.anchorMin = Vector2.zero; // 左下角锚点 rectTransform.anchorMax = Vector2.one; // 右上角锚点 rectTransform.offsetMin = Vector2.zero; // 左下角偏移为 0 rectTransform.offsetMax = Vector2.zero; // 右上角偏移为 0 } else if (useOriginalSize) { // 如果使用原始大小,则保留锚点和偏移量,通常不修改 rectTransform.anchorMin = new Vector2(0.5f, 0.5f); // 设置锚点到中心 rectTransform.anchorMax = new Vector2(0.5f, 0.5f); rectTransform.anchoredPosition = Vector2.zero; // 保证在父级中心位置 } else { // 默认大小逻辑,保持当前锚点和偏移 rectTransform.offsetMin = rectTransform.offsetMin; rectTransform.offsetMax = rectTransform.offsetMax; } return instance; } /// /// 判断传入的对象是否为预制体。 /// /// 待检查的对象 /// 如果是预制体,返回 true;否则返回 false public static bool IsPrefab(GameObject source) { #if UNITY_EDITOR return UnityEditor.PrefabUtility.GetPrefabAssetType(source) != UnityEditor.PrefabAssetType.NotAPrefab; #else Debug.LogWarning("只能在编辑器模式下检查是否为预制体。"); return false; #endif } //创建UX UI前都会创建一个GameObject来挂载Component private static GameObject CreateUIObjWithParent(string name) { RectTransform parent; bool haveParent = Utils.TryGetSelectionRectTransform(out parent); if (haveParent) { var obj = new GameObject(name); obj.layer = LayerMask.NameToLayer("UI"); var rectTransform = obj.AddComponent(); rectTransform.sizeDelta = new Vector2(200, 200); obj.transform.SetParent(parent.transform); obj.GetComponent().localPosition = new Vector3(0, 0, 0); Undo.RegisterCreatedObjectUndo(obj.gameObject, "Create" + obj.name); return obj; } else { EditorUtility.DisplayDialog("messageBox", "请先选择一个父节点", "确定", "取消"); return null; } } public static GameObject CreateUIObj(string name) { var obj = new GameObject(name); obj.layer = LayerMask.NameToLayer("UI"); var rectTransform = obj.AddComponent(); rectTransform.sizeDelta = new Vector2(100, 100); obj.GetComponent().localPosition = new Vector3(0, 0, 0); return obj; } /// /// /// /// /// LocalPosition /// /// /// public static GameObject CreateUIObj(string name, Vector3 pos, Vector3 size, GameObject[] selection) { name = "UX" + name; var obj = new GameObject(name); Undo.RegisterCreatedObjectUndo(obj, ""); obj.layer = LayerMask.NameToLayer("UI"); Transform parent; parent = FindContainerLogic.GetObjectParent(selection); Undo.SetTransformParent(obj.transform, parent, ""); obj.transform.SetParent(parent); var rectTransform = Undo.AddComponent(obj); rectTransform.sizeDelta = size; obj.transform.localPosition = pos; obj.transform.localScale = Vector3.one; Undo.SetCurrentGroupName("Create " + name); return obj; } } }