AlicizaX/Client/Packages/com.alicizax.uxtool/Editor/UXGUI/Utils/UIBuilderUtil.cs

174 lines
7.0 KiB
C#
Raw Normal View History

2025-12-01 16:46:28 +08:00
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace AlicizaX.UXTool
2025-12-01 16:46:28 +08:00
{
public static class UIBuilderUtil
{
public static void DuplicateSelectedObjects()
{
GameObject[] selectedObjects = UXSelectionUtil.gameObjects;
List<GameObject> duplicateObjects = new List<GameObject>();
foreach (var orginObject in selectedObjects)
{
GameObject duplicate = Object.Instantiate(orginObject, orginObject.transform.parent);
duplicateObjects.Add(duplicate);
}
Undo.RecordObjects(duplicateObjects.ToArray(), "Duplicate Objects");
}
/// <summary>
/// 创建并设置 UI 对象的工具方法。
/// </summary>
/// <param name="source">可以是预制体或现有对象</param>
/// <param name="parent">指定父级 Transform</param>
/// <param name="fullScreen">是否设置为全屏填充</param>
/// <param name="useOriginalSize">是否按照原始大小显示</param>
/// <param name="customScale">自定义缩放比例(可选)</param>
/// <param name="usePrefabMode">是否强制使用预制体模式(默认为 true如果传入的是预制体将在父节点下实例化</param>
/// <returns>返回创建的 GameObject</returns>
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<RectTransform>();
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;
}
/// <summary>
/// 判断传入的对象是否为预制体。
/// </summary>
/// <param name="source">待检查的对象</param>
/// <returns>如果是预制体,返回 true否则返回 false</returns>
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>();
rectTransform.sizeDelta = new Vector2(200, 200);
obj.transform.SetParent(parent.transform);
obj.GetComponent<RectTransform>().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>();
rectTransform.sizeDelta = new Vector2(100, 100);
obj.GetComponent<RectTransform>().localPosition = new Vector3(0, 0, 0);
return obj;
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="pos">LocalPosition</param>
/// <param name="size"></param>
/// <param name="selection"></param>
/// <returns></returns>
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<RectTransform>(obj);
rectTransform.sizeDelta = size;
obj.transform.localPosition = pos;
obj.transform.localScale = Vector3.one;
Undo.SetCurrentGroupName("Create " + name);
return obj;
}
}
}