Unity 三级目录

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

public class MenuManage : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform menus;
    private Transform slice1;
    private Transform slice2;
    private Transform slice3;
    private GameObject Clone2;
    private GameObject Clone3;
    private float startCloneHeight;
    private List<GameObject> cloneTwoList = new List<GameObject>();

    private int menesTotals;
    private List<Transform> menusTF = new List<Transform>();
    public GameObject content;
    public float contendHight;
    void Start()
    {
        menesTotals = menus.childCount;
        foreach (Transform tf in menus)
        {
            //  EventTriggerListener.Get(tf.gameObject).onClick = onMens;
            menusTF.Add(tf);
            tf.GetComponent<Button>().onClick.AddListener(() =>
            {
                onMens(tf.gameObject);
                // print(123);
            });
        }
        initi(MenuOne.sliceOneMenu);
    }
    void initi(int sliceOneMenu)
    {
        for (int i = 0; i < menesTotals; i++)
        {
            if (i == sliceOneMenu)
            {
                menus.GetChild(i).GetComponent<Image>().sprite = menus.GetChild(i).GetComponent<ChangleSP>().stopSP;
            }
            else
            {
                menus.GetChild(i).GetComponent<Image>().sprite = menus.GetChild(i).GetComponent<ChangleSP>().startSP;
            }
        }
        slice1 = menus.GetChild(sliceOneMenu);
        slice2 = slice1.GetComponent<MenuTwoThreeDate>().meneTwo;

        slice3 = slice1.GetComponent<MenuTwoThreeDate>().meneThree;
        ResetIndex(slice2, menus.gameObject, sliceOneMenu + 1);

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="newTf">克隆出来所有的二级目录</param>
    /// <param name="parentOBJ">被克隆出来放在parentOBJ的下面</param>
    /// <param name="indexNum">按照顺序排列</param>
    private void ResetIndex(Transform newTf, GameObject parentOBJ, int indexNum)
    {
        for (int i = slice2.childCount - 1; i >= 0; i--)
        {
            GameObject newOBJ2 = Instantiate(newTf.GetChild(i).gameObject, menus);
            newOBJ2.transform.SetSiblingIndex(indexNum);

            if (!newOBJ2.GetComponent<Button>())
            {
                newOBJ2.AddComponent<Button>();
            }

            // EventTriggerListener.Get(newOBJ2).onClick = onMens2;
            newOBJ2.GetComponent<Button>().onClick.AddListener(() =>
           {
               onMens2(newOBJ2);
           });
            cloneTwoList.Add(newOBJ2);
        }
    }


    #region 点击一级目录
    void onMens(GameObject sender)
    {
        if (sender.GetComponent<Image>().sprite.name == sender.GetComponent<ChangleSP>().stopSP.name)
        {
            StartCoroutine(resetMenuOne(-1));
            sender.GetComponent<Image>().sprite = sender.GetComponent<ChangleSP>().startSP;
            return;
        }

        for (int i = 0; i < menesTotals; i++)
        {
            if (sender.name == menusTF[i].name)
            {
                StartCoroutine(resetMenuOne(i));
                menusTF[i].GetComponent<MenuTwoThreeDate>().arrow.SetActive(true);
            }
            else
            {
                menusTF[i].GetComponent<MenuTwoThreeDate>().arrow.SetActive(false);
            }
        }
    }

    IEnumerator resetMenuOne(int i)
    {

        while (cloneTwoList.Count > 0)
        {
            Destroy(cloneTwoList[0]);
            cloneTwoList.RemoveAt(0);
        }
        if (cloneThreeList.Count > 0)
        {
            Destroy(cloneThreeList[0]);
            cloneThreeList.RemoveAt(0);
        }
        yield return new WaitForSeconds(0.1f);
        if (i !=-1) initi(i);
        getMenusHight();
    }
    #endregion
    private int num;
    #region 点击二级目录
    void onMens2(GameObject OBJ2)
    {

        // print("二级名字" + OBJ2.name);

        if (OBJ2.GetComponent<Image>().sprite.name == OBJ2.GetComponent<ChangleSP>().stopSP.name)
        {
            Destroy(cloneThreeList[0]);
            cloneThreeList.RemoveAt(0);
            OBJ2.GetComponent<Image>().sprite = OBJ2.GetComponent<ChangleSP>().startSP;
            return;
        }

        for (int i = cloneTwoList.Count - 1; i >= 0; i--)
        {
            if (OBJ2.name == cloneTwoList[i].name)
            {
                OBJ2.GetComponent<Image>().sprite = OBJ2.GetComponent<ChangleSP>().stopSP;
                num = cloneTwoList.Count - i - 1;
            }
            else
            {
                cloneTwoList[i].GetComponent<Image>().sprite = OBJ2.GetComponent<ChangleSP>().startSP;
            }
        }
        if (cloneThreeList.Count > 0)
        {
            print("删除");
            Destroy(cloneThreeList[0]);
            cloneThreeList.RemoveAt(0);
        }
        StartCoroutine(CloneIE(OBJ2, num));

    }

    IEnumerator CloneIE(GameObject OBJ2, int i)
    {
        {
            yield return new WaitForSeconds(0.01f);

            for (int j = 0; j < menus.childCount; j++)
            {
                if (OBJ2.name == menus.GetChild(j).name)
                {
                    Clone3 = Instantiate(slice3.GetChild(i).gameObject, menus);
                    cloneThreeList.Add(Clone3);
                    if (Clone3.transform.childCount == 0) Clone3.SetActive(false);
                   // print(Clone3.transform.childCount);
                    Clone3.transform.SetSiblingIndex(j + 1);
                }
            }



            foreach (Transform tf in Clone3.transform)
            {
                EventTriggerListener.Get(tf.gameObject).onClick = onMens3;
            }
            getMenusHight();
        }
    }

    #endregion
    //点击二级的时候判断
    private List<GameObject> cloneThreeList = new List<GameObject>();


    #region//点击三级目录

    void onMens3(GameObject OBJ3)
    {

        for (int i = 0; i < Clone3.transform.childCount; i++)
        {
            if (OBJ3.name == Clone3.transform.GetChild(i).name)
            {
                OBJ3.GetComponent<Image>().sprite = OBJ3.GetComponent<ChangleSP>().stopSP;
                //  print("三级名字:" + OBJ3);
            }
            else
            {
                Clone3.transform.GetChild(i).GetComponent<Image>().sprite = OBJ3.GetComponent<ChangleSP>().startSP;
            }
        }
        getMenusHight();
    }
    #endregion

    //获取目录的高度
    void getMenusHight()
    {
        contendHight = 0;
        foreach (Transform child in menus)
        {
            RectTransform rectTransform = child.GetComponent<RectTransform>();
            if (rectTransform != null)
            {
                contendHight += rectTransform.rect.height;
            }
        }

        Vector2 date = content.GetComponent<RectTransform>().sizeDelta;
        date.y = contendHight + 50;
      //  print(date.y);
        content.GetComponent<RectTransform>().sizeDelta = date;
    }

}

 

posted on 2024-10-29 14:18  zqiang0803  阅读(4)  评论(0编辑  收藏  举报

导航