2021-04-09 09:44:06 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
using UnityEngine.EventSystems;
|
2021-04-21 11:25:52 +08:00
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
using DG.Tweening;
|
2021-12-24 18:33:58 +08:00
|
|
|
|
using System.Timers;
|
2021-04-09 09:44:06 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Assets.Scripts.UI.Control
|
|
|
|
|
|
{
|
|
|
|
|
|
[RequireComponent(typeof(Button))]
|
2021-04-15 17:09:35 +08:00
|
|
|
|
public class PfUIButton : PFUIComponentBase
|
2021-04-09 09:44:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
public enum Type
|
|
|
|
|
|
{
|
|
|
|
|
|
Normal,
|
|
|
|
|
|
Border,
|
2021-04-21 11:25:52 +08:00
|
|
|
|
Primary,
|
|
|
|
|
|
Image
|
2021-04-09 09:44:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[SerializeField] Type mType;
|
2021-04-21 11:25:52 +08:00
|
|
|
|
[SerializeField] string Tooltips;
|
|
|
|
|
|
private GameObject tooltips;
|
2021-04-09 09:44:06 +08:00
|
|
|
|
private Image image;
|
2021-04-21 11:25:52 +08:00
|
|
|
|
private Text text;
|
|
|
|
|
|
//private Texture2D cursor;
|
2021-04-09 09:44:06 +08:00
|
|
|
|
private Outline outline;
|
2021-04-21 11:25:52 +08:00
|
|
|
|
private Button mButton;
|
2021-04-14 15:02:33 +08:00
|
|
|
|
|
2021-04-09 09:44:06 +08:00
|
|
|
|
protected void Awake()
|
|
|
|
|
|
{
|
2021-12-28 11:25:09 +08:00
|
|
|
|
|
|
|
|
|
|
#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
|
2021-04-09 09:44:06 +08:00
|
|
|
|
outline = this.GetComponent<Outline>();
|
|
|
|
|
|
if (mType == Type.Border && outline == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
outline = this.gameObject.AddComponent<Outline>();
|
|
|
|
|
|
outline.effectDistance = new Vector2(2, 2);
|
|
|
|
|
|
}
|
|
|
|
|
|
//设置光标
|
2021-04-21 11:25:52 +08:00
|
|
|
|
//cursor = Resources.Load<Texture2D>("Images/PointerButtonHover");
|
2021-04-09 09:44:06 +08:00
|
|
|
|
|
|
|
|
|
|
image = gameObject.GetComponent<Image>();
|
|
|
|
|
|
text = this.transform.Find("Text").GetComponent<Text>();
|
2021-04-21 11:25:52 +08:00
|
|
|
|
if(mType == Type.Image)
|
2021-04-09 09:44:06 +08:00
|
|
|
|
{
|
2021-04-21 11:25:52 +08:00
|
|
|
|
text.gameObject.SetActive(false);
|
2021-04-09 09:44:06 +08:00
|
|
|
|
}
|
2021-04-21 11:25:52 +08:00
|
|
|
|
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))
|
|
|
|
|
|
{
|
|
|
|
|
|
//
|
2021-04-27 18:26:30 +08:00
|
|
|
|
//tooltips.GetComponent<RectTransform>().localPosition.
|
2021-11-25 16:32:08 +08:00
|
|
|
|
tooltips.transform.Find("Text").GetComponent<Text>().text = App.GetLocalString(Tooltips);
|
|
|
|
|
|
var pftext = tooltips.transform.Find("Text").GetComponent<PFUIText>();
|
|
|
|
|
|
Debug.Log(pftext);
|
|
|
|
|
|
if (pftext)
|
|
|
|
|
|
{
|
|
|
|
|
|
tooltips.transform.Find("Text").GetComponent<PFUIText>().key = Tooltips;
|
|
|
|
|
|
}
|
2021-04-21 11:25:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
tooltips.SetActive(false);
|
|
|
|
|
|
mButton = this.GetComponent<Button>();
|
2021-04-09 09:44:06 +08:00
|
|
|
|
|
|
|
|
|
|
InitColor();
|
|
|
|
|
|
BindEvent();
|
|
|
|
|
|
}
|
|
|
|
|
|
private void InitColor()
|
|
|
|
|
|
{
|
|
|
|
|
|
text.color = Color.white;
|
|
|
|
|
|
switch (mType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Type.Normal:
|
|
|
|
|
|
{
|
2021-07-28 14:59:21 +08:00
|
|
|
|
image.color = ConvertColor("#414251");
|
2021-04-09 09:44:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Type.Border:
|
|
|
|
|
|
{
|
|
|
|
|
|
outline.enabled = true;
|
2021-04-14 15:02:33 +08:00
|
|
|
|
outline.effectColor = ConvertColor("#474759");
|
2021-04-29 11:36:30 +08:00
|
|
|
|
text.color = ConvertColor("#474759");
|
|
|
|
|
|
image.color = ConvertColor("#272732");
|
2021-04-09 09:44:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Type.Primary:
|
|
|
|
|
|
{
|
|
|
|
|
|
image.color = ConvertColor("#F93086");
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-11-18 14:45:31 +08:00
|
|
|
|
public bool showTooltip { get; set; }
|
2021-12-24 18:33:58 +08:00
|
|
|
|
|
2021-04-09 09:44:06 +08:00
|
|
|
|
private void BindEvent()
|
|
|
|
|
|
{
|
2021-12-24 18:33:58 +08:00
|
|
|
|
Timer timer = new Timer();
|
|
|
|
|
|
timer.Interval = 1000;
|
|
|
|
|
|
var group = tooltips.GetComponent<CanvasGroup>();
|
|
|
|
|
|
timer.Elapsed += (r, s) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
timer.Stop();
|
|
|
|
|
|
group.DOFade(1, 0.5f);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-04-09 09:44:06 +08:00
|
|
|
|
//鼠标进入
|
|
|
|
|
|
UIManager.AddEvent(this.gameObject, EventTriggerType.PointerEnter, new UnityEngine.Events.UnityAction<UnityEngine.EventSystems.BaseEventData>(e =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!this.isActiveAndEnabled) return;
|
2021-04-21 11:25:52 +08:00
|
|
|
|
//Cursor.SetCursor(cursor, Vector2.zero, CursorMode.Auto);
|
2021-08-27 15:06:56 +08:00
|
|
|
|
#if !(UNITY_ANDROID || UNITY_IOS)
|
2021-04-21 11:25:52 +08:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(Tooltips))
|
|
|
|
|
|
{
|
2021-11-18 14:45:31 +08:00
|
|
|
|
#else
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(Tooltips) && showTooltip)
|
|
|
|
|
|
{
|
|
|
|
|
|
#endif
|
2021-04-21 11:25:52 +08:00
|
|
|
|
tooltips.GetComponent<CanvasGroup>().alpha = 0f;
|
|
|
|
|
|
tooltips.SetActive(true);
|
2021-12-24 18:33:58 +08:00
|
|
|
|
timer.Start();
|
2021-04-21 11:25:52 +08:00
|
|
|
|
}
|
2021-04-09 09:44:06 +08:00
|
|
|
|
switch (mType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Type.Normal:
|
|
|
|
|
|
{
|
|
|
|
|
|
image.color = ConvertColor("#5C5C6E");
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Type.Border:
|
|
|
|
|
|
{
|
|
|
|
|
|
outline.effectColor = Color.white;
|
2021-04-29 11:36:30 +08:00
|
|
|
|
text.color = ConvertColor("#fff");
|
2021-04-09 09:44:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Type.Primary:
|
|
|
|
|
|
{
|
|
|
|
|
|
image.color = ConvertColor("#FF528A");
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
//鼠标离开
|
2021-04-21 11:25:52 +08:00
|
|
|
|
UIManager.AddEvent(this.gameObject, EventTriggerType.PointerExit, new UnityAction<BaseEventData>(e =>
|
2021-04-09 09:44:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!this.isActiveAndEnabled) return;
|
2021-12-24 18:33:58 +08:00
|
|
|
|
timer.Stop();
|
2021-11-19 10:54:14 +08:00
|
|
|
|
#if !(UNITY_ANDROID || UNITY_IOS)
|
2021-04-21 11:25:52 +08:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(Tooltips))
|
|
|
|
|
|
{
|
2021-11-19 10:54:14 +08:00
|
|
|
|
#else
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(Tooltips) && showTooltip)
|
|
|
|
|
|
{
|
2021-12-24 18:33:58 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
group.DOFade(0, 1f).onComplete += () => {
|
|
|
|
|
|
tooltips.SetActive(false);
|
|
|
|
|
|
};
|
2021-04-21 11:25:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
//Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
|
2021-04-09 09:44:06 +08:00
|
|
|
|
switch (mType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Type.Normal:
|
|
|
|
|
|
{
|
2021-07-28 14:59:21 +08:00
|
|
|
|
image.color = ConvertColor("#414251");
|
2021-04-09 09:44:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Type.Border:
|
|
|
|
|
|
{
|
|
|
|
|
|
outline.effectColor = ConvertColor("#474759");
|
2021-04-29 11:36:30 +08:00
|
|
|
|
text.color = ConvertColor("#474759");
|
2021-04-09 09:44:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
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;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
//松开
|
|
|
|
|
|
UIManager.AddEvent(this.gameObject, EventTriggerType.PointerUp, new UnityEngine.Events.UnityAction<BaseEventData>(e =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!this.isActiveAndEnabled) return;
|
2021-04-21 11:25:52 +08:00
|
|
|
|
//Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
|
2021-04-09 09:44:06 +08:00
|
|
|
|
|
|
|
|
|
|
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;
|
2021-04-25 15:33:17 +08:00
|
|
|
|
case Type.Image:
|
|
|
|
|
|
{
|
2021-11-01 11:04:22 +08:00
|
|
|
|
//tooltips.SetActive(false);
|
2021-04-25 15:33:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2021-04-09 09:44:06 +08:00
|
|
|
|
default:
|
2021-04-25 15:33:17 +08:00
|
|
|
|
|
2021-04-09 09:44:06 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void SetEnabled(bool value)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.enabled = value;
|
2021-04-21 11:25:52 +08:00
|
|
|
|
mButton.enabled = value;
|
2021-04-09 09:44:06 +08:00
|
|
|
|
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()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|