109 lines
2.3 KiB
C#
109 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using System;
|
|
|
|
|
|
public class NewButton : Button
|
|
{
|
|
public bool m_IsOn;
|
|
|
|
private bool isPointerInside;
|
|
public bool m_IsPointerInside { get => isPointerInside; protected set => isPointerInside = value; }
|
|
|
|
private bool findText = false;
|
|
private Text text;
|
|
/// <summary>
|
|
/// 按钮文本
|
|
/// </summary>
|
|
public Text m_Text
|
|
{
|
|
get
|
|
{
|
|
if (!findText && text == null)
|
|
{
|
|
findText = true;
|
|
text = GetComponentInChildren<Text>(true);
|
|
}
|
|
return text;
|
|
}
|
|
protected set => text = value;
|
|
}
|
|
public class Click : UnityEvent<bool> { }
|
|
|
|
public Click m_onClick = new Click();
|
|
|
|
/// <summary>
|
|
/// 是否按住
|
|
/// </summary>
|
|
public bool m_boolPressHold;
|
|
|
|
public Action m_actionPressHold;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
onClick.AddListener(OnClick);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (m_boolPressHold)
|
|
{
|
|
m_actionPressHold?.Invoke();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 点击事件
|
|
/// </summary>
|
|
private void OnClick()
|
|
{
|
|
m_IsOn = !m_IsOn;
|
|
m_onClick?.Invoke(m_IsOn);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同步文本内容 编辑器执行
|
|
/// </summary>
|
|
public void SyncText()
|
|
{
|
|
if (m_Text == null)
|
|
m_Text = GetComponentInChildren<Text>();
|
|
if (m_Text != null)
|
|
m_Text.text = name;
|
|
}
|
|
|
|
protected override void OnDisable()
|
|
{
|
|
base.OnDisable();
|
|
isPointerInside = false;
|
|
}
|
|
|
|
public override void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
base.OnPointerEnter(eventData);
|
|
isPointerInside = true;
|
|
}
|
|
|
|
public override void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
base.OnPointerExit(eventData);
|
|
isPointerInside = false;
|
|
}
|
|
|
|
public override void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
base.OnPointerDown(eventData);
|
|
m_boolPressHold = true;
|
|
}
|
|
|
|
public override void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
base.OnPointerUp(eventData);
|
|
m_boolPressHold = false;
|
|
}
|
|
} |