unity之TreeView


使用方式见  http://blog.csdn.net/qq_17813937/article/details/78164638


using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[RequireComponent(typeof(VerticalLayoutGroup))]
[RequireComponent(typeof(ContentSizeFitter))]
public class TreeView : MonoBehaviour {

    public bool isFold = true;
    public GameObject prefab;
    public Sprite on;
    public Sprite off;
    public int indent;
    public int rowSpacing;
    public int columnSpacing;

    private float left = 0;
    private TreeItem root;
    private RectTransform rectTransform;

    public class CreateEvent : UnityEvent { }
    public CreateEvent onCreateItem = new CreateEvent();

    public RectTransform RectTransform
    {
        get
        {
            return rectTransform ?? (rectTransform = transform as RectTransform);
        }
    }

    private void Awake()
    {
        GetComponent().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
        GetComponent().spacing = rowSpacing;
    }

    public void Show()
    {
        gameObject.SetActive(true);
    }

    public void Hide()
    {
        gameObject.SetActive(false);
    }

    public TreeItem CreateItem(int id)
    {
        TreeItem item;
        if (on != null)
        {
            item = new TreeItem(prefab, true);
            item.SetIcon(on);
            if (left == 0)
                left = item.IconSize.x + columnSpacing;

            item.Icon.rectTransform.anchoredPosition = new Vector2(-left, 0);
            item.ItemRectTr.anchoredPosition = new Vector2(left,0);

            if (isFold)
            {
                EventListener.Get(item.Icon.gameObject).onLeftClick += () => {
                    if (item.IsOpenList) item.SetIcon(off);
                    else item.SetIcon(on);
                    item.IsOpenList = !item.IsOpenList;
                };
            }
            item.HideIcon();
        }
        else
        {
            item = new TreeItem(prefab,false);
            if (isFold)
            {
                EventListener.Get(item.ItemRectTr.gameObject).onLeftDoubleClick += () => {
                    item.IsOpenList = !item.IsOpenList;
                };
            }
        }
        item.ItemRectTr.name = id.ToString();
        onCreateItem.Invoke(item.ItemRectTr);
        item.Parent = RectTransform;
        return item;
    }

    public void RootAddItem(TreeItem item)
    {
        if(root==null)
        {
            root = new TreeItem(null,false);
            root.ItemParent = RectTransform;
            root.index = -1;
        }
        AddItem(root, item);
    }

    public void AddItem(TreeItem parent, TreeItem child)
    {
        parent.AddItem(child);
        if(parent.Count > 0)
        {
            child.SetSiblingIndex(parent[parent.Count-1].GetSiblingIndex());
        }
        else
        {
            child.SetSiblingIndex(parent.GetSiblingIndex());
        }
        
    }

    public void UpdateIndent()
    {
        SetIndex(root);
    }

    private void SetIndex(TreeItem item)
    {
        int length = item.Count;
        for (int i = 0; i < length; i++)
        {
            var tmp = item[i];
            tmp.index = item.index+1;
            tmp.SetAnchoredPosition(new Vector2(left + tmp.index * indent, tmp.GetAnchoredPosition().y));
            SetIndex(item[i]);
        }
    }

    public void FoldAll()
    {
        int length = root.Count;
        if (on != null)
        {
            for (int i = 0; i < length; i++)
            {
                root[i].Hide(off);
            }
        }
        else
        {
            for (int i = 0; i < length; i++)
            {
                root[i].Hide();
            }
        }
    }
}



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

public class TreeItem{
    
    public int index;
    public  List list = new List();

    private bool isShowIcon = false;
    private bool isOpenList = true;
    private static Vector2 left = new Vector2(0, 0.5f);
    private RectTransform itemParent;
    private RectTransform item;
    private Image icon;

    public TreeItem this[int index]
    {
        get
        {
            return list[index];
        }
        set
        {
            list[index] = value;
        }
    }

    public Image Icon
    {
        get { return icon; }
    }

    public Vector2 IconSize
    {
        get { return icon.rectTransform.sizeDelta; }
    }

    public bool IsOpenList
    {
        get { return isOpenList; }
        set {
            if (value) Show();
            else Hide();
            isOpenList = value;
        }
    }

    public RectTransform ItemParent
    {
        get { return itemParent; }
        set { itemParent = value; }
    }

    public RectTransform ItemRectTr
    {
        get
        {
            return item;
        }
        set
        {
            item = value;
        }
    }

    public Transform Parent
    {
        set
        {
            itemParent.SetParent(value);
        }
    }
    
    public int Count
    {
        get
        {
            return list.Count;
        }
    }

    public TreeItem(GameObject prefab, bool showIcon)
    {
        if (prefab != null)
        {
            itemParent = new GameObject().AddComponent();
            itemParent.pivot = left;

            item = GameObject.Instantiate(prefab).transform as RectTransform;
            itemParent.sizeDelta = item.sizeDelta;
            item.transform.SetParent(itemParent);
            item.anchorMin = left;
            item.anchorMax = left;
            item.pivot = left;

            if (showIcon)
            {
                icon = new GameObject().AddComponent();
                icon.rectTransform.anchorMin = left;
                icon.rectTransform.anchorMax = left;
                icon.rectTransform.pivot = left;
                icon.transform.SetParent(item);
            }
        }
    }
    
    public void SetSiblingIndex(int index)
    {
        itemParent.SetSiblingIndex(index);
    }

    public int GetSiblingIndex()
    {
        return itemParent.GetSiblingIndex();
    }

    public void SetAnchoredPosition(Vector2 pos)
    {
        item.anchoredPosition = pos;
    }

    public Vector2 GetAnchoredPosition()
    {
        return item.anchoredPosition;
    }

    public void AddItem(TreeItem item)
    {
        if (icon!=null && !isShowIcon)
        {
            ShowIcon();
        }
        list.Add(item);
    }
    
    public void Show()
    {
        SetListActive(true);
    }

    public void Hide()
    {
        SetListActive(false);
    }
    
    public void SetIcon(Sprite sp)
    {
        icon.sprite = sp;
        icon.SetNativeSize();
    }

    public void Hide(Sprite sp)
    {
        SetListActive(sp, false);
    }

    public void Show(Sprite sp)
    {
        SetListActive(sp,true);
    }

    public void ShowIcon()
    {
        isShowIcon = true;
        icon.gameObject.SetActive(true);
    }
    
    public void HideIcon()
    {
        isShowIcon = false;
        icon.gameObject.SetActive(false);
    }

    private void SetListActive(bool active)
    {
        int length = list.Count;
        for (int i = 0; i < length; i++)
        {
            list[i].isOpenList = active;
            list[i].SetListActive(active);
            list[i].itemParent.gameObject.SetActive(active);
        }
        isOpenList = active;
    }

    private void SetListActive(Sprite sp, bool active)
    {
        int length = list.Count;
        for (int i = 0; i < length; i++)
        {
            list[i].isOpenList = active;
            list[i].SetIcon(sp);
            list[i].itemParent.gameObject.SetActive(active);
            list[i].SetListActive(sp, active);
        }
        isOpenList = active;
        SetIcon(sp);
    }
}


你可能感兴趣的:(unity3d)