UI组件扩展
This commit is contained in:
parent
16288a41fc
commit
37705271c5
File diff suppressed because one or more lines are too long
@ -12,7 +12,12 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
|
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
using UnityEditor;
|
||||||
|
#endif
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
|
||||||
namespace Ether
|
namespace Ether
|
||||||
{
|
{
|
||||||
@ -105,9 +110,72 @@ namespace Ether
|
|||||||
|
|
||||||
#endregion
|
#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>
|
||||||
/// 设置物体显示和隐藏
|
/// 设置物体显示和隐藏
|
||||||
/// </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)
|
public static void SetVisible(this GameObject go, bool visible, Action callback = null)
|
||||||
{
|
{
|
||||||
if (!go)
|
if (!go)
|
||||||
@ -128,7 +196,7 @@ namespace Ether
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
AnimController anim = go.GetComponent<AnimController>();
|
AnimController anim = go.GetComponent<AnimController>();
|
||||||
if (anim != null)
|
if (anim)
|
||||||
{
|
{
|
||||||
anim.PlayDisableAnim(() =>
|
anim.PlayDisableAnim(() =>
|
||||||
{
|
{
|
||||||
|
@ -441,53 +441,16 @@ namespace Ether
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
[MenuItem("GameObject/UIEx/ButtonEx", priority = 0)]
|
[MenuItem("GameObject/UIEx/ButtonEx", priority = -998)]
|
||||||
private static void CreateButtonEx(MenuCommand menuCmd)
|
private static void CreateButtonEx(MenuCommand menuCmd)
|
||||||
{
|
{
|
||||||
GameObject selection = Selection.activeGameObject;
|
GameObject selection = Selection.activeGameObject;
|
||||||
|
|
||||||
GameObject gameObject;
|
CommonExtension.CreateComponent<ButtonEx>(selection, (obj) =>
|
||||||
|
|
||||||
if (selection != null)
|
|
||||||
{
|
{
|
||||||
Debug.Log($"选择物体:{selection.name}");
|
|
||||||
|
|
||||||
// 获取物体的根 Canvas
|
}, typeof(Image));
|
||||||
Canvas rootCanvas = selection.GetComponentInParent<Canvas>();
|
|
||||||
Transform parentTransform;
|
|
||||||
|
|
||||||
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
|
#endif
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 02a47ea2a30a43f4be3ce83dfe1b5859
|
||||||
|
timeCreated: 1730342329
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 70e78a4898214182969b5f5c1049ac6e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: db4d6bc81622597438b97a60f098cce7
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e8808c5ebbc3a734f8e70b53850b6f9c
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@ -286,54 +286,18 @@ namespace Ether
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
[MenuItem("GameObject/UIEx/ScrollViewEx/ScrollViewGridEx", priority = 1)]
|
[MenuItem("GameObject/UIEx/ScrollViewEx/ScrollViewGridEx", priority = -990)]
|
||||||
private static void CreateScrollViewEx(MenuCommand menuCmd)
|
private static void CreateScrollViewEx(MenuCommand menuCmd)
|
||||||
{
|
{
|
||||||
GameObject selection = Selection.activeGameObject;
|
GameObject selection = Selection.activeGameObject;
|
||||||
|
|
||||||
GameObject gameObject;
|
CommonExtension.CreateComponent<ScrollRect>(selection, (obj) =>
|
||||||
|
|
||||||
if (selection != null)
|
|
||||||
{
|
{
|
||||||
Debug.Log($"选择物体:{selection.name}");
|
obj.SetWidth(800);
|
||||||
|
obj.SetHeight(600);
|
||||||
// 获取物体的根 Canvas
|
|
||||||
Canvas rootCanvas = selection.GetComponentInParent<Canvas>();
|
|
||||||
Transform parentTransform;
|
|
||||||
|
|
||||||
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 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));
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
GameObject viewport = ObjectFactory.CreateGameObject("Viewport", typeof(Image), typeof(Mask));
|
||||||
viewport.transform.SetParent(gameObject.transform, false);
|
viewport.transform.SetParent(obj.transform, false);
|
||||||
RectTransform viewportRect = viewport.GetComponent<RectTransform>();
|
RectTransform viewportRect = viewport.GetComponent<RectTransform>();
|
||||||
viewportRect.anchorMin = new Vector2(0, 0);
|
viewportRect.anchorMin = new Vector2(0, 0);
|
||||||
viewportRect.anchorMax = new Vector2(1, 1);
|
viewportRect.anchorMax = new Vector2(1, 1);
|
||||||
@ -352,18 +316,18 @@ namespace Ether
|
|||||||
content.SetWidth(800);
|
content.SetWidth(800);
|
||||||
content.SetHeight(600);
|
content.SetHeight(600);
|
||||||
|
|
||||||
ScrollRect scrollRect = gameObject.GetComponent<ScrollRect>();
|
ScrollRect scrollRect = obj.GetComponent<ScrollRect>();
|
||||||
scrollRect.content = contentRect;
|
scrollRect.content = contentRect;
|
||||||
scrollRect.viewport = viewportRect;
|
scrollRect.viewport = viewportRect;
|
||||||
scrollRect.horizontal = false;
|
scrollRect.horizontal = false;
|
||||||
scrollRect.vertical = true;
|
scrollRect.vertical = true;
|
||||||
|
|
||||||
ScrollViewGridEx scrollViewEx = gameObject.GetComponent<ScrollViewGridEx>();
|
ScrollViewGridEx scrollViewEx = obj.GetComponent<ScrollViewGridEx>();
|
||||||
scrollViewEx.content = contentRect;
|
scrollViewEx.content = contentRect;
|
||||||
|
|
||||||
EditorUtility.FocusProjectWindow();
|
}, typeof(Canvas));
|
||||||
Selection.activeObject = gameObject;
|
|
||||||
EditorGUIUtility.PingObject(Selection.activeObject);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -12,7 +12,11 @@ using System.Collections;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
|
using UnityEngine.UI;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
using UnityEditor;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Ether
|
namespace Ether
|
||||||
{
|
{
|
||||||
@ -74,5 +78,19 @@ namespace Ether
|
|||||||
{
|
{
|
||||||
textPreprocessor = new TextExPreprocesser();
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,9 @@
|
|||||||
using Sirenix.OdinInspector;
|
using Sirenix.OdinInspector;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
using UnityEditor;
|
||||||
|
#endif
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.EventSystems;
|
using UnityEngine.EventSystems;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
@ -71,7 +74,7 @@ namespace Ether
|
|||||||
checkRoot?.SetActive(isOn);
|
checkRoot?.SetActive(isOn);
|
||||||
uncheckRoot?.SetActive(!isOn);
|
uncheckRoot?.SetActive(!isOn);
|
||||||
|
|
||||||
if (isOn && toggleGroup != null)
|
if (isOn && toggleGroup)
|
||||||
{
|
{
|
||||||
toggleGroup.SetToggleSelected(this);
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,5 +12,5 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: c5ac578b65530ce44be05b4dd02c3247, type: 3}
|
m_Script: {fileID: 11500000, guid: c5ac578b65530ce44be05b4dd02c3247, type: 3}
|
||||||
m_Name: GlobalSettings
|
m_Name: GlobalSettings
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
startFrame: LoginFrame
|
startFrame:
|
||||||
startScence: Login
|
startScence: Boot
|
||||||
|
Loading…
Reference in New Issue
Block a user