Unity3d UGUI 动态加载按钮

    static int numPerRow = 5;//每行的按钮个数
    static int lineHeight = 60;
    static float panelWidth = 300;
    static float panelHeight = 300;

    public static void ShowSelectPanel(EModelType type)
    {
        List list = Resource.typeItem[Enum.GetName(typeof(EModelType), type)];
        int length = list.Count;
        for (int i = 0; i < length; i++)
        {
            int row = i / numPerRow;
            int col = i % numPerRow;
            float offsetX = panelWidth / numPerRow * col - panelWidth / 2 + panelWidth / numPerRow / 2;
            float offsetY = panelHeight / 2 - lineHeight * row - lineHeight;

            GameObject bt = Instantiate(Resources.Load("KT/SelectItemButton")) as GameObject;
            bt.transform.SetParent(_selectPanel);
            bt.GetComponent().localPosition = new Vector3(offsetX, offsetY, 0);
        }
    }

你可能感兴趣的:(Unity3D)