Skip to content

Instantly share code, notes, and snippets.

@reidscarboro
Created November 3, 2017 17:39
Show Gist options
  • Save reidscarboro/aa790b3e8940520d6e83a3f397107e85 to your computer and use it in GitHub Desktop.
Save reidscarboro/aa790b3e8940520d6e83a3f397107e85 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using DG.Tweening;
public class ButtonEnlarger : MonoBehaviour {
private Button button;
private Vector3 initialScale;
public float scaleFactor = 1.2f;
public float scaleTime = 0.1f;
private bool isEnabled = true;
void Awake()
{
initialScale = transform.localScale;
button = GetComponent<Button>();
EventTrigger trigger = gameObject.AddComponent<EventTrigger>();
var pointerDown = new EventTrigger.Entry();
pointerDown.eventID = EventTriggerType.PointerDown;
pointerDown.callback.AddListener((e) => {
if (isEnabled && button.interactable && transform.localScale.x > 0.001f)
{
DOTween.Kill("buttonScale");
transform.DOScale(initialScale * scaleFactor, scaleTime).SetId("buttonScale");
}
});
trigger.triggers.Add(pointerDown);
var pointerUp = new EventTrigger.Entry();
pointerUp.eventID = EventTriggerType.PointerUp;
pointerUp.callback.AddListener((e) => {
if (isEnabled && button.interactable && transform.localScale.x > 0.001f)
{
DOTween.Kill("buttonScale");
transform.DOScale(initialScale, scaleTime).SetId("buttonScale");
}
});
trigger.triggers.Add(pointerUp);
}
public void SetEnabled(bool e)
{
isEnabled = e;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment