TouhouLS/Assets/RealCode/Menu/ButtonS/MainMenuButton.cs

67 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MainMenuButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler, IPointerDownHandler, IPointerUpHandler {
[Header("References")]
public Image targetGraphic;
public RectTransform targetTransform;
public bool pointerSelected;
public bool keyboardSelected;
public bool pressed;
[Header("Style")]
public Color baseColor;
public Color selectedColor;
public float baseWidth;
public float selectedWidth;
public Color tintColor;
public void OnPointerEnter(PointerEventData eventData) {
pointerSelected = true;
}
public void OnPointerExit(PointerEventData eventData) {
pointerSelected = false;
}
public void OnSelect(BaseEventData eventData) {
keyboardSelected = true;
}
public void OnDeselect(BaseEventData eventData) {
keyboardSelected = false;
}
public void OnPointerDown(PointerEventData eventData) {
pressed = true;
}
public void OnPointerUp(PointerEventData eventData) {
pressed = false;
}
void Start(){
var b = GetComponent<Button>();
if (b){
b.onClick.RemoveAllListeners();
var click = GetComponent<ButtonOnClick>();
if (click)
b.onClick.AddListener(click.OnClick);
}
}
// Update is called once per frame
void Update() {
var selected = keyboardSelected || pointerSelected;
targetGraphic.color = (selected ? selectedColor : baseColor) * (pressed ? tintColor : Color.white);
targetTransform.sizeDelta = new Vector2(selected ? selectedWidth : baseWidth, targetTransform.sizeDelta.y);
}
}