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);
}
}