Exemplo n.º 1
0
    /// <summary>
    /// 新建,黏贴插入操作
    /// </summary>
    /// <param name="index">Index.</param>
    /// <param name="itemName">Item name.</param>
    /// <param name="parentFolder">Parent folder.</param>
    /// <param name="menuList">Menu list.</param>
    private TreeItem InsertItem(int index, string itemName, TreeFolder parentFolder, TreeContainer menuList)
    {
        //实例化文件夹
        GameObject go = GameObject.Instantiate(Resources.Load("Prefabs/TreeItem")) as GameObject;

        go.name = itemName;
        TreeItem item = go.GetComponent <TreeItem>();

        item.MyTreeContainer = menuList;
        item.BeFolder        = false;
        item.Name            = itemName;
        go.transform.SetParent(parentFolder.SubRootRectTransform as Transform);
        item._RectTransform           = go.GetComponent <RectTransform>();
        item._RectTransform.pivot     = new Vector2(0, 1);
        item._RectTransform.anchorMax = new Vector2(0, 1);
        item._RectTransform.anchorMin = new Vector2(0, 1);

        item.ItemNameText.text     = itemName;
        item.ItemNameText.fontSize = UIEnum.FontSize;
        var   textRt = item.ItemNameText.gameObject.GetComponent <RectTransform>();
        float w      = Tools.GetStringW(item.ItemNameText.text, UIEnum.FontSize);

        textRt.sizeDelta = new Vector2(w, UIEnum.ItmeH);

        //重新设置下Folder的边框宽度
        float w1 = UIEnum.ItemImageW + w;

        item._RectTransform.sizeDelta = new Vector2(w1, UIEnum.ItemH);

        item.ParentFolder = parentFolder;

        //这里这句还是要注意点,文件夹和item混合插入应该会出问题,不过目前没这种需求
        float h = (index + 1) * UIEnum.ItemH;

        item._RectTransform.anchoredPosition = new Vector2(UIEnum.FolderBtnW * 2 + 4, -h);
        item.CreateMenu(item);

        parentFolder.ItemList.Insert(index, item);
        parentFolder.RefreshItemNum();
        //新建文件夹后通知上级进行重新排序,比如rootFolder下有10个subFolder,在第二个subFolder下新建,那么后面所有subFolder都要重排
        //这里注意下ui坐标系是左下是0,0所以负值是向下移动
        parentFolder.ParentYOrder(-UIEnum.ItemH, index);

        return(item);
    }
Exemplo n.º 2
0
    /// <summary>
    /// 这个方法只有从folder右键创建item时调用
    /// 其他地方用InsertItem
    /// </summary>
    /// <param name="itemName">Item name.</param>
    /// <param name="parentFolder">Parent folder.</param>
    /// <param name="menuList">Menu list.</param>
    public static TreeItem Create(string itemName, TreeFolder parentFolder, TreeContainer treeContainer)
    {
        //添加文件夹时,如果父文件夹没有打开,先打开再添加
        if (!parentFolder.BeFolderOpen)
        {
            parentFolder.BeFolderOpen = true;
        }

        //实例化文件夹
        GameObject go = GameObject.Instantiate(Resources.Load("Prefabs/TreeItem")) as GameObject;

        go.name = itemName;
        TreeItem item = go.GetComponent <TreeItem>();

        item.MyTreeContainer = treeContainer;
        item.BeFolder        = false;
        item.Name            = itemName;
        go.transform.SetParent(parentFolder.SubRootRectTransform as Transform);
        item._RectTransform           = go.GetComponent <RectTransform>();
        item._RectTransform.pivot     = new Vector2(0, 1);
        item._RectTransform.anchorMax = new Vector2(0, 1);
        item._RectTransform.anchorMin = new Vector2(0, 1);

        item.ItemNameText.text     = itemName;
        item.ItemNameText.fontSize = UIEnum.FontSize;
        var   textRt = item.ItemNameText.gameObject.GetComponent <RectTransform>();
        float w      = Tools.GetStringW(item.ItemNameText.text, UIEnum.FontSize);

        textRt.sizeDelta = new Vector2(w, UIEnum.ItmeH);

        //重新设置下Folder的边框宽度
        float w1 = UIEnum.ItemImageW + w;

        item._RectTransform.sizeDelta = new Vector2(w1, UIEnum.ItemH);

        item.ParentFolder = parentFolder;

        //float h = (parentFolder.SubItemNums + 1) * UIEnum.ItemH;
        float h = parentFolder.CurFolderH;

        //float h = parentFolder.CurFolderH;
        item._RectTransform.anchoredPosition = new Vector2(UIEnum.FolderBtnW * 2 + 4, -h);
        item.CreateMenu(item);

        parentFolder.ItemList.Add(item);
        parentFolder.RefreshItemNum();


        //新建文件夹后通知上级进行重新排序,比如rootFolder下有10个subFolder,在第二个subFolder下新建,那么后面所有subFolder都要重排
        //这里注意下ui坐标系是左下是0,0所以负值是向下移动
        parentFolder.ParentYOrder(-UIEnum.ItemH, item.IndexInParentFolder);


        //刷新下MenuList的边框宽,用于刷新MenuList滚动区域,item距离Menulist左边距离+一个图标间隔+4(文字和图标间隔)+w1(文字长度)
        float _w = item.CurItemLeftOffset + UIEnum.ItemImageW + 4 + w1;

        treeContainer.SetTreeW(_w);
        treeContainer.RefreshTreeArea();

        return(item);
    }