UI左右切换数据

来源:互联网 发布:新开淘宝店如何刷信誉 编辑:程序博客网 时间:2024/06/03 04:15
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class Select : MonoBehaviour {

    protected int currentIndex = 0, totalCount;
    public int CurrentIndex { get { return currentIndex; } }


    public void OnNextClick(GameObject go){
        currentIndex++;
        OnIndexChange();
    }

    public void OnPreviousClick(GameObject go){
        currentIndex--;
        OnIndexChange();
    }
    protected void OnIndexChange(){
        HandleBounds();
        AssignByIndex();
    }
    protected void HandleBounds(){
        if (currentIndex < 0){
            currentIndex = totalCount - 1;
        }
        if (currentIndex >= totalCount){
            currentIndex = 0;
        }
    }


    protected virtual void AssignByIndex() { }

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ModleSelect : Select {


    public DetailList infos;
    public GameObject nextButton, previousButton;

    private void Start(){
        UGUIEventListener.Get(nextButton).onClick += OnNextClick;
        UGUIEventListener.Get(previousButton).onClick += OnPreviousClick;
    }
    protected override void AssignByIndex()
    {
        DetailInfo info = infos.GetDetails(CurrentIndex);
        ShowInfos(info);
    }
    void ShowInfos(DetailInfo info) { }

}

using System.Reflection;
using UnityEngine;
using UnityEngine.EventSystems;


public class UGUIEventListener : EventTrigger
{
    public delegate void VoidDelegate(GameObject go);
    public VoidDelegate onClick;
    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onUpdateSelect;


    static public UGUIEventListener Get(GameObject go)
    {
        UGUIEventListener listener = go.GetComponent<UGUIEventListener>();
        if (listener == null) listener = go.AddComponent<UGUIEventListener>();
        return listener;
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null) onClick(gameObject);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown(gameObject);
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onEnter != null) onEnter(gameObject);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null) onExit(gameObject);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null) onUp(gameObject);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect(gameObject);
    }


    public void ClearClickEvent()
    {
        if (onClick != null)
        {
            foreach (VoidDelegate item in onClick.GetInvocationList())
            {
                MethodInfo m = item.Method;
                if (!m.Name.Contains("ChangeButtonState"))
                {
                    onClick -= item;
                }
            }
        }
    }
}