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-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()
|
|
|
|
|
|
{
|
|
|
|
|
|
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-04-21 11:25:52 +08:00
|
|
|
|
tooltips.transform.Find("Text").GetComponent<Text>().text = Tooltips;
|
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void BindEvent()
|
|
|
|
|
|
{
|
|
|
|
|
|
//鼠标进入
|
|
|
|
|
|
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))
|
|
|
|
|
|
{
|
|
|
|
|
|
tooltips.GetComponent<CanvasGroup>().alpha = 0f;
|
|
|
|
|
|
tooltips.SetActive(true);
|
|
|
|
|
|
tooltips.GetComponent<CanvasGroup>().DOFade(1f, 0.2f);
|
|
|
|
|
|
}
|
2021-08-27 15:06:56 +08:00
|
|
|
|
#endif
|
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-04-21 11:25:52 +08:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(Tooltips))
|
|
|
|
|
|
{
|
|
|
|
|
|
tooltips.SetActive(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
//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:
|
|
|
|
|
|
{
|
|
|
|
|
|
tooltips.SetActive(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|