2020-10-23 01:50:33 +11:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
using UnityEngine.Events;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
using UnityEngine.UI;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
using UnityExplorer.Core.Config;
|
|
|
|
|
using UnityExplorer.Core.Runtime;
|
2021-04-18 21:38:09 +10:00
|
|
|
|
using UnityExplorer.UI.Models;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
using UnityExplorer.UI.Utility;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
using UnityExplorer.UI.Widgets;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-11-03 20:59:13 +11:00
|
|
|
|
namespace UnityExplorer.UI
|
2020-10-23 01:50:33 +11:00
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
public static class UIFactory
|
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
internal static Vector2 _largeElementSize = new Vector2(160f, 30f);
|
|
|
|
|
internal static Vector2 _smallElementSize = new Vector2(160f, 20f);
|
|
|
|
|
internal static Color _defaultTextColor = Color.white;
|
|
|
|
|
internal static Font _defaultFont;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
public static void Init()
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
_defaultFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
public static GameObject CreateUIObject(string name, GameObject parent = null, Vector2 size = default)
|
|
|
|
|
{
|
|
|
|
|
if (!parent)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
ExplorerCore.LogWarning("Cannot create UI object as the parent is null or destroyed! (" + name + ")");
|
|
|
|
|
return null;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var obj = new GameObject(name)
|
|
|
|
|
{
|
|
|
|
|
layer = 5,
|
|
|
|
|
hideFlags = HideFlags.HideAndDontSave
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
obj.transform.SetParent(parent.transform, false);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
RectTransform rect = obj.AddComponent<RectTransform>();
|
|
|
|
|
rect.sizeDelta = size == default
|
|
|
|
|
? _smallElementSize
|
|
|
|
|
: size;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
return obj;
|
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
internal static void SetDefaultTextValues(Text text)
|
2020-11-08 21:04:41 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
text.color = _defaultTextColor;
|
|
|
|
|
text.font = _defaultFont;
|
|
|
|
|
text.fontSize = 14;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
internal static void SetDefaultSelectableColors(Selectable selectable)
|
2020-11-08 21:04:41 +11:00
|
|
|
|
{
|
2021-04-10 18:25:13 +10:00
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(selectable, new Color(0.2f, 0.2f, 0.2f),
|
2021-03-31 22:58:17 +11:00
|
|
|
|
new Color(0.3f, 0.3f, 0.3f), new Color(0.15f, 0.15f, 0.15f));
|
2021-03-30 19:50:04 +11:00
|
|
|
|
|
|
|
|
|
// Deselect all Buttons after they are clicked.
|
|
|
|
|
if (selectable is Button button)
|
|
|
|
|
button.onClick.AddListener(() => { button.OnDeselect(null); });
|
2020-11-08 21:04:41 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get and/or Add a LayoutElement component to the GameObject, and set any of the values on it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static LayoutElement SetLayoutElement(GameObject gameObject, int? minWidth = null, int? minHeight = null,
|
2021-04-15 20:18:03 +10:00
|
|
|
|
int? flexibleWidth = null, int? flexibleHeight = null, int? preferredWidth = null, int? preferredHeight = null,
|
2021-03-30 19:50:04 +11:00
|
|
|
|
bool? ignoreLayout = null)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var layout = gameObject.GetComponent<LayoutElement>();
|
|
|
|
|
if (!layout)
|
|
|
|
|
layout = gameObject.AddComponent<LayoutElement>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (minWidth != null)
|
|
|
|
|
layout.minWidth = (int)minWidth;
|
2020-11-11 00:16:01 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (minHeight != null)
|
|
|
|
|
layout.minHeight = (int)minHeight;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (flexibleWidth != null)
|
|
|
|
|
layout.flexibleWidth = (int)flexibleWidth;
|
|
|
|
|
|
|
|
|
|
if (flexibleHeight != null)
|
|
|
|
|
layout.flexibleHeight = (int)flexibleHeight;
|
|
|
|
|
|
|
|
|
|
if (preferredWidth != null)
|
|
|
|
|
layout.preferredWidth = (int)preferredWidth;
|
|
|
|
|
|
|
|
|
|
if (preferredHeight != null)
|
|
|
|
|
layout.preferredHeight = (int)preferredHeight;
|
|
|
|
|
|
|
|
|
|
if (ignoreLayout != null)
|
|
|
|
|
layout.ignoreLayout = (bool)ignoreLayout;
|
|
|
|
|
|
|
|
|
|
return layout;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get and/or Add a HorizontalOrVerticalLayoutGroup (must pick one) to the GameObject, and set the values on it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static T SetLayoutGroup<T>(GameObject gameObject, bool? forceWidth = null, bool? forceHeight = null,
|
2021-04-15 20:18:03 +10:00
|
|
|
|
bool? childControlWidth = null, bool? childControlHeight = null, int? spacing = null, int? padTop = null,
|
|
|
|
|
int? padBottom = null, int? padLeft = null, int? padRight = null, TextAnchor? childAlignment = null)
|
2021-03-31 22:58:17 +11:00
|
|
|
|
where T : HorizontalOrVerticalLayoutGroup
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var group = gameObject.GetComponent<T>();
|
|
|
|
|
if (!group)
|
|
|
|
|
group = gameObject.AddComponent<T>();
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
return SetLayoutGroup(group, forceWidth, forceHeight, childControlWidth, childControlHeight, spacing, padTop,
|
2021-03-31 22:58:17 +11:00
|
|
|
|
padBottom, padLeft, padRight, childAlignment);
|
2021-03-30 19:50:04 +11:00
|
|
|
|
}
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set the values on a HorizontalOrVerticalLayoutGroup.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static T SetLayoutGroup<T>(T group, bool? forceWidth = null, bool? forceHeight = null,
|
|
|
|
|
bool? childControlWidth = null, bool? childControlHeight = null, int? spacing = null, int? padTop = null,
|
2021-04-15 20:18:03 +10:00
|
|
|
|
int? padBottom = null, int? padLeft = null, int? padRight = null, TextAnchor? childAlignment = null)
|
2021-03-31 22:58:17 +11:00
|
|
|
|
where T : HorizontalOrVerticalLayoutGroup
|
2021-03-30 19:50:04 +11:00
|
|
|
|
{
|
|
|
|
|
if (forceWidth != null)
|
|
|
|
|
group.childForceExpandWidth = (bool)forceWidth;
|
|
|
|
|
if (forceHeight != null)
|
|
|
|
|
group.childForceExpandHeight = (bool)forceHeight;
|
|
|
|
|
if (childControlWidth != null)
|
|
|
|
|
group.SetChildControlWidth((bool)childControlWidth);
|
|
|
|
|
if (childControlHeight != null)
|
|
|
|
|
group.SetChildControlHeight((bool)childControlHeight);
|
|
|
|
|
if (spacing != null)
|
|
|
|
|
group.spacing = (int)spacing;
|
|
|
|
|
if (padTop != null)
|
|
|
|
|
group.padding.top = (int)padTop;
|
|
|
|
|
if (padBottom != null)
|
|
|
|
|
group.padding.bottom = (int)padBottom;
|
|
|
|
|
if (padLeft != null)
|
|
|
|
|
group.padding.left = (int)padLeft;
|
|
|
|
|
if (padRight != null)
|
|
|
|
|
group.padding.right = (int)padRight;
|
|
|
|
|
if (childAlignment != null)
|
|
|
|
|
group.childAlignment = (TextAnchor)childAlignment;
|
|
|
|
|
|
|
|
|
|
return group;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a Panel on the UI Canvas.
|
|
|
|
|
/// </summary>
|
2021-04-16 17:49:05 +10:00
|
|
|
|
public static GameObject CreatePanel(string name, out GameObject contentHolder, Color? bgColor = null)
|
2021-03-30 19:50:04 +11:00
|
|
|
|
{
|
|
|
|
|
var panelObj = CreateUIObject(name, UIManager.CanvasRoot);
|
|
|
|
|
var rect = panelObj.GetComponent<RectTransform>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
rect.anchorMin = Vector2.zero;
|
|
|
|
|
rect.anchorMax = Vector2.one;
|
|
|
|
|
rect.anchoredPosition = Vector2.zero;
|
|
|
|
|
rect.sizeDelta = Vector2.zero;
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var maskImg = panelObj.AddComponent<Image>();
|
|
|
|
|
maskImg.color = Color.white;
|
|
|
|
|
panelObj.AddComponent<Mask>().showMaskGraphic = false;
|
|
|
|
|
|
|
|
|
|
SetLayoutGroup<VerticalLayoutGroup>(panelObj, true, true, true, true);
|
|
|
|
|
|
|
|
|
|
contentHolder = CreateUIObject("Content", panelObj);
|
|
|
|
|
|
|
|
|
|
Image bgImage = contentHolder.AddComponent<Image>();
|
|
|
|
|
bgImage.type = Image.Type.Filled;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
if (bgColor == null)
|
|
|
|
|
bgImage.color = new Color(0.1f, 0.1f, 0.1f);
|
|
|
|
|
else
|
|
|
|
|
bgImage.color = (Color)bgColor;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
|
|
|
|
|
SetLayoutGroup<VerticalLayoutGroup>(contentHolder, true, true, true, true, 3, 3, 3, 3, 3);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
return panelObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a VerticalLayoutGroup object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static GameObject CreateVerticalGroup(GameObject parent, string name, bool forceWidth, bool forceHeight,
|
|
|
|
|
bool childControlWidth, bool childControlHeight, int spacing = 0, Vector4 padding = default, Color bgColor = default,
|
|
|
|
|
TextAnchor? childAlignment = null)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GameObject groupObj = CreateUIObject(name, parent);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SetLayoutGroup<VerticalLayoutGroup>(groupObj, forceWidth, forceHeight, childControlWidth, childControlHeight,
|
|
|
|
|
spacing, (int)padding.x, (int)padding.y, (int)padding.z, (int)padding.w, childAlignment);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image image = groupObj.AddComponent<Image>();
|
2021-03-30 19:50:04 +11:00
|
|
|
|
image.color = bgColor == default
|
|
|
|
|
? new Color(0.17f, 0.17f, 0.17f)
|
|
|
|
|
: bgColor;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
return groupObj;
|
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a HorizontalLayoutGroup object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static GameObject CreateHorizontalGroup(GameObject parent, string name, bool forceExpandWidth, bool forceExpandHeight,
|
|
|
|
|
bool childControlWidth, bool childControlHeight, int spacing = 0, Vector4 padding = default, Color bgColor = default,
|
|
|
|
|
TextAnchor? childAlignment = null)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GameObject groupObj = CreateUIObject(name, parent);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SetLayoutGroup<HorizontalLayoutGroup>(groupObj, forceExpandWidth, forceExpandHeight, childControlWidth, childControlHeight,
|
|
|
|
|
spacing, (int)padding.x, (int)padding.y, (int)padding.z, (int)padding.w, childAlignment);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image image = groupObj.AddComponent<Image>();
|
2021-03-30 19:50:04 +11:00
|
|
|
|
image.color = bgColor == default
|
|
|
|
|
? new Color(0.17f, 0.17f, 0.17f)
|
|
|
|
|
: bgColor;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
return groupObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a GridLayoutGroup object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static GameObject CreateGridGroup(GameObject parent, string name, Vector2 cellSize, Vector2 spacing, Color bgColor = default)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var groupObj = CreateUIObject(name, parent);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GridLayoutGroup gridGroup = groupObj.AddComponent<GridLayoutGroup>();
|
|
|
|
|
gridGroup.childAlignment = TextAnchor.UpperLeft;
|
|
|
|
|
gridGroup.cellSize = cellSize;
|
|
|
|
|
gridGroup.spacing = spacing;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image image = groupObj.AddComponent<Image>();
|
2021-03-30 19:50:04 +11:00
|
|
|
|
|
|
|
|
|
image.color = bgColor == default
|
|
|
|
|
? new Color(0.17f, 0.17f, 0.17f)
|
|
|
|
|
: bgColor;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
return groupObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a Label object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Text CreateLabel(GameObject parent, string name, string text, TextAnchor alignment,
|
|
|
|
|
Color color = default, bool supportRichText = true, int fontSize = 14)
|
|
|
|
|
{
|
|
|
|
|
var obj = CreateUIObject(name, parent);
|
|
|
|
|
var textComp = obj.AddComponent<Text>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SetDefaultTextValues(textComp);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
textComp.text = text;
|
|
|
|
|
textComp.color = color == default ? _defaultTextColor : color;
|
|
|
|
|
textComp.supportRichText = supportRichText;
|
|
|
|
|
textComp.alignment = alignment;
|
|
|
|
|
textComp.fontSize = fontSize;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
return textComp;
|
|
|
|
|
}
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
public static Button CreateButton(GameObject parent, string name, string text, Action onClick = null, Color? normalColor = null)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var colors = new ColorBlock();
|
2021-03-31 22:58:17 +11:00
|
|
|
|
normalColor = normalColor ?? new Color(0.25f, 0.25f, 0.25f);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-04-10 18:25:13 +10:00
|
|
|
|
var btn = CreateButton(parent, name, text, onClick, colors);
|
|
|
|
|
|
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(btn, normalColor, new Color(0.4f, 0.4f, 0.4f), new Color(0.15f, 0.15f, 0.15f));
|
|
|
|
|
|
|
|
|
|
return btn;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
public static Button CreateButton(GameObject parent, string name, string text, Action onClick, ColorBlock colors)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GameObject buttonObj = CreateUIObject(name, parent, _smallElementSize);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var textObj = CreateUIObject("Text", buttonObj);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image image = buttonObj.AddComponent<Image>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
image.type = Image.Type.Sliced;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
image.color = new Color(1, 1, 1, 1);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var button = buttonObj.AddComponent<Button>();
|
|
|
|
|
SetDefaultSelectableColors(button);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
colors.colorMultiplier = 1;
|
2021-04-10 18:25:13 +10:00
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(button, colors);
|
2020-11-06 20:42:16 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
Text textComp = textObj.AddComponent<Text>();
|
|
|
|
|
textComp.text = text;
|
|
|
|
|
SetDefaultTextValues(textComp);
|
|
|
|
|
textComp.alignment = TextAnchor.MiddleCenter;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
RectTransform rect = textObj.GetComponent<RectTransform>();
|
|
|
|
|
rect.anchorMin = Vector2.zero;
|
|
|
|
|
rect.anchorMax = Vector2.one;
|
|
|
|
|
rect.sizeDelta = Vector2.zero;
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (onClick != null)
|
|
|
|
|
button.onClick.AddListener(onClick);
|
|
|
|
|
|
|
|
|
|
return button;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a Slider control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static GameObject CreateSlider(GameObject parent, string name, out Slider slider)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GameObject sliderObj = CreateUIObject(name, parent, _smallElementSize);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
GameObject bgObj = CreateUIObject("Background", sliderObj);
|
|
|
|
|
GameObject fillAreaObj = CreateUIObject("Fill Area", sliderObj);
|
|
|
|
|
GameObject fillObj = CreateUIObject("Fill", fillAreaObj);
|
|
|
|
|
GameObject handleSlideAreaObj = CreateUIObject("Handle Slide Area", sliderObj);
|
|
|
|
|
GameObject handleObj = CreateUIObject("Handle", handleSlideAreaObj);
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image bgImage = bgObj.AddComponent<Image>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
bgImage.type = Image.Type.Sliced;
|
|
|
|
|
bgImage.color = new Color(0.15f, 0.15f, 0.15f, 1.0f);
|
|
|
|
|
|
|
|
|
|
RectTransform bgRect = bgObj.GetComponent<RectTransform>();
|
|
|
|
|
bgRect.anchorMin = new Vector2(0f, 0.25f);
|
|
|
|
|
bgRect.anchorMax = new Vector2(1f, 0.75f);
|
|
|
|
|
bgRect.sizeDelta = new Vector2(0f, 0f);
|
|
|
|
|
|
|
|
|
|
RectTransform fillAreaRect = fillAreaObj.GetComponent<RectTransform>();
|
|
|
|
|
fillAreaRect.anchorMin = new Vector2(0f, 0.25f);
|
|
|
|
|
fillAreaRect.anchorMax = new Vector2(1f, 0.75f);
|
|
|
|
|
fillAreaRect.anchoredPosition = new Vector2(-5f, 0f);
|
|
|
|
|
fillAreaRect.sizeDelta = new Vector2(-20f, 0f);
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image fillImage = fillObj.AddComponent<Image>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
fillImage.type = Image.Type.Sliced;
|
|
|
|
|
fillImage.color = new Color(0.3f, 0.3f, 0.3f, 1.0f);
|
|
|
|
|
|
|
|
|
|
fillObj.GetComponent<RectTransform>().sizeDelta = new Vector2(10f, 0f);
|
|
|
|
|
|
|
|
|
|
RectTransform handleSlideRect = handleSlideAreaObj.GetComponent<RectTransform>();
|
|
|
|
|
handleSlideRect.sizeDelta = new Vector2(-20f, 0f);
|
|
|
|
|
handleSlideRect.anchorMin = new Vector2(0f, 0f);
|
|
|
|
|
handleSlideRect.anchorMax = new Vector2(1f, 1f);
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image handleImage = handleObj.AddComponent<Image>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
handleImage.color = new Color(0.5f, 0.5f, 0.5f, 1.0f);
|
|
|
|
|
|
|
|
|
|
handleObj.GetComponent<RectTransform>().sizeDelta = new Vector2(20f, 0f);
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
slider = sliderObj.AddComponent<Slider>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
slider.fillRect = fillObj.GetComponent<RectTransform>();
|
|
|
|
|
slider.handleRect = handleObj.GetComponent<RectTransform>();
|
|
|
|
|
slider.targetGraphic = handleImage;
|
|
|
|
|
slider.direction = Slider.Direction.LeftToRight;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
|
2021-04-10 18:25:13 +10:00
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(slider, new Color(0.4f, 0.4f, 0.4f),
|
2021-03-31 22:58:17 +11:00
|
|
|
|
new Color(0.55f, 0.55f, 0.55f), new Color(0.3f, 0.3f, 0.3f));
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
return sliderObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a Scrollbar control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static GameObject CreateScrollbar(GameObject parent, string name, out Scrollbar scrollbar)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GameObject scrollObj = CreateUIObject(name, parent, _smallElementSize);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
GameObject slideAreaObj = CreateUIObject("Sliding Area", scrollObj);
|
|
|
|
|
GameObject handleObj = CreateUIObject("Handle", slideAreaObj);
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image scrollImage = scrollObj.AddComponent<Image>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
scrollImage.type = Image.Type.Sliced;
|
|
|
|
|
scrollImage.color = new Color(0.1f, 0.1f, 0.1f);
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image handleImage = handleObj.AddComponent<Image>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
handleImage.type = Image.Type.Sliced;
|
|
|
|
|
handleImage.color = new Color(0.4f, 0.4f, 0.4f);
|
|
|
|
|
|
|
|
|
|
RectTransform slideAreaRect = slideAreaObj.GetComponent<RectTransform>();
|
|
|
|
|
slideAreaRect.sizeDelta = new Vector2(-20f, -20f);
|
|
|
|
|
slideAreaRect.anchorMin = Vector2.zero;
|
|
|
|
|
slideAreaRect.anchorMax = Vector2.one;
|
|
|
|
|
|
|
|
|
|
RectTransform handleRect = handleObj.GetComponent<RectTransform>();
|
|
|
|
|
handleRect.sizeDelta = new Vector2(20f, 20f);
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
scrollbar = scrollObj.AddComponent<Scrollbar>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
scrollbar.handleRect = handleRect;
|
|
|
|
|
scrollbar.targetGraphic = handleImage;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
|
|
|
|
|
SetDefaultSelectableColors(scrollbar);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
return scrollObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a Toggle control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static GameObject CreateToggle(GameObject parent, string name, out Toggle toggle, out Text text, Color bgColor = default)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GameObject toggleObj = CreateUIObject(name, parent, _smallElementSize);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
GameObject bgObj = CreateUIObject("Background", toggleObj);
|
|
|
|
|
GameObject checkObj = CreateUIObject("Checkmark", bgObj);
|
|
|
|
|
GameObject labelObj = CreateUIObject("Label", toggleObj);
|
|
|
|
|
|
|
|
|
|
toggle = toggleObj.AddComponent<Toggle>();
|
2020-11-16 00:50:06 +11:00
|
|
|
|
toggle.isOn = true;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Toggle toggleComp = toggle;
|
2020-10-27 00:54:08 +11:00
|
|
|
|
|
2020-11-13 23:14:57 +11:00
|
|
|
|
toggle.onValueChanged.AddListener(Deselect);
|
|
|
|
|
void Deselect(bool _)
|
2020-10-27 00:54:08 +11:00
|
|
|
|
{
|
2020-11-13 23:14:57 +11:00
|
|
|
|
toggleComp.OnDeselect(null);
|
2020-10-27 00:54:08 +11:00
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image bgImage = bgObj.AddComponent<Image>();
|
2021-03-30 19:50:04 +11:00
|
|
|
|
bgImage.color = bgColor == default
|
|
|
|
|
? new Color(0.2f, 0.2f, 0.2f, 1.0f)
|
2020-11-14 19:51:16 +11:00
|
|
|
|
: bgColor;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image checkImage = checkObj.AddComponent<Image>();
|
2020-11-14 19:51:16 +11:00
|
|
|
|
checkImage.color = new Color(0.3f, 0.5f, 0.3f, 1.0f);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
text = labelObj.AddComponent<Text>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
text.text = "Toggle";
|
|
|
|
|
SetDefaultTextValues(text);
|
|
|
|
|
|
|
|
|
|
toggle.graphic = checkImage;
|
|
|
|
|
toggle.targetGraphic = bgImage;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SetDefaultSelectableColors(toggle);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
RectTransform bgRect = bgObj.GetComponent<RectTransform>();
|
|
|
|
|
bgRect.anchorMin = new Vector2(0f, 1f);
|
|
|
|
|
bgRect.anchorMax = new Vector2(0f, 1f);
|
2020-11-14 19:51:16 +11:00
|
|
|
|
bgRect.anchoredPosition = new Vector2(13f, -13f);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
bgRect.sizeDelta = new Vector2(20f, 20f);
|
|
|
|
|
|
|
|
|
|
RectTransform checkRect = checkObj.GetComponent<RectTransform>();
|
|
|
|
|
checkRect.anchorMin = new Vector2(0.5f, 0.5f);
|
|
|
|
|
checkRect.anchorMax = new Vector2(0.5f, 0.5f);
|
|
|
|
|
checkRect.anchoredPosition = Vector2.zero;
|
|
|
|
|
checkRect.sizeDelta = new Vector2(14f, 14f);
|
|
|
|
|
|
|
|
|
|
RectTransform labelRect = labelObj.GetComponent<RectTransform>();
|
|
|
|
|
labelRect.anchorMin = new Vector2(0f, 0f);
|
|
|
|
|
labelRect.anchorMax = new Vector2(1f, 1f);
|
2020-11-14 19:51:16 +11:00
|
|
|
|
labelRect.offsetMin = new Vector2(28f, 2f);
|
|
|
|
|
labelRect.offsetMax = new Vector2(-5f, -5f);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
return toggleObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a Scrollable Input Field control (custom InputFieldScroller).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static GameObject CreateSrollInputField(GameObject parent, string name, string placeHolderText,
|
|
|
|
|
out InputFieldScroller inputScroll, int fontSize = 14, Color color = default)
|
2020-11-11 20:16:43 +11:00
|
|
|
|
{
|
|
|
|
|
if (color == default)
|
|
|
|
|
color = new Color(0.15f, 0.15f, 0.15f);
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
var mainObj = CreateScrollView(parent, "InputFieldScrollView", out GameObject scrollContent, out SliderScrollbar scroller, color);
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
var inputObj = CreateInputField(scrollContent, name, placeHolderText ?? "...", out InputField inputField, fontSize, 0);
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
|
|
|
|
inputField.lineType = InputField.LineType.MultiLineNewline;
|
|
|
|
|
inputField.targetGraphic.color = color;
|
|
|
|
|
|
|
|
|
|
inputScroll = new InputFieldScroller(scroller, inputField);
|
|
|
|
|
|
|
|
|
|
return mainObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a standard InputField control.
|
|
|
|
|
/// </summary>
|
2021-04-15 20:18:03 +10:00
|
|
|
|
public static GameObject CreateInputField(GameObject parent, string name, string placeHolderText, out InputField inputField,
|
|
|
|
|
int fontSize = 14, int alignment = 3, int wrap = 0)
|
2020-10-24 20:18:42 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GameObject mainObj = CreateUIObject(name, parent);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image mainImage = mainObj.AddComponent<Image>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
mainImage.type = Image.Type.Sliced;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
mainImage.color = new Color(0.15f, 0.15f, 0.15f);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
inputField = mainObj.AddComponent<InputField>();
|
|
|
|
|
Navigation nav = inputField.navigation;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
nav.mode = Navigation.Mode.None;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
inputField.navigation = nav;
|
|
|
|
|
inputField.lineType = InputField.LineType.SingleLine;
|
|
|
|
|
inputField.interactable = true;
|
|
|
|
|
inputField.transition = Selectable.Transition.ColorTint;
|
|
|
|
|
inputField.targetGraphic = mainImage;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(inputField, new Color(1, 1, 1, 1),
|
2021-03-31 22:58:17 +11:00
|
|
|
|
new Color(0.95f, 0.95f, 0.95f, 1.0f), new Color(0.78f, 0.78f, 0.78f, 1.0f));
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SetLayoutGroup<VerticalLayoutGroup>(mainObj, true, true, true, true);
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
GameObject textArea = CreateUIObject("TextArea", mainObj);
|
2020-10-24 20:18:42 +11:00
|
|
|
|
textArea.AddComponent<RectMask2D>();
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
RectTransform textAreaRect = textArea.GetComponent<RectTransform>();
|
2020-11-10 20:18:14 +11:00
|
|
|
|
textAreaRect.anchorMin = Vector2.zero;
|
|
|
|
|
textAreaRect.anchorMax = Vector2.one;
|
|
|
|
|
textAreaRect.offsetMin = Vector2.zero;
|
|
|
|
|
textAreaRect.offsetMax = Vector2.zero;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
2020-11-10 20:18:14 +11:00
|
|
|
|
// mainInput.textViewport = textArea.GetComponent<RectTransform>();
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
GameObject placeHolderObj = CreateUIObject("Placeholder", textArea);
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Text placeholderText = placeHolderObj.AddComponent<Text>();
|
2020-11-10 20:18:14 +11:00
|
|
|
|
SetDefaultTextValues(placeholderText);
|
2021-03-30 19:50:04 +11:00
|
|
|
|
placeholderText.text = placeHolderText ?? "...";
|
2020-10-28 06:39:26 +11:00
|
|
|
|
placeholderText.color = new Color(0.5f, 0.5f, 0.5f, 1.0f);
|
2020-11-10 20:18:14 +11:00
|
|
|
|
placeholderText.horizontalOverflow = (HorizontalWrapMode)wrap;
|
|
|
|
|
placeholderText.alignment = (TextAnchor)alignment;
|
|
|
|
|
placeholderText.fontSize = fontSize;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
RectTransform placeHolderRect = placeHolderObj.GetComponent<RectTransform>();
|
2020-10-24 20:18:42 +11:00
|
|
|
|
placeHolderRect.anchorMin = Vector2.zero;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
placeHolderRect.anchorMax = Vector2.one;
|
|
|
|
|
placeHolderRect.offsetMin = Vector2.zero;
|
2020-10-24 20:18:42 +11:00
|
|
|
|
placeHolderRect.offsetMax = Vector2.zero;
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SetLayoutElement(placeHolderObj, minWidth: 500, flexibleWidth: 5000);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
inputField.placeholder = placeholderText;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
GameObject inputTextObj = CreateUIObject("Text", textArea);
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Text inputText = inputTextObj.AddComponent<Text>();
|
2020-11-10 20:18:14 +11:00
|
|
|
|
SetDefaultTextValues(inputText);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
inputText.text = "";
|
|
|
|
|
inputText.color = new Color(1f, 1f, 1f, 1f);
|
2020-11-10 20:18:14 +11:00
|
|
|
|
inputText.horizontalOverflow = (HorizontalWrapMode)wrap;
|
|
|
|
|
inputText.alignment = (TextAnchor)alignment;
|
|
|
|
|
inputText.fontSize = fontSize;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
RectTransform inputTextRect = inputTextObj.GetComponent<RectTransform>();
|
|
|
|
|
inputTextRect.anchorMin = Vector2.zero;
|
|
|
|
|
inputTextRect.anchorMax = Vector2.one;
|
|
|
|
|
inputTextRect.offsetMin = Vector2.zero;
|
|
|
|
|
inputTextRect.offsetMax = Vector2.zero;
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SetLayoutElement(inputTextObj, minWidth: 500, flexibleWidth: 5000);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
inputField.textComponent = inputText;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
return mainObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a DropDown control.
|
|
|
|
|
/// </summary>
|
2021-04-15 20:18:03 +10:00
|
|
|
|
public static GameObject CreateDropdown(GameObject parent, out Dropdown dropdown, string defaultItemText, int itemFontSize,
|
2021-03-30 19:50:04 +11:00
|
|
|
|
Action<int> onValueChanged, string[] defaultOptions = null)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GameObject dropdownObj = CreateUIObject("Dropdown", parent, _largeElementSize);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
GameObject labelObj = CreateUIObject("Label", dropdownObj);
|
2020-11-02 20:48:53 +11:00
|
|
|
|
GameObject arrowObj = CreateUIObject("Arrow", dropdownObj);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
GameObject templateObj = CreateUIObject("Template", dropdownObj);
|
|
|
|
|
GameObject viewportObj = CreateUIObject("Viewport", templateObj);
|
|
|
|
|
GameObject contentObj = CreateUIObject("Content", viewportObj);
|
|
|
|
|
GameObject itemObj = CreateUIObject("Item", contentObj);
|
|
|
|
|
GameObject itemBgObj = CreateUIObject("Item Background", itemObj);
|
|
|
|
|
GameObject itemCheckObj = CreateUIObject("Item Checkmark", itemObj);
|
|
|
|
|
GameObject itemLabelObj = CreateUIObject("Item Label", itemObj);
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
GameObject scrollbarObj = CreateScrollbar(templateObj, "DropdownScroll", out Scrollbar scrollbar);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
scrollbar.SetDirection(Scrollbar.Direction.BottomToTop, true);
|
2021-04-16 21:07:32 +10:00
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(scrollbar, new Color(0.45f, 0.45f, 0.45f), new Color(0.6f, 0.6f, 0.6f), new Color(0.4f, 0.4f, 0.4f));
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
RectTransform scrollRectTransform = scrollbarObj.GetComponent<RectTransform>();
|
|
|
|
|
scrollRectTransform.anchorMin = Vector2.right;
|
|
|
|
|
scrollRectTransform.anchorMax = Vector2.one;
|
|
|
|
|
scrollRectTransform.pivot = Vector2.one;
|
|
|
|
|
scrollRectTransform.sizeDelta = new Vector2(scrollRectTransform.sizeDelta.x, 0f);
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Text itemLabelText = itemLabelObj.AddComponent<Text>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
SetDefaultTextValues(itemLabelText);
|
|
|
|
|
itemLabelText.alignment = TextAnchor.MiddleLeft;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
itemLabelText.text = defaultItemText;
|
|
|
|
|
itemLabelText.fontSize = itemFontSize;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
var arrowText = arrowObj.AddComponent<Text>();
|
2020-11-02 20:48:53 +11:00
|
|
|
|
SetDefaultTextValues(arrowText);
|
|
|
|
|
arrowText.text = "▼";
|
|
|
|
|
var arrowRect = arrowObj.GetComponent<RectTransform>();
|
|
|
|
|
arrowRect.anchorMin = new Vector2(1f, 0.5f);
|
|
|
|
|
arrowRect.anchorMax = new Vector2(1f, 0.5f);
|
|
|
|
|
arrowRect.sizeDelta = new Vector2(20f, 20f);
|
|
|
|
|
arrowRect.anchoredPosition = new Vector2(-15f, 0f);
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image itemBgImage = itemBgObj.AddComponent<Image>();
|
2021-04-16 21:07:32 +10:00
|
|
|
|
itemBgImage.color = new Color(0.25f, 0.35f, 0.25f, 1.0f);
|
2020-10-27 00:54:08 +11:00
|
|
|
|
|
|
|
|
|
Toggle itemToggle = itemObj.AddComponent<Toggle>();
|
|
|
|
|
itemToggle.targetGraphic = itemBgImage;
|
|
|
|
|
itemToggle.isOn = true;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(itemToggle,
|
2021-04-16 21:07:32 +10:00
|
|
|
|
new Color(0.35f, 0.35f, 0.35f, 1.0f), new Color(0.25f, 0.55f, 0.25f, 1.0f));
|
2020-10-27 00:54:08 +11:00
|
|
|
|
|
2020-11-13 23:14:57 +11:00
|
|
|
|
itemToggle.onValueChanged.AddListener((bool val) => { itemToggle.OnDeselect(null); });
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image templateImage = templateObj.AddComponent<Image>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
templateImage.type = Image.Type.Sliced;
|
2021-04-16 21:07:32 +10:00
|
|
|
|
templateImage.color = Color.black;
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-11-15 21:11:43 +11:00
|
|
|
|
var scrollRect = templateObj.AddComponent<ScrollRect>();
|
2020-11-12 20:31:08 +11:00
|
|
|
|
scrollRect.scrollSensitivity = 35;
|
2020-10-23 19:55:02 +11:00
|
|
|
|
scrollRect.content = contentObj.GetComponent<RectTransform>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
scrollRect.viewport = viewportObj.GetComponent<RectTransform>();
|
|
|
|
|
scrollRect.horizontal = false;
|
|
|
|
|
scrollRect.movementType = ScrollRect.MovementType.Clamped;
|
|
|
|
|
scrollRect.verticalScrollbar = scrollbar;
|
|
|
|
|
scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
|
|
|
|
|
scrollRect.verticalScrollbarSpacing = -3f;
|
|
|
|
|
|
|
|
|
|
viewportObj.AddComponent<Mask>().showMaskGraphic = false;
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image viewportImage = viewportObj.AddComponent<Image>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
viewportImage.type = Image.Type.Sliced;
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Text labelText = labelObj.AddComponent<Text>();
|
2020-10-28 06:39:26 +11:00
|
|
|
|
SetDefaultTextValues(labelText);
|
|
|
|
|
labelText.alignment = TextAnchor.MiddleLeft;
|
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image dropdownImage = dropdownObj.AddComponent<Image>();
|
2021-04-16 21:07:32 +10:00
|
|
|
|
dropdownImage.color = new Color(0.07f, 0.07f, 0.07f, 1);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
dropdownImage.type = Image.Type.Sliced;
|
|
|
|
|
|
|
|
|
|
dropdown = dropdownObj.AddComponent<Dropdown>();
|
|
|
|
|
dropdown.targetGraphic = dropdownImage;
|
|
|
|
|
dropdown.template = templateObj.GetComponent<RectTransform>();
|
|
|
|
|
dropdown.captionText = labelText;
|
|
|
|
|
dropdown.itemText = itemLabelText;
|
2021-03-30 19:50:04 +11:00
|
|
|
|
//itemLabelText.text = "DEFAULT";
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-10-23 19:55:02 +11:00
|
|
|
|
dropdown.RefreshShownValue();
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
RectTransform labelRect = labelObj.GetComponent<RectTransform>();
|
|
|
|
|
labelRect.anchorMin = Vector2.zero;
|
|
|
|
|
labelRect.anchorMax = Vector2.one;
|
2020-11-08 21:04:41 +11:00
|
|
|
|
labelRect.offsetMin = new Vector2(10f, 2f);
|
|
|
|
|
labelRect.offsetMax = new Vector2(-28f, -2f);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
RectTransform templateRect = templateObj.GetComponent<RectTransform>();
|
|
|
|
|
templateRect.anchorMin = new Vector2(0f, 0f);
|
|
|
|
|
templateRect.anchorMax = new Vector2(1f, 0f);
|
|
|
|
|
templateRect.pivot = new Vector2(0.5f, 1f);
|
|
|
|
|
templateRect.anchoredPosition = new Vector2(0f, 2f);
|
|
|
|
|
templateRect.sizeDelta = new Vector2(0f, 150f);
|
|
|
|
|
|
|
|
|
|
RectTransform viewportRect = viewportObj.GetComponent<RectTransform>();
|
|
|
|
|
viewportRect.anchorMin = new Vector2(0f, 0f);
|
|
|
|
|
viewportRect.anchorMax = new Vector2(1f, 1f);
|
|
|
|
|
viewportRect.sizeDelta = new Vector2(-18f, 0f);
|
|
|
|
|
viewportRect.pivot = new Vector2(0f, 1f);
|
|
|
|
|
|
|
|
|
|
RectTransform contentRect = contentObj.GetComponent<RectTransform>();
|
|
|
|
|
contentRect.anchorMin = new Vector2(0f, 1f);
|
|
|
|
|
contentRect.anchorMax = new Vector2(1f, 1f);
|
|
|
|
|
contentRect.pivot = new Vector2(0.5f, 1f);
|
|
|
|
|
contentRect.anchoredPosition = new Vector2(0f, 0f);
|
|
|
|
|
contentRect.sizeDelta = new Vector2(0f, 28f);
|
|
|
|
|
|
|
|
|
|
RectTransform itemRect = itemObj.GetComponent<RectTransform>();
|
|
|
|
|
itemRect.anchorMin = new Vector2(0f, 0.5f);
|
|
|
|
|
itemRect.anchorMax = new Vector2(1f, 0.5f);
|
2020-10-28 20:52:40 +11:00
|
|
|
|
itemRect.sizeDelta = new Vector2(0f, 25f);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
RectTransform itemBgRect = itemBgObj.GetComponent<RectTransform>();
|
|
|
|
|
itemBgRect.anchorMin = Vector2.zero;
|
|
|
|
|
itemBgRect.anchorMax = Vector2.one;
|
|
|
|
|
itemBgRect.sizeDelta = Vector2.zero;
|
|
|
|
|
|
|
|
|
|
RectTransform itemLabelRect = itemLabelObj.GetComponent<RectTransform>();
|
|
|
|
|
itemLabelRect.anchorMin = Vector2.zero;
|
|
|
|
|
itemLabelRect.anchorMax = Vector2.one;
|
|
|
|
|
itemLabelRect.offsetMin = new Vector2(20f, 1f);
|
|
|
|
|
itemLabelRect.offsetMax = new Vector2(-10f, -2f);
|
|
|
|
|
templateObj.SetActive(false);
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
if (onValueChanged != null)
|
|
|
|
|
dropdown.onValueChanged.AddListener(onValueChanged);
|
|
|
|
|
|
|
|
|
|
if (defaultOptions != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var option in defaultOptions)
|
|
|
|
|
dropdown.options.Add(new Dropdown.OptionData(option));
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
return dropdownObj;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 21:38:09 +10:00
|
|
|
|
public static DynamicScrollPool CreateDynamicScrollPool(GameObject parent, string name, out GameObject uiRoot,
|
|
|
|
|
out GameObject content, Color? bgColor, bool autoResizeSliderHandle = true)
|
|
|
|
|
{
|
|
|
|
|
var pool = CreateScrollPool<DynamicScrollPool>(parent, name, out uiRoot, out content, bgColor, autoResizeSliderHandle);
|
|
|
|
|
|
|
|
|
|
SetLayoutGroup<VerticalLayoutGroup>(content, true, false, true, true, 2, 2, 2, 2, 2,
|
|
|
|
|
TextAnchor.UpperCenter);
|
|
|
|
|
|
|
|
|
|
var rect = content.GetComponent<RectTransform>();
|
|
|
|
|
rect.pivot = new Vector2(0.5f, 1f);
|
|
|
|
|
|
|
|
|
|
return pool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T CreateScrollPool<T>(GameObject parent, string name, out GameObject uiRoot,
|
|
|
|
|
out GameObject content, Color? bgColor = null, bool autoResizeSliderHandle = true) where T : IScrollPool
|
2021-04-15 20:18:03 +10:00
|
|
|
|
{
|
|
|
|
|
var mainObj = CreateUIObject(name, parent, new Vector2(1, 1));
|
|
|
|
|
mainObj.AddComponent<Image>().color = bgColor ?? new Color(0.12f, 0.12f, 0.12f);
|
|
|
|
|
SetLayoutGroup<HorizontalLayoutGroup>(mainObj, false, true, true, true);
|
|
|
|
|
|
|
|
|
|
GameObject viewportObj = CreateUIObject("Viewport", mainObj);
|
|
|
|
|
SetLayoutElement(viewportObj, flexibleWidth: 9999);
|
|
|
|
|
var viewportRect = viewportObj.GetComponent<RectTransform>();
|
|
|
|
|
viewportRect.anchorMin = Vector2.zero;
|
|
|
|
|
viewportRect.anchorMax = Vector2.one;
|
|
|
|
|
viewportRect.pivot = new Vector2(0.0f, 1.0f);
|
|
|
|
|
viewportRect.sizeDelta = new Vector2(0f, 0.0f);
|
|
|
|
|
viewportRect.offsetMax = new Vector2(-10.0f, 0.0f);
|
|
|
|
|
viewportObj.AddComponent<Image>().color = Color.white;
|
|
|
|
|
viewportObj.AddComponent<Mask>().showMaskGraphic = false;
|
|
|
|
|
|
|
|
|
|
content = CreateUIObject("Content", viewportObj);
|
|
|
|
|
var contentRect = content.GetComponent<RectTransform>();
|
|
|
|
|
contentRect.anchorMin = Vector2.zero;
|
|
|
|
|
contentRect.anchorMax = Vector2.one;
|
|
|
|
|
contentRect.pivot = new Vector2(0.0f, 1.0f);
|
|
|
|
|
contentRect.sizeDelta = new Vector2(0f, 0f);
|
|
|
|
|
contentRect.offsetMax = new Vector2(0f, 0f);
|
|
|
|
|
|
2021-04-16 02:48:49 +10:00
|
|
|
|
var scrollRect = mainObj.AddComponent<ScrollRect>();
|
|
|
|
|
scrollRect.movementType = ScrollRect.MovementType.Clamped;
|
|
|
|
|
scrollRect.inertia = false;
|
|
|
|
|
//scrollRect.decelerationRate = 0.135f;
|
|
|
|
|
scrollRect.scrollSensitivity = 15;
|
|
|
|
|
scrollRect.horizontal = false;
|
|
|
|
|
scrollRect.vertical = true;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 02:48:49 +10:00
|
|
|
|
scrollRect.viewport = viewportRect;
|
|
|
|
|
scrollRect.content = contentRect;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 17:49:05 +10:00
|
|
|
|
var sliderContainer = CreateVerticalGroup(mainObj, "SliderContainer",
|
|
|
|
|
false, false, true, true, 0, default, new Color(0.05f, 0.05f, 0.05f));
|
|
|
|
|
SetLayoutElement(sliderContainer, minWidth: 25, flexibleWidth:0, flexibleHeight: 9999);
|
|
|
|
|
sliderContainer.AddComponent<Mask>();
|
|
|
|
|
|
|
|
|
|
var sliderObj = SliderScrollbar.CreateSliderScrollbar(sliderContainer, out Slider slider);
|
2021-04-15 20:18:03 +10:00
|
|
|
|
slider.direction = Slider.Direction.TopToBottom;
|
2021-04-16 17:49:05 +10:00
|
|
|
|
SetLayoutElement(sliderObj, minWidth: 25, flexibleWidth: 0, flexibleHeight: 9999);
|
|
|
|
|
|
|
|
|
|
if (autoResizeSliderHandle)
|
|
|
|
|
{
|
|
|
|
|
slider.handleRect.offsetMin = new Vector2(slider.handleRect.offsetMin.x, 0);
|
|
|
|
|
slider.handleRect.offsetMax = new Vector2(slider.handleRect.offsetMax.x, 0);
|
|
|
|
|
slider.handleRect.pivot = new Vector2(0.5f, 0.5f);
|
|
|
|
|
|
|
|
|
|
var container = slider.m_HandleContainerRect;
|
|
|
|
|
container.anchorMin = Vector3.zero;
|
|
|
|
|
container.anchorMax = Vector3.one;
|
|
|
|
|
container.pivot = new Vector3(0.5f, 0.5f);
|
|
|
|
|
}
|
2021-04-15 20:18:03 +10:00
|
|
|
|
|
2021-04-16 02:48:49 +10:00
|
|
|
|
uiRoot = mainObj;
|
|
|
|
|
|
2021-04-18 21:38:09 +10:00
|
|
|
|
var scrollPool = (T)Activator.CreateInstance(typeof(T), new object[] { scrollRect });
|
|
|
|
|
scrollPool.AutoResizeHandleRect = autoResizeSliderHandle;
|
2021-04-16 02:48:49 +10:00
|
|
|
|
|
2021-04-18 21:38:09 +10:00
|
|
|
|
return scrollPool;
|
2021-04-15 20:18:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a ScrollView element.
|
|
|
|
|
/// </summary>
|
2021-04-15 20:18:03 +10:00
|
|
|
|
public static GameObject CreateScrollView(GameObject parent, string name, out GameObject content, out SliderScrollbar scroller,
|
2021-03-30 19:50:04 +11:00
|
|
|
|
Color color = default)
|
2020-10-28 06:39:26 +11:00
|
|
|
|
{
|
2020-11-06 20:42:16 +11:00
|
|
|
|
GameObject mainObj = CreateUIObject("DynamicScrollView", parent);
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SetLayoutElement(mainObj, minWidth: 100, minHeight: 30, flexibleWidth: 5000, flexibleHeight: 5000);
|
2020-11-06 20:42:16 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
Image mainImage = mainObj.AddComponent<Image>();
|
2020-11-06 20:42:16 +11:00
|
|
|
|
mainImage.type = Image.Type.Filled;
|
|
|
|
|
mainImage.color = (color == default) ? new Color(0.3f, 0.3f, 0.3f, 1f) : color;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-06 20:42:16 +11:00
|
|
|
|
GameObject viewportObj = CreateUIObject("Viewport", mainObj);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-06 20:42:16 +11:00
|
|
|
|
var viewportRect = viewportObj.GetComponent<RectTransform>();
|
|
|
|
|
viewportRect.anchorMin = Vector2.zero;
|
|
|
|
|
viewportRect.anchorMax = Vector2.one;
|
|
|
|
|
viewportRect.pivot = new Vector2(0.0f, 1.0f);
|
|
|
|
|
viewportRect.sizeDelta = new Vector2(-15.0f, 0.0f);
|
2021-03-30 19:50:04 +11:00
|
|
|
|
viewportRect.offsetMax = new Vector2(-20.0f, 0.0f);
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-12 16:15:41 +11:00
|
|
|
|
viewportObj.AddComponent<Image>().color = Color.white;
|
2020-11-06 20:42:16 +11:00
|
|
|
|
viewportObj.AddComponent<Mask>().showMaskGraphic = false;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
|
|
|
|
content = CreateUIObject("Content", viewportObj);
|
2020-11-06 20:42:16 +11:00
|
|
|
|
var contentRect = content.GetComponent<RectTransform>();
|
|
|
|
|
contentRect.anchorMin = new Vector2(0.0f, 1.0f);
|
|
|
|
|
contentRect.anchorMax = new Vector2(1.0f, 1.0f);
|
|
|
|
|
contentRect.pivot = new Vector2(0.0f, 1.0f);
|
|
|
|
|
contentRect.sizeDelta = new Vector2(5f, 0f);
|
|
|
|
|
contentRect.offsetMax = new Vector2(0f, 0f);
|
|
|
|
|
var contentFitter = content.AddComponent<ContentSizeFitter>();
|
|
|
|
|
contentFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
|
|
|
|
|
contentFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
|
|
|
|
|
2021-03-30 19:50:04 +11:00
|
|
|
|
SetLayoutGroup<VerticalLayoutGroup>(content, true, true, true, true, 5, 5, 5, 5, 5);
|
2020-11-06 20:42:16 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
CreateSliderScrollbar(mainObj, out scroller, out Scrollbar hiddenScrollbar);
|
|
|
|
|
|
|
|
|
|
// Back to the main scrollview ScrollRect, setting it up now that we have all references.
|
|
|
|
|
|
|
|
|
|
var scrollRect = mainObj.AddComponent<ScrollRect>();
|
|
|
|
|
scrollRect.horizontal = false;
|
|
|
|
|
scrollRect.vertical = true;
|
|
|
|
|
scrollRect.verticalScrollbar = hiddenScrollbar;
|
|
|
|
|
scrollRect.movementType = ScrollRect.MovementType.Clamped;
|
|
|
|
|
scrollRect.scrollSensitivity = 35;
|
|
|
|
|
scrollRect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
|
|
|
|
|
scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.Permanent;
|
|
|
|
|
|
|
|
|
|
scrollRect.viewport = viewportRect;
|
|
|
|
|
scrollRect.content = contentRect;
|
|
|
|
|
|
|
|
|
|
return mainObj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GameObject CreateSliderScrollbar(GameObject mainObj, out SliderScrollbar scroller, out Scrollbar hiddenScrollbar)
|
|
|
|
|
{
|
2020-11-11 20:16:43 +11:00
|
|
|
|
GameObject scrollBarObj = CreateUIObject("DynamicScrollbar", mainObj);
|
2020-11-06 20:42:16 +11:00
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
var scrollbarLayout = scrollBarObj.AddComponent<VerticalLayoutGroup>();
|
2020-11-06 20:42:16 +11:00
|
|
|
|
scrollbarLayout.childForceExpandHeight = true;
|
2021-03-26 19:49:53 +11:00
|
|
|
|
scrollbarLayout.SetChildControlHeight(true);
|
2020-11-06 20:42:16 +11:00
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
RectTransform scrollBarRect = scrollBarObj.GetComponent<RectTransform>();
|
2020-11-06 20:42:16 +11:00
|
|
|
|
scrollBarRect.anchorMin = new Vector2(1.0f, 0.0f);
|
|
|
|
|
scrollBarRect.anchorMax = new Vector2(1.0f, 1.0f);
|
|
|
|
|
scrollBarRect.sizeDelta = new Vector2(15.0f, 0.0f);
|
|
|
|
|
scrollBarRect.offsetMin = new Vector2(-15.0f, 0.0f);
|
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
GameObject hiddenBar = CreateScrollbar(scrollBarObj, "HiddenScrollviewScroller", out hiddenScrollbar);
|
|
|
|
|
hiddenScrollbar.SetDirection(Scrollbar.Direction.BottomToTop, true);
|
2020-11-06 20:42:16 +11:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < hiddenBar.transform.childCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var child = hiddenBar.transform.GetChild(i);
|
|
|
|
|
child.gameObject.SetActive(false);
|
|
|
|
|
}
|
2020-10-28 06:39:26 +11:00
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
SliderScrollbar.CreateSliderScrollbar(scrollBarObj, out Slider scrollSlider);
|
2020-10-23 01:50:33 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
scroller = new SliderScrollbar(hiddenScrollbar, scrollSlider);
|
2020-10-24 20:18:42 +11:00
|
|
|
|
|
2021-04-15 20:18:03 +10:00
|
|
|
|
return scrollBarObj;
|
2020-11-06 20:42:16 +11:00
|
|
|
|
}
|
2020-10-28 06:39:26 +11:00
|
|
|
|
}
|
2020-10-23 01:50:33 +11:00
|
|
|
|
}
|