359 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using DG.Tweening;
using System.Timers;
using System.Collections;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
namespace Assets.Scripts.UI.Control
{
[RequireComponent(typeof(Button))]
public class PfUIButton : PFUIComponentBase
{
public enum Type
{
Normal,
Border,
Primary,
Image,
Transparent
}
[SerializeField] Type mType;
[SerializeField] string Tooltips;
private GameObject tooltips;
private Image image;
private Text text;
//private Texture2D cursor;
private Outline outline;
private Button mButton;
private Sequence se = null;
private Sequence a;
private TweenerCore<float, float, FloatOptions> b;
private TweenerCore<float, float, FloatOptions> c;
protected void Awake()
{
#if !(UNITY_ANDROID || UNITY_IOS)
//pc版依然让tooltip变成下面的
var newtool = transform.Find("Tooltips").gameObject;
var newrect = newtool.GetComponent<RectTransform>();
newrect.anchorMin = new Vector2(0.5f, 1);
newrect.anchorMax = new Vector2(0.5f, 1);
newrect.pivot = new Vector2(0.5f, 1);
newrect.anchoredPosition = new Vector2(0, -50);
//newrect.transform.localPosition = newrect.GetTruePosition(new Vector2(0, -50));
// -16 -66 0 -50
#endif
outline = this.GetComponent<Outline>();
if (mType == Type.Border && outline == null)
{
outline = this.gameObject.AddComponent<Outline>();
outline.effectDistance = new Vector2(2, 2);
}
//设置光标
//cursor = Resources.Load<Texture2D>("Images/PointerButtonHover");
image = gameObject.GetComponent<Image>();
text = this.transform.Find("Text").GetComponent<Text>();
if(mType == Type.Image)
{
text.gameObject.SetActive(false);
}
else
{
Material material = null;
if (material == null)
{
material = Instantiate(Resources.Load<Material>("UI/Material/RoundedCornersTextureMaterial"));
}
var rect = ((RectTransform)transform).rect;
material.SetVector(Shader.PropertyToID("_WidthHeightRadius"), new Vector4(rect.width, rect.height, rect.height * 0.5f, 0));
image.material = material;
}
tooltips = this.transform.Find("Tooltips").gameObject;
if (!string.IsNullOrWhiteSpace(Tooltips))
{
//
//tooltips.GetComponent<RectTransform>().localPosition.
tooltips.transform.Find("Text").GetComponent<Text>().text = App.GetLocalString(Tooltips);
var pftext = tooltips.transform.Find("Text").GetComponent<PFUIText>();
if (pftext)
{
tooltips.transform.Find("Text").GetComponent<PFUIText>().key = Tooltips;
}
}
tooltips.SetActive(false);
mButton = this.GetComponent<Button>();
InitColor();
BindEvent();
}
private void InitColor()
{
text.color = Color.white;
switch (mType)
{
case Type.Normal:
{
image.color = ConvertColor("#414251");
}
break;
case Type.Border:
{
outline.enabled = true;
outline.effectColor = ConvertColor("#474759");
text.color = ConvertColor("#474759");
image.color = ConvertColor("#272732");
}
break;
case Type.Primary:
{
image.color = ConvertColor("#F93086");
}
break;
case Type.Transparent:
{
image.color = Color.clear;
}
break;
default:
break;
}
}
public bool showTooltip { get; set; }
private void BindEvent()
{
Timer timer = new Timer();
timer.Interval = 1000;
var group = tooltips.GetComponent<CanvasGroup>();
timer.Elapsed += (r, s) =>
{
timer.Stop();
group.DOFade(1, 1f);
};
//鼠标进入
UIManager.AddEvent(this.gameObject, EventTriggerType.PointerEnter, new UnityEngine.Events.UnityAction<UnityEngine.EventSystems.BaseEventData>(e =>
{
if (!this.isActiveAndEnabled) return;
//Cursor.SetCursor(cursor, Vector2.zero, CursorMode.Auto);
#if !(UNITY_ANDROID || UNITY_IOS)
if (!string.IsNullOrWhiteSpace(Tooltips))
{
#else
if (!string.IsNullOrWhiteSpace(Tooltips) && showTooltip)
{
#endif
//tooltips.SetActive(true);
//tooltips.GetComponent<CanvasGroup>().DOFade(1, 0.5f);
//tooltips.GetComponent<CanvasGroup>().DOFade(1, 0.5f);
//DOTween.CompleteAll(true);
//se = DOTween.Sequence();
//se.SetDelay(1);
//se.AppendCallback(() =>
//{
// if(a!=null) a.Complete(true);
// if (b != null) b.Complete(true);
// tooltips.SetActive(true);
// tooltips.GetComponent<CanvasGroup>().alpha = 0f;
// c = tooltips.GetComponent<CanvasGroup>().DOFade(1, 0.5f);
//});
//a = se.Play();
tooltips.SetActive(true);
tooltips.GetComponent<CanvasGroup>().alpha = 0f;
//StartCoroutine(DelayShowTooltip());
timer.Start();
}
switch (mType)
{
case Type.Normal:
{
image.color = ConvertColor("#5C5C6E");
}
break;
case Type.Border:
{
outline.effectColor = Color.white;
text.color = ConvertColor("#fff");
}
break;
case Type.Primary:
{
image.color = ConvertColor("#FF528A");
}
break;
default:
break;
}
}));
//鼠标离开
UIManager.AddEvent(this.gameObject, EventTriggerType.PointerExit, new UnityAction<BaseEventData>(e =>
{
if (!this.isActiveAndEnabled) return;
timer.Stop();
#if !(UNITY_ANDROID || UNITY_IOS)
if (!string.IsNullOrWhiteSpace(Tooltips))
{
#else
if (!string.IsNullOrWhiteSpace(Tooltips) && showTooltip)
{
#endif
group.DOFade(0, 1f).onComplete += () => {
tooltips.SetActive(false);
};
}
//Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
switch (mType)
{
case Type.Normal:
{
image.color = ConvertColor("#414251");
}
break;
case Type.Border:
{
outline.effectColor = ConvertColor("#474759");
text.color = ConvertColor("#474759");
}
break;
case Type.Primary:
{
image.color = ConvertColor("#F93086");
}
break;
default:
break;
}
}));
//按下
UIManager.AddEvent(this.gameObject, EventTriggerType.PointerDown, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
{
if (!this.isActiveAndEnabled) return;
//if (a != null) a.Complete(true);
//if (b != null) b.Complete(true);
//if (c != null) c.Complete(true);
tooltips.SetActive(false);
//else
//{
// DOTween.CompleteAll(true);
// tooltips.SetActive(true);
// tooltips.GetComponent<CanvasGroup>().alpha = 0f;
// tooltips.GetComponent<CanvasGroup>().DOFade(1, 0.5f);
//}
switch (mType)
{
case Type.Normal:
{
image.color = ConvertColor("#23232D");
}
break;
case Type.Border:
{
outline.effectColor = ConvertColor("#353543");
text.color = ConvertColor("#414251");
image.color = ConvertColor("#23232D");
}
break;
case Type.Primary:
{
image.color = ConvertColor("#BD2255");
}
break;
case Type.Image:
{
tooltips.SetActive(tooltips.activeSelf);
}
break;
default:
break;
}
}));
//松开
UIManager.AddEvent(this.gameObject, EventTriggerType.PointerUp, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
{
if (!this.isActiveAndEnabled) return;
//Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
switch (mType)
{
case Type.Normal:
{
image.color = ConvertColor("#5C5C6E");
}
break;
case Type.Border:
{
outline.effectColor = Color.white;
text.color = Color.white;
}
break;
case Type.Primary:
{
image.color = ConvertColor("#FF528A");
}
break;
case Type.Image:
{
tooltips.SetActive(false);
}
break;
default:
break;
}
}));
}
public void SetEnabled(bool value)
{
base.enabled = value;
mButton.enabled = value;
if (base.enabled)
{
InitColor();
}
else
{
text.color = ConvertColor("#353543");
if (outline != null)
{
outline.enabled = false;
}
image.color = ConvertColor("#414251");
}
}
private Color ConvertColor(string colorStr)
{
ColorUtility.TryParseHtmlString(colorStr, out Color color);
return color;
}
private void OnDestroy()
{
}
}
}