ugui自带了17种监听事件,作为界面和硬件交互的方式,利用好这些方式,可以更简单,更省性能地实现ui功能。
在斗地主选牌这里,需求要点击选牌,滑动选牌,由于windows平台与Android,ios平台的差异,在windows平台里需要监听进入事件和点击事件,在移动平台上只需实现进入事件,也就是Enter事件。
首先为预制体添加button脚本作为触发事件的载体,下面贴出挂在预制体上的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Card : MonoBehaviour
{
public int _cardId;
private Vector3 upVector3, localVector3;
[SerializeField]
private bool isCardUp;
public bool _isCardUp
{
get { return isCardUp; }
set
{
if (upVector3 == Vector3.zero)
{
upVector3 = transform.localPosition + new Vector3(0, 30, 0);
localVector3 = transform.localPosition;
}
isCardUp = value;
if (isCardUp)
{
transform.localPosition = upVector3;
}
else
{
transform.localPosition = localVector3;
}
}
}
void Start()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
EventTriggerListener.Get(gameObject).onDown -= Down;
EventTriggerListener.Get(gameObject).onEnter -= Enter;
EventTriggerListener.Get(gameObject).onEnter += Enter;