UIModule
This commit is contained in:
ALEXTANG
2022-08-26 17:12:43 +08:00
parent 776b84adc2
commit 8983c52b59
11 changed files with 676 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
namespace TEngine.Runtime.UIModule
{
public partial class UISys:BehaviourSingleton<UISys>
public partial class UISys : BehaviourSingleton<UISys>
{
public static int DesginWidth => 750;

View File

@@ -0,0 +1,115 @@
using UnityEngine;
using UnityEngine.EventSystems;
namespace TEngine.Runtime.UIModule
{
public enum UIDragType
{
Draging,
Drop
}
public class DragItem : UIEventItem<DragItem>
{
private UIDragType m_DragState = UIDragType.Drop;
private Vector3 m_ItemOldPos;
private Vector3 m_ItemCachePos;
private bool m_CanDrag = false;
/// <summary>
/// 是否可以拖拽
/// </summary>
public bool CanDrag
{
get { return m_CanDrag; }
set
{
m_CanDrag = value;
if (m_CanDrag)
{
BindDrag();
}
}
}
protected override void OnCreate()
{
base.OnCreate();
BindDrag();
}
private void BindDrag()
{
if (m_CanDrag)
{
BindBeginDragEvent(delegate(DragItem item, PointerEventData data)
{
if (!m_CanDrag)
{
return;
}
StartDragItem(UIDragType.Draging);
});
BindEndDragEvent(delegate(DragItem item, PointerEventData data)
{
if (!m_CanDrag)
{
return;
}
EndDrag();
});
}
}
protected override void OnUpdate()
{
if (!m_CanDrag)
{
return;
}
UpdateDragPos();
}
private void StartDragItem(UIDragType type)
{
if (type != UIDragType.Drop)
{
m_ItemOldPos = transform.position;
Vector3 pos;
UISys.Mgr.GetMouseDownUiPos(out pos);
m_ItemCachePos = pos;
UpdateDragPos();
m_DragState = type;
}
}
private void EndDrag()
{
m_DragState = UIDragType.Drop;
transform.position = m_ItemOldPos;
#if UNITY_EDITOR
//Debug.LogError("m_ItemCachePos.y - m_ItemOldPos.y " + (m_ItemCachePos.y - m_ItemOldPos.y));
#endif
if (m_ItemCachePos.y - m_ItemOldPos.y > 3)
{
}
}
private void UpdateDragPos()
{
if (m_DragState == UIDragType.Drop)
{
return;
}
Vector3 pos;
UISys.Mgr.GetMouseDownUiPos(out pos);
transform.position += (pos - m_ItemCachePos);
m_ItemCachePos = pos;
}
}
}

View File

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

View File

@@ -0,0 +1,18 @@
using UnityEngine.UI;
public class EmptyGraph : Graphic
{
public bool m_debug = false;
protected override void OnPopulateMesh(VertexHelper vbo)
{
vbo.Clear();
#if UNITY_EDITOR
if (m_debug)
{
base.OnPopulateMesh(vbo);
}
#endif
}
}

View File

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

View File

@@ -0,0 +1,111 @@
using UnityEngine;
namespace TEngine.Runtime.UIModule
{
public enum TweenType
{
Position,
Rotation,
Scale,
Alpha,
}
public class TweenUtil : MonoBehaviour
{
public bool isLocal;
public TweenType type;
public Vector3 from;
public Vector3 to;
public AnimationCurve curve = AnimationCurve.Linear(0, 0, 1, 1);
public float duration = 1f;
public bool isLoop;
public bool isPingPong;
private float timer = 0f;
private CanvasGroup canvasGroup;
void Awake()
{
canvasGroup = gameObject.GetComponent<CanvasGroup>();
}
void Update()
{
if (duration > 0)
{
timer += Time.deltaTime;
float curveValue;
if (isLoop)
{
float remainTime = timer % duration;
int loopCount = (int)(timer / duration);
float evaluateTime = remainTime / duration;
if (isPingPong)
{
evaluateTime = loopCount % 2 == 0 ? evaluateTime : 1 - evaluateTime;
}
curveValue = curve.Evaluate(evaluateTime);
}
else
{
curveValue = curve.Evaluate(timer);
}
var lerpValue = Vector3.Lerp(from, to, curveValue);
//if (lerpValue == lastValue)
//{
// return;
//}
//lastValue = lerpValue;
switch (type)
{
case TweenType.Position:
if (isLocal)
{
transform.localPosition = lerpValue;
}
else
{
transform.position = lerpValue;
}
break;
case TweenType.Rotation:
if (isLocal)
{
transform.localEulerAngles = lerpValue;
}
else
{
transform.eulerAngles = lerpValue;
}
break;
case TweenType.Scale:
if (isLocal)
{
transform.localScale = lerpValue;
}
else
{
var value1 = VectorWiseDivision(transform.lossyScale, transform.localScale);
var value2 = VectorWiseDivision(lerpValue, value1);
transform.localScale = value2;
}
break;
case TweenType.Alpha:
if (canvasGroup != null)
{
canvasGroup.alpha = lerpValue.x;
}
else
{
TLogger.LogError("Change Alpha need Component: [CanvasGroup]");
}
break;
}
}
}
Vector3 VectorWiseDivision(Vector3 a, Vector3 b)
{
return new Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
}
}
}

View File

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

View File

@@ -0,0 +1,196 @@
using System;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace TEngine.Runtime.UIModule
{
public class UIEventItem<T> : UIWindowWidget where T : UIEventItem<T>
{
protected Button m_buttonClick;
private object[] m_eventParam;
public object EventParam1
{
get { return m_eventParam != null && m_eventParam.Length > 0 ? m_eventParam[0] : null; }
}
public object EventParam2
{
get { return m_eventParam != null && m_eventParam.Length > 1 ? m_eventParam[1] : null; }
}
public object EventParam3
{
get { return m_eventParam != null && m_eventParam.Length > 2 ? m_eventParam[2] : null; }
}
public object this[int index]
{
get { return m_eventParam != null && index < m_eventParam.Length ? m_eventParam[index] : null; }
}
private Action<T> m_clickAction;
private Action<T, bool, PointerEventData> m_pressAction;
private Action<T, PointerEventData> m_beginDragAction;
private Action<T, PointerEventData> m_dragAction;
private Action<T, PointerEventData> m_endDragAction;
public void BindClickEvent(Action<T> clickAction, params object[] arg)
{
if (m_clickAction != null)
{
m_clickAction = clickAction;
}
else
{
m_clickAction = clickAction;
m_buttonClick = UnityUtil.AddMonoBehaviour<Button>(gameObject);
m_buttonClick.transition = Selectable.Transition.None;
m_buttonClick.onClick.AddListener(OnButtonClick);
}
SetEventParam(arg);
}
private void OnButtonClick()
{
if (m_clickAction != null)
{
//AudioSys.GameMgr.PlayUISoundEffect(UiSoundType.UI_SOUND_CLICK);
m_clickAction(this as T);
}
}
public void BindBeginDragEvent(Action<T, PointerEventData> dragAction, params object[] arg)
{
if (m_beginDragAction != null)
{
m_beginDragAction = dragAction;
}
else
{
m_beginDragAction = dragAction;
var trigger = UnityUtil.AddMonoBehaviour<EventTrigger>(gameObject);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.BeginDrag;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerBeginDrag);
trigger.triggers.Add(entry);
}
SetEventParam(arg);
}
private void OnTriggerBeginDrag(BaseEventData data)
{
var pointerEventData = (PointerEventData)data;
if (m_beginDragAction != null)
{
m_beginDragAction(this as T, pointerEventData);
}
}
public void BindDragEvent(Action<T, PointerEventData> dragAction, params object[] arg)
{
if (m_dragAction != null)
{
m_dragAction = dragAction;
}
else
{
m_dragAction = dragAction;
var trigger = UnityUtil.AddMonoBehaviour<EventTrigger>(gameObject);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerDrag);
trigger.triggers.Add(entry);
}
SetEventParam(arg);
}
private void OnTriggerDrag(BaseEventData data)
{
var pointerEventData = (PointerEventData)data;
if (m_dragAction != null)
{
m_dragAction(this as T, pointerEventData);
}
}
public void BindEndDragEvent(Action<T, PointerEventData> dragendAction, params object[] arg)
{
if (m_endDragAction != null)
{
m_endDragAction = dragendAction;
}
else
{
m_endDragAction = dragendAction;
var trigger = UnityUtil.AddMonoBehaviour<EventTrigger>(gameObject);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.EndDrag;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerEndDrag);
trigger.triggers.Add(entry);
}
SetEventParam(arg);
}
private void OnTriggerEndDrag(BaseEventData data)
{
if (m_endDragAction != null)
{
m_endDragAction(this as T, (PointerEventData)data);
}
}
public void BindPressEvent(Action<T, bool, PointerEventData> pressAction, params object[] arg)
{
if (m_pressAction != null)
{
m_pressAction = pressAction;
}
else
{
m_pressAction = pressAction;
var trigger = UnityUtil.AddMonoBehaviour<EventTrigger>(gameObject);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerDown;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerPointerDown);
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerUp;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnTriggerPointerUp);
trigger.triggers.Add(entry);
}
SetEventParam(arg);
}
private void OnTriggerPointerUp(BaseEventData data)
{
if (m_pressAction != null)
{
m_pressAction(this as T, false, data as PointerEventData);
}
}
private void OnTriggerPointerDown(BaseEventData data)
{
if (m_pressAction != null)
{
m_pressAction(this as T, true, data as PointerEventData);
}
}
public void SetEventParam(params object[] arg)
{
m_eventParam = arg;
}
}
}

View File

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

View File

@@ -0,0 +1,177 @@
using UnityEngine;
namespace TEngine.Runtime.UIModule
{
/// <summary>
/// 用来封装各个界面里子模块用
/// </summary>
public class UIWindowWidget : UIWindowBase
{
public int SortingOrder
{
get
{
if (m_canvas != null)
{
return m_canvas.sortingOrder;
}
return 0;
}
set
{
if (m_canvas != null)
{
int oldOrder = m_canvas.sortingOrder;
if (oldOrder != value)
{
var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true);
for (int i = 0; i < listCanvas.Length; i++)
{
var childCanvas = listCanvas[i];
childCanvas.sortingOrder = value + (childCanvas.sortingOrder - oldOrder);
}
m_canvas.sortingOrder = value;
_OnSortingOrderChg();
}
}
}
}
/// <summary>
/// 所属的窗口
/// </summary>
public UIWindow OwnerWindow
{
get
{
var parent = m_parent;
while (parent != null)
{
if (parent.BaseType == UIWindowBaseType.Window)
{
return parent as UIWindow;
}
parent = parent.Parent;
}
return null;
}
}
public override UIWindowBaseType BaseType
{
get { return UIWindowBaseType.Widget; }
}
/// <summary> 根据类型创建 </summary>
public bool CreateByType<T>(UIWindowBase parent, Transform parentTrans = null) where T : UIWindowWidget
{
string resPath = string.Format("UI/{0}.prefab", typeof(T).Name);
return CreateByPath(resPath, parent, parentTrans);
}
/// <summary> 根据资源名创建 </summary>
public bool CreateByPath(string resPath, UIWindowBase parent, Transform parentTrans = null, bool visible = true)
{
GameObject goInst = TResources.Load(resPath, parentTrans);
if (goInst == null)
{
return false;
}
if (!Create(parent, goInst, visible))
{
return false;
}
goInst.transform.localScale = Vector3.one;
goInst.transform.localPosition = Vector3.zero;
return true;
}
/**
* 根据prefab或者模版来创建新的 widget
*/
public bool CreateByPrefab(UIWindowBase parent, GameObject goPrefab, Transform parentTrans, bool visible = true)
{
if (parentTrans == null)
{
parentTrans = parent.transform;
}
var widgetRoot = GameObject.Instantiate(goPrefab, parentTrans);
return CreateImp(parent, widgetRoot, true, visible);
}
/**
* 创建窗口内嵌的界面
*/
public bool Create(UIWindowBase parent, GameObject widgetRoot, bool visible = true)
{
return CreateImp(parent, widgetRoot, false, visible);
}
#region
private bool CreateImp(UIWindowBase parent, GameObject widgetRoot, bool bindGo, bool visible = true)
{
if (!CreateBase(widgetRoot, bindGo))
{
return false;
}
RestChildCanvas(parent);
m_parent = parent;
if (m_parent != null)
{
m_parent.AddChild(this);
}
if (m_canvas != null)
{
m_canvas.overrideSorting = true;
}
ScriptGenerator();
BindMemberProperty();
RegisterEvent();
OnCreate();
if (visible)
{
Show(true);
}
else
{
widgetRoot.SetActive(false);
}
return true;
}
private void RestChildCanvas(UIWindowBase parent)
{
if (gameObject == null)
{
return;
}
if (parent == null || parent.gameObject == null)
{
return;
}
Canvas parentCanvas = parent.gameObject.GetComponentInParent<Canvas>();
if (parentCanvas == null)
{
return;
}
var listCanvas = gameObject.GetComponentsInChildren<Canvas>(true);
for (var index = 0; index < listCanvas.Length; index++)
{
var childCanvas = listCanvas[index];
childCanvas.sortingOrder = parentCanvas.sortingOrder + childCanvas.sortingOrder % UIWindow.MaxCanvasSortingOrder;
}
}
#endregion
}
}

View File

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