unity单选列表

1 功能描述

制作一个单选列表,在导入资源,选择资源等使用,要求鼠标进入时可以有颜色改变或者其他效果可扩展,单击选择回调,其他的会自动取消选择状态

2 准备

1)创建工程如下图,添加一个Scroll View
在这里插入图片描述

2)在Scroll View下找到content添加GridLayoutGroup组件 如下图
在这里插入图片描述

3 创建脚本

1)Dan_ChoosePanel 单选界面,脚本如下

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

public class Dan_ChoosePanel : MonoBehaviour
{
    public Transform G_Parent;
    public GameObject G_Prefber;
    List<GameObject> G_Objects = new List<GameObject>();
    public System.Action<DanDataBase> OnItemClickBack = null;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void ClearButtons() {
        for (int i = 0; i < G_Objects.Count; i++)
        {
            if (G_Objects[i] != null) {
                Destroy(G_Objects[i]);
            }
        }
        G_Objects.Clear();
    }
    public void ShowPanel(List<DanDataBase> lst)
    {
        ClearButtons();
        for (int i = 0; i < lst.Count; i++)
        {
            GameObject tmp = Instantiate(G_Prefber, G_Parent);
            tmp.transform.localScale = Vector3.one;
            tmp.GetComponent<DanItemBase>().SetDanItem(lst[i], OnItemClickBack);
            tmp.GetComponent<DanItemBase>().SetIDClick(i, OnClickBack);
            G_Objects.Add(tmp);
        }
        this.gameObject.SetActive(true);
    }
    void OnClickBack(int id) {
        for (int i = 0; i < G_Objects.Count; i++)
        {
            if (id == i) {
                G_Objects[i].GetComponent<DanItemBase>().OnChoose();
            } else {
                G_Objects[i].GetComponent<DanItemBase>().CancleChoose();
            }
        }
    }
    public void ClosePanel()
    {
        this.gameObject.SetActive(false);
    }
}

2)DanDataBase 要显示的内容,脚本如下

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

public class DanDataBase 
{
    public DanDataBase() { }
    public DanDataBase(string m) {
        ButtonName = m;
    }
    public string ButtonName;
}

3)Dan_test 用来测试的脚本,如下

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

public class Dan_test : MonoBehaviour
{
    public Dan_ChoosePanel mDan_ChoosePanel;
    // Start is called before the first frame update
    void Start()
    {
        List<DanDataBase> mylist = new List<DanDataBase>();
        for (int i = 0; i < 30; i++)
        {
            mylist.Add(new DanDataBase (""+i));
        }
        mDan_ChoosePanel.OnItemClickBack = OnItemClickBack;
        mDan_ChoosePanel.ShowPanel(mylist);
    }

    private void OnItemClickBack(DanDataBase obj)
    {
        Debug.Log(obj.ButtonName);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

4)DanItemBase 负责显示内容

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class DanItemBase : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
{
    protected int IndexID = 0;
    protected System.Action<int> OnIdCallback;
    protected System.Action<DanDataBase> OnDataCallBack;
    protected DanDataBase mdata;
    public bool IsInChoose = false;
    public virtual void SetDanItem(DanDataBase data,System.Action<DanDataBase> cb) {
        mdata = data;
        OnDataCallBack = cb;
    }
    public  void SetIDClick(int id, System.Action<int> cb) {
        IndexID = id;
        OnIdCallback = cb;
    }
    public virtual void OnChoose() {
        IsInChoose = true;
    }
    public virtual void CancleChoose() {
        IsInChoose = false;
    }
    public virtual void OnMouseIn()
    {

    }
    public virtual void OnMouseOut()
    {

    }
 
    public virtual void OnButtonClick() {
        if (OnIdCallback != null) {
            OnIdCallback(IndexID);
        }
        if (OnDataCallBack != null) {
            OnDataCallBack(mdata);
        }
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        OnMouseIn();
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        OnMouseOut();
    }
}

5)DanTextButton 增加一个只显示文字和颜色变化的单选列表按钮

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DanTextButton : DanItemBase
{
    public Color NormalColor=Color.white;
    public Color ChooseColor=Color.yellow;

    public Color MouseInColor = Color.blue;
 
    public Text mtxt;
    public Image colorobj;

    // Start is called before the first frame update
    void Start()
    {
        CancleChoose();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public override void OnMouseIn()
    {
        colorobj.color = MouseInColor;
    }
    public override void OnMouseOut()
    {
        if (IsInChoose)
        {
            colorobj.color = ChooseColor;
        }
        else {
            colorobj.color = NormalColor;
        }
    }
    public override void OnChoose()
    {
        colorobj.color = ChooseColor;
        base.OnChoose();
    }
    public override void CancleChoose()
    {
        colorobj.color = NormalColor;
        base.CancleChoose();
    }
    public override void SetDanItem(DanDataBase data, Action<DanDataBase> cb)
    {
        mtxt.text = data.ButtonName;
        base.SetDanItem(data, cb);
    }
}

4 制作预制体

在这里插入图片描述

5 设置Dan_ChoosePanel

在这里插入图片描述

6 设置Dan_test

在这里插入图片描述

7 测试

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值