【UGUI】无限循环列表和InputField.onValueChanged 事件

23 篇文章 0 订阅
15 篇文章 1 订阅

先看一下效果

其中应用了InputField.onValueChanged  属于像浏览器那种实施搜索


其中有一些 UI相关代码 可以看主要的 循环操作

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Text.RegularExpressions;
using DG.Tweening;
/// <summary>
/// Select 查询模块
/// </summary>
public class SelectWindow : UIWindowTween
{
    private Button _selectButton;
    private Button _closeButton;
    private InputField _selectInputField;
    [SerializeField]
    private GameObject pre;
    [SerializeField]
    private Scrollbar rect;
    public ScrollRect _selectScroll;
    //储存了所有的查找到的 物体
    public List<GameObject> mainObj = new List<GameObject>();
    public List<Vector3> mainPos = new List<Vector3>();
    private List<GameObject> seleButtonList = new List<GameObject>();
    private System.Action OnspawnSelectOBJ;

    /// <summary>
    /// 列表循环
    /// </summary>
    /// <param name="type"></param>
    /// <param name="mode"></param>
    //只会出现10item
    public int maxNum = 10;
    private int number = 0;
    private int allNumber;
    //content
    [SerializeField]
    private Transform content;
    //group
    public GridLayoutGroup gird;

    //protected List<object> preList = new List<object>();
    用于储存总数
    //protected int preListLength;
    //第一个物体
    public Transform fristChild;
    //三个物体的高
    private float heightTop;
    //两个物体的高
    private float heightBottom;

    private int count;

    private bool load3DUI = false;
    public GameObject pre3DUI;

    private LookTarget jiantou;
    private Target3DUI target_3DJianToul;

    private GameObject targetTemp = null;
    //显示面板
    [SerializeField]
    public GameObject bg;
    protected override void SetWindowUnit(WindowType type = WindowType.Alone, WindowMode mode = WindowMode.Normal)
    {
        base.SetWindowUnit(type, mode);
    }
    /// <summary>
    /// 初始化监听
    /// </summary>
    public override void Initialize()
    {
        base.Initialize();
        _selectButton = transform.Find("BackGround/SelectButton").GetComponent<Button>();
        _closeButton = transform.Find("BackGround/SelectCloseButton").GetComponent<Button>();
        _selectInputField = transform.Find("BackGround/SelectInputField").GetComponent<InputField>();
        _selectButton.onClick.AddListener(SelectButtonOnClick);
        _closeButton.onClick.AddListener(OnClose);
        _selectInputField.onValueChanged.AddListener(OnSelectEvent);
        //_selectInputField.onEndEdit.AddListener(OnSelectEvent);
        gird.cellSize = new Vector2(250, 25);
        OnspawnSelectOBJ += showData;
    }

    public void OnSelectEvent(string str)
    {
        //_selectInputField.text = str;
        SelectButtonOnClick();

    }
    /// <summary>
    /// 查找信息
    /// </summary>
    private void SelectButtonOnClick()
    {
        CleanList();

        if (_selectInputField.text != string.Empty)
        {
            string text = _selectInputField.text;

            Regex RegChara = new Regex("^[A-Za-z0-9]+$");
            Match _character = RegChara.Match(text);
            text = text.ToUpper();
            for (int i = 0; i < ModelManager.Instance.Values.Count; i++)
            {
                //只包含字母和数字
                if (_character.Success)
                {
                    if (ModelManager.Instance.Values[i].Point != null)
                    {
                        if (ModelManager.Instance.Values[i].Point.Contains(text))
                        {
                            mainObj.Add(ModelManager.Instance.Values[i].MainObject);

                        }
                    }
                }
                else
                {
                    if (ModelManager.Instance.Values[i].Desc != null)
                    {
                        if (ModelManager.Instance.Values[i].Desc.Contains(text))
                        {
                            mainObj.Add(ModelManager.Instance.Values[i].MainObject);
                        }
                    }
                }

            }
            if (mainObj.Count == 0)
            {
                InitScrollBar(false);
                bg.SetActive(false);
                return;
            }
            else
            {
                InitScrollBar(true);
                bg.SetActive(true);
            }
            if (mainObj.Count < 10)
            {
                allNumber = 10;
            }
            else
            {
                allNumber = mainObj.Count;
            }
            //生成item 事件
            if (OnspawnSelectOBJ != null)
            {
                OnspawnSelectOBJ();
            }
            AddHightLight();

        }
        else
        {
            InitScrollBar(false);
            bg.SetActive(false);
        }
    }


    //让Content 高度为总数高度
    private void afterGetDate()
    {
        //preListLength = preList.Count;
        if (allNumber < 10)
        {
            content.GetComponent<RectTransform>().sizeDelta = new Vector2(0, 10 * gird.cellSize.y);
        }
        else
        {
            content.GetComponent<RectTransform>().sizeDelta = new Vector2(0, mainObj.Count * gird.cellSize.y);
        }

    }
    //生成item
    public void showData()
    {
        afterGetDate();

        InitPre();


    }
    public void InitPre()
    {
        //生成 只生成一次
        if (content.childCount < maxNum)
        {
            for (int i = 0; i < maxNum; i++)
            {
                creatPre(i);
                content.GetChild(i).gameObject.SetActive(false);
            }
            //监听
            InitSelectButton();

            fristChild = content.GetChild(0);
            heightTop = (fristChild.transform.position.y - content.GetChild(1).position.y) * 3 + fristChild.transform.position.y;

            heightBottom = (fristChild.transform.position.y - content.GetChild(1).position.y) * 2 + fristChild.transform.position.y;
        }
        //如果 总量 大于 显示总数
        if (mainObj.Count > maxNum)
        {
            for (int i = 0; i < maxNum; i++)
            {
                content.GetChild(i).gameObject.SetActive(true);
                updatePre(content.GetChild(i), i);
            }
            //显示总量赋予count
            count = maxNum;
            number = maxNum;
        }
        else
        {
            //如果 总量 小于 显示总数 则有多少生成多少
            int length = mainObj.Count;


            for (int i = 0; i < length; i++)
            {
                content.GetChild(i).gameObject.SetActive(true);
                updatePre(content.GetChild(i), i);
            }
            //显示总量赋予count
            count = length;
            number = length;

        }
        //获取content中第一个子物体
        fristChild = content.GetChild(0);


    }
    protected void creatPre(int num)
    {
        GameObject obj = Instantiate(pre, transform.position, Quaternion.identity);
        obj.transform.SetParent(content);
        obj.transform.localScale = Vector3.one;
        updatePre(obj.transform, num);
        obj.GetComponent<RectTransform>().sizeDelta = gird.cellSize;
        setgridLocalPosition(obj.transform, num);
        obj.SetActive(true);
        obj.name = num.ToString();
        mainPos.Add(obj.transform.GetComponent<RectTransform>().anchoredPosition3D);
        seleButtonList.Add(obj);
    }
    //更新数据(每一个子物体)
    protected void updatePre(Transform pre, int num)
    {
        if (num >= mainObj.Count)
        {
            pre.GetChild(0).GetComponent<Text>().text = string.Empty;

        }
        else
        {
            pre.GetChild(0).GetComponent<Text>().text = mainObj[num].gameObject.name;

        }
    }

    private void setgridLocalPosition(Transform obj, int num)
    {
        obj.localPosition = new Vector3(130, -(num + 0.5f) * gird.cellSize.y, 0);
    }

    void Update()
    {
        updateDate();
        if (jiantou != null)
        {
            jiantou.FollowNPC();
        }
        if (target_3DJianToul != null)
        {
            target_3DJianToul.FollowTarget();
        }
    }

    private void updateDate()
    {
        if (fristChild == null)
        {
            return;
        }
        //Debug.Log("fristChild " + fristChild.name);
        //Debug.Log("fristChild  " + content.GetChild(0).position.y);
        //Debug.Log("thirdChild  " + content.GetChild(1).position.y);
        //Debug.Log("heightTop  " + heightTop);
        //如果物体向上
        if (count < allNumber)
        {
            //Debug.Log("count < preListLength" + "count  " + count + "  preListLength  " + preListLength);
            //第一个物体大于三个物体的高度
            //Debug.Log("fristChild.localPosition.y  " + fristChild.localPosition.y + "heightTop " + heightTop);
            if (fristChild.position.y > heightTop)
            {
                Debug.Log("dadadadadadad");
                //第一个物体到队列中最后一个位置
                fristChild.SetAsLastSibling();
                //更新数据
                updatePre(fristChild, count);
                //设置 物体当前位置
                setgridLocalPosition(fristChild, count);
                //获取更新后队列中的第一个
                fristChild = content.GetChild(0);
                //count 递增
                count++;
            }
        }
        //如果cout 大于显示总数
        if (count > maxNum)
        {
            Debug.Log("count > maxNum" + "count" + count + "preListLength" + maxNum);
            //当往回拉的时候 count == 20
            //当在往回拉的时候 第一物体如果小于 3倍的item 高度
            if (fristChild.position.y < heightTop)
            {
                //获取最后一个物体
                Transform lastChild = content.GetChild(content.childCount - 1);
                updatePre(lastChild, count - maxNum - 1);
                setgridLocalPosition(lastChild, count - maxNum - 1);

                lastChild.SetAsFirstSibling();
                fristChild = content.GetChild(0);
                count--;
            }
        }
    }

    private void CleanList()
    {
        if (content.childCount != 0)
        {
            InitScrollBar();
            //清楚添加的所有物体
            mainObj.Clear();
            //preList.Clear();
            count = 0;
            DestoryObj();

        }
    }
    private void InitInputFieldText()
    {
        _selectInputField.text = string.Empty;
    }
    private void InitScrollBar()
    {
        if (rect != null)
            rect.value = 1;
    }
    private void DestoryObj()
    {
        for (int i = 0; i < maxNum; i++)
        {
            for (int j = 0; j < maxNum; j++)
            {
                if (string.Compare(content.GetChild(j).gameObject.name, i.ToString()) == 0)
                {
                    content.GetChild(j).gameObject.transform.GetComponent<RectTransform>().anchoredPosition3D = mainPos[i];
                    content.GetChild(j).gameObject.transform.SetAsLastSibling();
                    content.GetChild(j).gameObject.SetActive(false);
                }
            }
        }
        fristChild = content.GetChild(0).transform; ;
        Debug.Log(content.GetChild(0).transform.position);
    }
    /// <summary>
    /// 查找列表监听
    /// </summary>
    public void InitSelectButton()
    {
        for (int i = 0; i < seleButtonList.Count; i++)
        {
            UIEventListener.Get(seleButtonList[i]).onClick = SelectButtonEvent;
        }
    }
    /// <summary>
    /// 监听事件
    /// </summary>
    /// <param name="go"></param>
    public void SelectButtonEvent(GameObject go)
    {
        for (int i = 0; i < mainObj.Count; i++)
        {
            if (string.Compare(mainObj[i].name, go.transform.GetChild(0).GetComponent<Text>().text) == 0)
            {
                TowardsTarget(mainObj[i].gameObject);
            }
        }
        Debug.Log("BinGo");
    }
    /// <summary>
    /// 点击后高亮
    /// </summary>
    /// <param name="target"></param>
    public void TowardsTarget(GameObject target)
    {
        InitTargetTemp(false);
        Load3DUI();
        jiantou.GetTarget(target);
        target_3DJianToul.GetTarget(target);
        targetTemp = target;
        target.GetComponent<MouseTriggerAction>().Switchhighlighted(true);
        OnClose();
    }
    /// <summary>
    /// 生成3DUI
    /// </summary>
    private void Load3DUI()
    {
        if (!load3DUI)
        {
            load3DUI = true;
            GameObject ui = Instantiate(pre3DUI, Vector3.zero, Quaternion.identity);
            ui.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
            jiantou = GameObject.Find("jiantou").GetComponent<LookTarget>();
            target_3DJianToul = GameObject.Find("targetJiantou").GetComponent<Target3DUI>();
        }
        else
        {
            jiantou.gameObject.SetActive(true);
            target_3DJianToul.gameObject.SetActive(true);
        }
    }
    /// <summary>
    /// 添加高亮
    /// </summary>
    public void AddHightLight()
    {
        for (int i = 0; i < mainObj.Count; i++)
        {
            if (mainObj[i].GetComponent<MouseTriggerAction>() == null)
            {
                mainObj[i].AddComponent<MouseTriggerAction>();
            }
        }
    }
    public void InitTargetTemp(bool isok)
    {
        if (targetTemp != null)
        {
            targetTemp.GetComponent<MouseTriggerAction>().Switchhighlighted(isok);
        }
    }
    /// <summary>
    /// 初始化ScrollBar
    /// </summary>
    /// <param name="isok"></param>
    public void InitScrollBar(bool isok)
    {
        rect.interactable = isok;
        rect.GetComponent<Image>().enabled = isok;
        rect.gameObject.transform.GetChild(0).gameObject.SetActive(isok);
    }
    public override void OnOpen(InterResults result, params object[] args)
    {
        base.OnOpen(result, args);
        if (jiantou != null)
        {
            jiantou.gameObject.SetActive(false);
        }
        if (target_3DJianToul != null)
        {
            target_3DJianToul.gameObject.SetActive(false);
        }
        InitTargetTemp(false);

    }

    public override void OnClose()
    {
        base.OnClose();
        CleanList();
        InitInputFieldText();
        InitScrollBar(false);
        bg.SetActive(false);
    }
}

QQ群 : 593906968 有什么不懂的可以加群咨询互相学习 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity_阿黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值