Unity Editor脚本一键导入

养翔
2023-12-01
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;

namespace XP.XUI
{
    public struct StylePadding
    {
        public float topPadding;
        public float bottomPadding;
        public float titleSpacing;
        public float widgetSpacing;
    }
    [CanEditMultipleObjects]
    [CustomEditor(typeof(ListItemFitter), true)]
    public class ListItemFitterTool : Editor
    {
        private static List<string> title_Names = new List<string>
        {
            "Title",
            "title",
            "T_MainContent",
            "T_Title",
        };
        private static List<string> subtitle_Names = new List<string>
        {
            "SubTitle",
            "Subtitle",
            "subtitle",
            "T_Subtitle",
        };
        private static List<System.Type> widget_Types = new List<System.Type>
        {
            typeof(XSegmented),
            typeof(XSlider),
            typeof(XToggleGroup),
        };

        /// <summary>
        /// 副标题为空
        /// 控件为空
        /// 的样式
        /// </summary>
        StylePadding stylePadding_Sub_Null_Widget_Null = new StylePadding
        {
            topPadding = 32,
            bottomPadding = 32,
            titleSpacing = 0,
            widgetSpacing = 0,
        };

        /// <summary>
        /// 副标题为非空
        /// 控件为空
        /// 的样式
        /// </summary>
        StylePadding stylePadding_Sub_Widget_Null = new StylePadding
        {
            topPadding = 20,
            bottomPadding = 25,
            titleSpacing = 0,
            widgetSpacing = 0,
        };

        /// <summary>
        /// 副标题为非空
        /// 控件为非空
        /// 的样式
        /// </summary>
        StylePadding stylePadding_Sub_Widget = new StylePadding
        {
            topPadding = 25,
            bottomPadding = 40,
            titleSpacing = 1,
            widgetSpacing = 20,
        };

        /// <summary>
        /// 副标题为空
        /// 控件为非空
        /// 的样式
        /// </summary>
        StylePadding stylePadding_Sub_Null_Widget = new StylePadding
        {
            topPadding = 25,
            bottomPadding = 40,
            titleSpacing = 0,
            widgetSpacing = 20,
        };


        private ListItemFitter m_target;
        private SerializedObject m_so;
        protected SerializedProperty m_titleRectTransform;
        protected SerializedProperty m_subRectTransform;
        protected SerializedProperty m_widgetRectTransform;
        protected SerializedProperty m_topPadding;
        protected SerializedProperty m_bottomPadding;
        protected SerializedProperty m_titleSpacing;
        protected SerializedProperty m_widgetSpacing;

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            if (GUILayout.Button("淦!一键导入"))
            {

                foreach (Object obj in targets)
                {
                    EditorUtility.SetDirty(obj);
                    m_target = obj as ListItemFitter;
                    SerializedObject m_serializedObject = new SerializedObject(obj);
                    m_titleRectTransform = m_serializedObject.FindProperty("titleRectTransform");
                    m_subRectTransform = m_serializedObject.FindProperty("subRectTransform");
                    m_widgetRectTransform = m_serializedObject.FindProperty("widgetRectTransform");

                    m_topPadding = m_serializedObject.FindProperty("topPadding");
                    m_bottomPadding = m_serializedObject.FindProperty("bottomPadding");
                    m_titleSpacing = m_serializedObject.FindProperty("titleSpacing");
                    m_widgetSpacing = m_serializedObject.FindProperty("widgetSpacing");

                    SerializedObject serializedItem;
                    //获取标题
                    for (int i = 0; i < title_Names.Count; i++)
                    {
                        serializedItem = GetSerializedObject(title_Names[i]);
                        if (serializedItem != null)
                        {
                            m_titleRectTransform.objectReferenceValue = serializedItem.targetObject;
                        }
                    }
                    //获取副标题
                    for (int i = 0; i < subtitle_Names.Count; i++)
                    {
                        serializedItem = GetSerializedObject(subtitle_Names[i]);
                        if (serializedItem != null)
                        {
                            m_subRectTransform.objectReferenceValue = serializedItem.targetObject;
                        }
                    }
                    //获取控件
                    for (int i = 0; i < widget_Types.Count; i++)
                    {
                        serializedItem = GetSerializedObjectByType(widget_Types[i]);
                        if (serializedItem != null)
                        {
                            m_widgetRectTransform.objectReferenceValue = serializedItem.targetObject;
                        }
                    }

                    m_serializedObject.ApplyModifiedProperties();

                    //根据控件来填充不同样式
                    //副标题为空
                    if (m_subRectTransform.objectReferenceValue == null)
                    {
                        //控件为空
                        if (m_widgetRectTransform.objectReferenceValue == null)
                        {
                            SetStyle(stylePadding_Sub_Null_Widget_Null);
                        }
                        else
                        {
                            SetStyle(stylePadding_Sub_Null_Widget);
                        }
                    }
                    //副标题为非空
                    else
                    {
                        //控件为空
                        if (m_widgetRectTransform.objectReferenceValue == null)
                        {
                            SetStyle(stylePadding_Sub_Widget_Null);
                        }
                        else
                        {
                            SetStyle(stylePadding_Sub_Widget);
                        }
                    }
                    m_serializedObject.ApplyModifiedProperties();
                }
            }

        }

        /// <summary>
        /// 设置style
        /// </summary>
        /// <param name="style"></param>
        private void SetStyle(StylePadding style)
        {
            m_topPadding.floatValue = style.topPadding;
            m_bottomPadding.floatValue = style.bottomPadding;
            m_titleSpacing.floatValue = style.titleSpacing;
            m_widgetSpacing.floatValue = style.widgetSpacing;
        }

        /// <summary>
        /// 通过名字获取recttranform
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private SerializedObject GetSerializedObject(string name)
        {
            RectTransform transform = GetChild(m_target.transform, name) as RectTransform;
            if (transform == null)
            {
                return null;
            }
            else
            {
                return new SerializedObject(transform);
            }
        }

        /// <summary>
        /// 通过类型获取recttranform
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private SerializedObject GetSerializedObjectByType(System.Type type)
        {
            Component component = m_target.transform.GetComponentInChildren(type, true);
            if (component == null)
            {
                return null;
            }
            else
            {
                return new SerializedObject(component.GetComponent<RectTransform>());
            }
        }


        /// <summary>
        /// 递归查找子物体
        /// </summary>
        /// <param name="parentTF">父物体变换组件</param>
        /// <param name="children">子物体名称</param>
        public Transform GetChild(Transform parentTF, string childName)
        {
            //在子物体中查找
            Transform childTF = parentTF.Find(childName);
            if (childTF != null) return childTF;
            //将问题交给子物体
            for (int i = 0; i < parentTF.childCount; i++)
            {
                childTF = GetChild(parentTF.GetChild(i), childName);
                if (childTF != null) return childTF;
            }
            return null;

        }
    }
}

 类似资料: