using UnityEditor; namespace UnityEngine.UI { public static class UIEffectWrapDrawer { static string LabelStr; static string ShadowStr; static string OutlineStr; public static void InitInspectorString() { LabelStr = "Effect"; ShadowStr = "Shadow"; OutlineStr = "Outline"; } public static void Draw(Rect position, GameObject target) { var nameRect = new Rect(position) { width = position.width / 2 }; EditorGUI.LabelField(nameRect, LabelStr); var shadowRect = new Rect(position) { x = position.x + EditorGUIUtility.labelWidth + 2, width = 60 }; if (GUI.Button(shadowRect, ShadowStr)) { if (target.GetComponent() != null) { } else if (target.GetComponent() != null) { } GenShadowComponent(target); } var outlineRect = new Rect(position) { x = shadowRect.x + 50 + 20, width = 60 }; if (GUI.Button(outlineRect, OutlineStr)) { if (target.GetComponent() != null) { } else if (target.GetComponent() != null) { } GenOutLineComponent(target); } } private static void GenOutLineComponent(GameObject target) { target.TryAddComponent(); } private static void GenShadowComponent(GameObject target) { //暂时无法处理 有继承关系的Component 单独判定区分outline //target.TryAddComponent(); // Shadow[] components = target.GetComponents(); // bool hasShadow = false; // for (int i = 0; i < components.Length; i++) // { // Outline outline = components[i] as Outline; // //有一个不是OutLine就认为是Shadow // if (outline == null) // { // hasShadow = true; // } // } //if (!hasShadow) //{ target.AddComponent(); //} } private static T TryAddComponent(this GameObject target) where T : Component { //暂时无法处理 有继承关系的Component 挠头 target.TryGetComponent(out T component); //if (component == null) //{ component = target.AddComponent(); //} return component; } } }