UI组件扩展

This commit is contained in:
梦语 2024-10-31 10:58:14 +08:00
parent 16288a41fc
commit 37705271c5
13 changed files with 421 additions and 165 deletions

File diff suppressed because one or more lines are too long

View File

@ -12,7 +12,12 @@ using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.UI;
namespace Ether
{
@ -105,9 +110,72 @@ namespace Ether
#endregion
#if UNITY_EDITOR
public static void CreateComponent<T>(GameObject selectObject, Action<GameObject> callBack, params Type[] types) where T : Component
{
GameObject gameObject;
Type[] typesArray = new Type[types.Length + 1];
for (int i = 0; i < types.Length; i++)
{
typesArray[i] = types[i];
}
typesArray[types.Length] = typeof(T);
if (selectObject != null)
{
Debug.Log($"选择物体:{selectObject.name}");
// 获取物体的根 Canvas
Canvas rootCanvas = selectObject.GetComponentInParent<Canvas>();
Transform parentTransform;
if (rootCanvas != null)
{
parentTransform = selectObject.transform;
}
else
{
var canvas = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));
var eventSystem = ObjectFactory.CreateGameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(UnityEngine.EventSystems.StandaloneInputModule));
parentTransform = canvas.transform;
}
gameObject = ObjectFactory.CreateGameObject($"New {typeof(T).Name}", typesArray);
gameObject.transform.SetParent(parentTransform, false);
}
else
{
var canvas = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));
var eventSystem = ObjectFactory.CreateGameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(UnityEngine.EventSystems.StandaloneInputModule));
canvas.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
Transform parentTransform = canvas.transform;
gameObject = ObjectFactory.CreateGameObject($"New {typeof(T).Name}", typesArray);
gameObject.transform.SetParent(parentTransform, false);
}
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localScale = new Vector3(1, 1, 1);
callBack?.Invoke(gameObject);
EditorUtility.FocusProjectWindow();
Selection.activeObject = gameObject;
EditorGUIUtility.PingObject(Selection.activeObject);
}
#endif
/// <summary>
/// 设置物体显示和隐藏
/// </summary>
public static void SetVisible(this Component component, bool visible, Action callback = null)
{
SetVisible(component.gameObject, visible, callback);
}
public static void SetVisible(this GameObject go, bool visible, Action callback = null)
{
if (!go)
@ -128,7 +196,7 @@ namespace Ether
else
{
AnimController anim = go.GetComponent<AnimController>();
if (anim != null)
if (anim)
{
anim.PlayDisableAnim(() =>
{

View File

@ -441,53 +441,16 @@ namespace Ether
#endregion
#if UNITY_EDITOR
[MenuItem("GameObject/UIEx/ButtonEx", priority = 0)]
[MenuItem("GameObject/UIEx/ButtonEx", priority = -998)]
private static void CreateButtonEx(MenuCommand menuCmd)
{
GameObject selection = Selection.activeGameObject;
GameObject gameObject;
if (selection != null)
CommonExtension.CreateComponent<ButtonEx>(selection, (obj) =>
{
Debug.Log($"选择物体:{selection.name}");
// 获取物体的根 Canvas
Canvas rootCanvas = selection.GetComponentInParent<Canvas>();
Transform parentTransform;
}, typeof(Image));
if (rootCanvas != null)
{
parentTransform = selection.transform;
}
else
{
var canvas = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));
var eventSystem = ObjectFactory.CreateGameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(UnityEngine.EventSystems.StandaloneInputModule));
parentTransform = canvas.transform;
}
gameObject = ObjectFactory.CreateGameObject("New ButtonEx", typeof(Image), typeof(ButtonEx));
gameObject.transform.SetParent(parentTransform, false);
}
else
{
var canvas = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));
var eventSystem = ObjectFactory.CreateGameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(UnityEngine.EventSystems.StandaloneInputModule));
canvas.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
Transform parentTransform = canvas.transform;
gameObject = ObjectFactory.CreateGameObject("New ButtonEx", typeof(Image), typeof(ButtonEx));
gameObject.transform.SetParent(parentTransform, false);
}
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localScale = new Vector3(1, 1, 1);
EditorUtility.FocusProjectWindow();
Selection.activeObject = gameObject;
EditorGUIUtility.PingObject(Selection.activeObject);
}
#endif

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 02a47ea2a30a43f4be3ce83dfe1b5859
timeCreated: 1730342329

View File

@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Ether
{
public class ImageEx : Image
{
#if UNITY_EDITOR
[MenuItem("GameObject/UIEx/ImageEx", priority = -1000)]
private static void CreateButtonEx(MenuCommand menuCmd)
{
GameObject selection = Selection.activeGameObject;
CommonExtension.CreateComponent<ImageEx>(selection, (obj) =>
{
obj.GetComponent<ImageEx>().raycastTarget = false;
});
}
#endif
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 70e78a4898214182969b5f5c1049ac6e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: db4d6bc81622597438b97a60f098cce7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Ether
{
public class InputFielldEx : InputField
{
#if UNITY_EDITOR
[MenuItem("GameObject/UIEx/InputFielldEx", priority = -996)]
private static void CreateButtonEx(MenuCommand menuCmd)
{
GameObject selection = Selection.activeGameObject;
CommonExtension.CreateComponent<InputFielldEx>(selection, (obj) =>
{
});
}
#endif
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e8808c5ebbc3a734f8e70b53850b6f9c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -286,84 +286,48 @@ namespace Ether
}
#if UNITY_EDITOR
[MenuItem("GameObject/UIEx/ScrollViewEx/ScrollViewGridEx", priority = 1)]
[MenuItem("GameObject/UIEx/ScrollViewEx/ScrollViewGridEx", priority = -990)]
private static void CreateScrollViewEx(MenuCommand menuCmd)
{
GameObject selection = Selection.activeGameObject;
GameObject gameObject;
if (selection != null)
CommonExtension.CreateComponent<ScrollRect>(selection, (obj) =>
{
Debug.Log($"选择物体:{selection.name}");
obj.SetWidth(800);
obj.SetHeight(600);
// 获取物体的根 Canvas
Canvas rootCanvas = selection.GetComponentInParent<Canvas>();
Transform parentTransform;
GameObject viewport = ObjectFactory.CreateGameObject("Viewport", typeof(Image), typeof(Mask));
viewport.transform.SetParent(obj.transform, false);
RectTransform viewportRect = viewport.GetComponent<RectTransform>();
viewportRect.anchorMin = new Vector2(0, 0);
viewportRect.anchorMax = new Vector2(1, 1);
viewportRect.pivot = new Vector2(0, 1);
viewport.SetWidth(0);
viewport.SetHeight(0);
Mask viewportMask = viewport.GetComponent<Mask>();
viewportMask.showMaskGraphic = false;
if (rootCanvas != null)
{
parentTransform = selection.transform;
}
else
{
var canvas = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));
var eventSystem = ObjectFactory.CreateGameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(UnityEngine.EventSystems.StandaloneInputModule));
GameObject content = ObjectFactory.CreateGameObject("Content", typeof(RectTransform));
content.transform.SetParent(viewport.transform, false);
RectTransform contentRect = content.GetComponent<RectTransform>();
contentRect.anchorMin = new Vector2(0, 1);
contentRect.anchorMax = new Vector2(0, 1);
contentRect.pivot = new Vector2(0, 1);
content.SetWidth(800);
content.SetHeight(600);
parentTransform = canvas.transform;
}
ScrollRect scrollRect = obj.GetComponent<ScrollRect>();
scrollRect.content = contentRect;
scrollRect.viewport = viewportRect;
scrollRect.horizontal = false;
scrollRect.vertical = true;
gameObject = ObjectFactory.CreateGameObject("New ScrollViewEx", typeof(ScrollViewGridEx), typeof(ScrollRect), typeof(Canvas));
gameObject.transform.SetParent(parentTransform, false);
}
else
{
var canvas = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster));
var eventSystem = ObjectFactory.CreateGameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(UnityEngine.EventSystems.StandaloneInputModule));
ScrollViewGridEx scrollViewEx = obj.GetComponent<ScrollViewGridEx>();
scrollViewEx.content = contentRect;
canvas.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
Transform parentTransform = canvas.transform;
gameObject = ObjectFactory.CreateGameObject("New ScrollViewEx", typeof(ScrollViewGridEx), typeof(ScrollRect), typeof(Canvas));
gameObject.transform.SetParent(parentTransform, false);
}
}, typeof(Canvas));
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localScale = new Vector3(1, 1, 1);
gameObject.SetWidth(800);
gameObject.SetHeight(600);
GameObject viewport = ObjectFactory.CreateGameObject("Viewport", typeof(Image), typeof(Mask));
viewport.transform.SetParent(gameObject.transform, false);
RectTransform viewportRect = viewport.GetComponent<RectTransform>();
viewportRect.anchorMin = new Vector2(0, 0);
viewportRect.anchorMax = new Vector2(1, 1);
viewportRect.pivot = new Vector2(0, 1);
viewport.SetWidth(0);
viewport.SetHeight(0);
Mask viewportMask = viewport.GetComponent<Mask>();
viewportMask.showMaskGraphic = false;
GameObject content = ObjectFactory.CreateGameObject("Content", typeof(RectTransform));
content.transform.SetParent(viewport.transform, false);
RectTransform contentRect = content.GetComponent<RectTransform>();
contentRect.anchorMin = new Vector2(0, 1);
contentRect.anchorMax = new Vector2(0, 1);
contentRect.pivot = new Vector2(0, 1);
content.SetWidth(800);
content.SetHeight(600);
ScrollRect scrollRect = gameObject.GetComponent<ScrollRect>();
scrollRect.content = contentRect;
scrollRect.viewport = viewportRect;
scrollRect.horizontal = false;
scrollRect.vertical = true;
ScrollViewGridEx scrollViewEx = gameObject.GetComponent<ScrollViewGridEx>();
scrollViewEx.content = contentRect;
EditorUtility.FocusProjectWindow();
Selection.activeObject = gameObject;
EditorGUIUtility.PingObject(Selection.activeObject);
}
#endif

View File

@ -12,7 +12,11 @@ using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using TMPro;
using UnityEngine.UI;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Ether
{
@ -74,5 +78,19 @@ namespace Ether
{
textPreprocessor = new TextExPreprocesser();
}
#if UNITY_EDITOR
[MenuItem("GameObject/UIEx/TextEx", priority = -999)]
private static void CreateToggleEx(MenuCommand menuCmd)
{
GameObject selection = Selection.activeGameObject;
CommonExtension.CreateComponent<TextEx>(selection, (obj) =>
{
obj.GetComponent<TextEx>().raycastTarget = false;
});
}
#endif
}
}

View File

@ -2,6 +2,9 @@
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
@ -71,7 +74,7 @@ namespace Ether
checkRoot?.SetActive(isOn);
uncheckRoot?.SetActive(!isOn);
if (isOn && toggleGroup != null)
if (isOn && toggleGroup)
{
toggleGroup.SetToggleSelected(this);
}
@ -137,6 +140,21 @@ namespace Ether
{
}
#if UNITY_EDITOR
[MenuItem("GameObject/UIEx/ToggleEx", priority = -997)]
private static void CreateToggleEx(MenuCommand menuCmd)
{
GameObject selection = Selection.activeGameObject;
CommonExtension.CreateComponent<ToggleEx>(selection, (obj) =>
{
obj.GetComponent<Image>().color = new Color(1, 1, 1, 0);
}, typeof(Image));
}
#endif
}
}

View File

@ -12,5 +12,5 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c5ac578b65530ce44be05b4dd02c3247, type: 3}
m_Name: GlobalSettings
m_EditorClassIdentifier:
startFrame: LoginFrame
startScence: Login
startFrame:
startScence: Boot