RICKShaozhiheng的专栏

一个游戏程序员

【Unity】模仿GUILayout.SelectionGird绘制一组互斥的按钮

        /// <summary>
        /// 绘制一组互斥的按钮
        /// 模仿GUILayout.SelectionGird绘制一组互斥的按钮,
        /// 做了一点修改, 传入的string数组改为KeyValuePair<int, string>的数组,
        /// 返回的不再是button在这个组合中的位置,而是我们给定的id(KeyValue中的int)
        /// </summary>
        /// <param name="ActiveID"></param>
        /// <param name="toggleDes"></param>
        /// <param name="colCount">Colum Count</param>
        /// <param name="style">GUIStyle</param>
        /// <param name="options">params GUILayoutOption[]</param>
        /// <returns></returns>
        public static int MutexToggles(int ActiveID, IList<KeyValuePair<int, string>> toggleDes, int colCount = 1, GUIStyle style = null, params GUILayoutOption[] options)
        {
            style = style == null ? new GUIStyle("button") : style;

            int rowCount = Mathf.CeilToInt(((float)toggleDes.Count) / colCount);
            GUILayout.BeginVertical();
            for (int row = 1; row <= rowCount; row++)
            {
                GUILayout.BeginHorizontal();
                for (int col = 1; col <= colCount; col++)
                {
                    int index = (row - 1) * colCount + col - 1;

                    if (index >= toggleDes.Count)
                        break;
                    bool flag = GUILayout.Toggle(toggleDes[index].Key == ActiveID, toggleDes[index].Value, style, options);
                    if (flag != (toggleDes[index].Key == ActiveID))
                        return toggleDes[index].Key;

                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndVertical();

            return ActiveID;
        }
    }
}
阅读更多
版权声明:转载请表明出处 https://blog.csdn.net/RICKShaozhiheng/article/details/52369450
个人分类: unity
上一篇[Unity Tips] [RuntimeInitializeOnLoadMethod]
下一篇【Unity】Unity编辑器搜索风格输入框
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭