diff --git a/Scripts/Editor/UIExtensionsMenuOptions.cs b/Scripts/Editor/UIExtensionsMenuOptions.cs index 96d2209..fe68e7d 100644 --- a/Scripts/Editor/UIExtensionsMenuOptions.cs +++ b/Scripts/Editor/UIExtensionsMenuOptions.cs @@ -14,19 +14,19 @@ namespace UnityEditor.UI #region Unity Builder section - Do not change unless UI Source (Editor\MenuOptions) changes #region Unity Builder properties - Do not change unless UI Source (Editor\MenuOptions) changes private const string kUILayerName = "UI"; - private const float kWidth = 160f; - private const float kThickHeight = 30f; - private const float kThinHeight = 20f; - private const string kStandardSpritePath = "UI/Skin/UISprite.psd"; + private const float kWidth = 160f; + private const float kThickHeight = 30f; + private const float kThinHeight = 20f; + private const string kStandardSpritePath = "UI/Skin/UISprite.psd"; private const string kBackgroundSpriteResourcePath = "UI/Skin/Background.psd"; - private const string kInputFieldBackgroundPath = "UI/Skin/InputFieldBackground.psd"; - private const string kKnobPath = "UI/Skin/Knob.psd"; - private const string kCheckmarkPath = "UI/Skin/Checkmark.psd"; + private const string kInputFieldBackgroundPath = "UI/Skin/InputFieldBackground.psd"; + private const string kKnobPath = "UI/Skin/Knob.psd"; + private const string kCheckmarkPath = "UI/Skin/Checkmark.psd"; - private static Vector2 s_ThickGUIElementSize = new Vector2(kWidth, kThickHeight); - private static Vector2 s_ThinGUIElementSize = new Vector2(kWidth, kThinHeight); - private static Vector2 s_ImageGUIElementSize = new Vector2(100f, 100f); - private static Color s_DefaultSelectableColor = new Color(1f, 1f, 1f, 1f); + private static Vector2 s_ThickGUIElementSize = new Vector2(kWidth, kThickHeight); + private static Vector2 s_ThinGUIElementSize = new Vector2(kWidth, kThinHeight); + private static Vector2 s_ImageGUIElementSize = new Vector2(100f, 100f); + private static Color s_DefaultSelectableColor = new Color(1f, 1f, 1f, 1f); private static Color s_TextColor = new Color(50f / 255f, 50f / 255f, 50f / 255f, 1f); #endregion #region Unity Builder methods - Do not change unless UI Source (Editor\MenuOptions) changes @@ -97,7 +97,7 @@ namespace UnityEditor.UI Selection.activeGameObject = child; return child; } - + static GameObject CreateUIObject(string name, GameObject parent) { GameObject go = new GameObject(name); @@ -207,6 +207,7 @@ namespace UnityEditor.UI #region UI Extensions "Create" Menu items + #region Scroll Snap controls [MenuItem("GameObject/UI/Extensions/Horizontal Scroll Snap", false)] static public void AddHorizontalScrollSnap(MenuCommand menuCommand) { @@ -224,7 +225,7 @@ namespace UnityEditor.UI rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; rectTransformScrollSnapRoot.sizeDelta = new Vector2(300f, 150f); - + Image image = horizontalScrollSnapRoot.AddComponent(); image.sprite = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpriteResourcePath); @@ -240,7 +241,6 @@ namespace UnityEditor.UI RectTransform rectTransformContent = childContent.GetComponent(); rectTransformContent.anchorMin = Vector2.zero; rectTransformContent.anchorMax = new Vector2(1f, 1f); - //rectTransformContent.anchoredPosition = Vector2.zero; rectTransformContent.sizeDelta = Vector2.zero; sr.content = rectTransformContent; @@ -249,15 +249,13 @@ namespace UnityEditor.UI Image pageImage = childPage01.AddComponent(); pageImage.sprite = AssetDatabase.GetBuiltinExtraResource(kStandardSpritePath); pageImage.type = Image.Type.Sliced; - pageImage.color = s_DefaultSelectableColor; - + pageImage.color = s_DefaultSelectableColor; + RectTransform rectTransformPage01 = childPage01.GetComponent(); rectTransformPage01.anchorMin = new Vector2(0f, 0.5f); rectTransformPage01.anchorMax = new Vector2(0f, 0.5f); - //rectTransformPage01.anchoredPosition = Vector2.zero; - //rectTransformPage01.sizeDelta = Vector2.zero; - rectTransformPage01.pivot = new Vector2(0f, 0.5f); - + rectTransformPage01.pivot = new Vector2(0f, 0.5f); + //Setup Text on Page01 Text text = childText.AddComponent(); text.text = "Page_01"; @@ -268,13 +266,10 @@ namespace UnityEditor.UI RectTransform rectTransformPage01Text = childText.GetComponent(); rectTransformPage01Text.anchorMin = new Vector2(0.5f, 0.5f); rectTransformPage01Text.anchorMax = new Vector2(0.5f, 0.5f); - //rectTransformPage01Text.anchoredPosition = Vector2.zero; - //rectTransformPage01Text.sizeDelta = Vector2.zero; rectTransformPage01Text.pivot = new Vector2(0.5f, 0.5f); //Need to add example child components like in the Asset (SJ) - Selection.activeGameObject = horizontalScrollSnapRoot; } @@ -350,6 +345,131 @@ namespace UnityEditor.UI Selection.activeGameObject = verticalScrollSnapRoot; } + #region New ScrollSnapCode + static public void FixedScrollSnapBase(MenuCommand menuCommand, string name, ScrollSnap.ScrollDirection direction, int itemVisible, int itemCount, Vector2 itemSize) + { + GameObject scrollSnapRoot = CreateUIElementRoot(name, menuCommand, s_ThickGUIElementSize); + GameObject itemList = CreateUIObject("List", scrollSnapRoot); + + // Set RectTransform to stretch + RectTransform rectTransformScrollSnapRoot = scrollSnapRoot.GetComponent(); + rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; + + if (direction == ScrollSnap.ScrollDirection.Horizontal) + { + rectTransformScrollSnapRoot.sizeDelta = new Vector2(itemVisible * itemSize.x, itemSize.y); + } + else + { + rectTransformScrollSnapRoot.sizeDelta = new Vector2(itemSize.x, itemVisible * itemSize.y); + } + + Image image = scrollSnapRoot.AddComponent(); + image.sprite = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpriteResourcePath); + image.type = Image.Type.Sliced; + image.color = new Color(1f, 1f, 1f, 1f); + + Mask listMask = scrollSnapRoot.AddComponent(); + listMask.showMaskGraphic = false; + + ScrollRect scrollRect = scrollSnapRoot.AddComponent(); + scrollRect.vertical = direction == ScrollSnap.ScrollDirection.Vertical; + scrollRect.horizontal = direction == ScrollSnap.ScrollDirection.Horizontal; + + ScrollSnap scrollSnap = scrollSnapRoot.AddComponent(); + scrollSnap.direction = direction; + scrollSnap.itemsVisibleAtOnce = itemVisible; + + //Setup Content container + RectTransform rectTransformContent = itemList.GetComponent(); + rectTransformContent.anchorMin = Vector2.zero; + rectTransformContent.anchorMax = new Vector2(1f, 1f); + rectTransformContent.sizeDelta = Vector2.zero; + scrollRect.content = rectTransformContent; + + //Setup Item list container + if (direction == ScrollSnap.ScrollDirection.Horizontal) + { + itemList.AddComponent(); + ContentSizeFitter sizeFitter = itemList.AddComponent(); + sizeFitter.horizontalFit = ContentSizeFitter.FitMode.MinSize; + } + else + { + itemList.AddComponent(); + ContentSizeFitter sizeFitter = itemList.AddComponent(); + sizeFitter.verticalFit = ContentSizeFitter.FitMode.MinSize; + } + + //Setup children + for (var i = 0; i < itemCount; i++) + { + GameObject item = CreateUIObject(string.Format("Item_{0:00}", i), itemList); + GameObject childText = CreateUIObject("Text", item); + + Image pageImage = item.AddComponent(); + pageImage.sprite = AssetDatabase.GetBuiltinExtraResource(kStandardSpritePath); + pageImage.type = Image.Type.Sliced; + pageImage.color = s_DefaultSelectableColor; + + LayoutElement elementLayout = item.AddComponent(); + if (direction == ScrollSnap.ScrollDirection.Horizontal) + { + elementLayout.minWidth = itemSize.x; + } + else + { + elementLayout.minHeight = itemSize.y; + } + + RectTransform rectTransformPage01 = item.GetComponent(); + rectTransformPage01.anchorMin = new Vector2(0f, 0.5f); + rectTransformPage01.anchorMax = new Vector2(0f, 0.5f); + rectTransformPage01.pivot = new Vector2(0f, 0.5f); + + //Setup Text on Page01 + Text text = childText.AddComponent(); + text.text = item.name; + text.alignment = TextAnchor.MiddleCenter; + text.color = new Color(0.196f, 0.196f, 0.196f); + + //Setup Text 1st Child + RectTransform rectTransformPage01Text = childText.GetComponent(); + rectTransformPage01Text.anchorMin = new Vector2(0.5f, 0.5f); + rectTransformPage01Text.anchorMax = new Vector2(0.5f, 0.5f); + rectTransformPage01Text.pivot = new Vector2(0.5f, 0.5f); + } + Selection.activeGameObject = scrollSnapRoot; + } + + [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Horizontal Single Item", false)] + static public void AddFixedItemScrollSnapHorizontalSingle(MenuCommand menuCommand) + { + FixedScrollSnapBase(menuCommand, "Scroll Snap Horizontal Single", ScrollSnap.ScrollDirection.Horizontal, 1, 3, new Vector2(100, 100)); + } + + [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Horizontal Multiple Items", false)] + static public void AddFixedItemScrollSnapHorizontalMultiple(MenuCommand menuCommand) + { + FixedScrollSnapBase(menuCommand, "Scroll Snap Horizontal Multiple", ScrollSnap.ScrollDirection.Horizontal, 3, 15, new Vector2(100, 100)); + } + + [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Vertical Single Item", false)] + static public void AddFixedItemScrollSnapVerticalSingle(MenuCommand menuCommand) + { + FixedScrollSnapBase(menuCommand, "Scroll Snap Vertical Multiple", ScrollSnap.ScrollDirection.Vertical, 1, 3, new Vector2(100, 100)); + } + + [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Vertical Multiple Items", false)] + static public void AddFixedItemScrollSnapVerticalMultiple(MenuCommand menuCommand) + { + FixedScrollSnapBase(menuCommand, "Scroll Snap Vertical Multiple", ScrollSnap.ScrollDirection.Vertical, 3, 15, new Vector2(100, 100)); + } + #endregion + + #endregion [MenuItem("GameObject/UI/Extensions/UI Button", false)] static public void AddUIButton(MenuCommand menuCommand) @@ -397,6 +517,7 @@ namespace UnityEditor.UI Selection.activeGameObject = go; } + #region Accordian [MenuItem("GameObject/UI/Extensions/Accordion/Accordion Group", false)] static public void AddAccordionGroup(MenuCommand menuCommand) { @@ -417,7 +538,9 @@ namespace UnityEditor.UI Selection.activeGameObject = go; } +#endregion + #region Drop Down controls [MenuItem("GameObject/UI/Extensions/AutoComplete ComboBox", false)] static public void AddAutoCompleteComboBox(MenuCommand menuCommand) { @@ -437,7 +560,7 @@ namespace UnityEditor.UI //Create Arrow Button GameObject arrowButton = AddButtonAsChild(autoCompleteComboBoxRoot); - + //Setup ComboBox var autoCompleteComboBox = autoCompleteComboBoxRoot.AddComponent(); var cbbRT = autoCompleteComboBoxRoot.GetComponent(); @@ -671,8 +794,8 @@ namespace UnityEditor.UI mainButtonTextRT.anchorMin = Vector2.zero; mainButtonTextRT.anchorMin = Vector2.zero; mainButtonTextRT.pivot = new Vector2(0f, 1f); - mainButtonTextRT.offsetMin = new Vector2(10f,0f); - mainButtonTextRT.offsetMax = new Vector2(-4f,0f); + mainButtonTextRT.offsetMin = new Vector2(10f, 0f); + mainButtonTextRT.offsetMax = new Vector2(-4f, 0f); //Main Button Image var mainButtonImageRT = mainButtonImage.GetComponent(); mainButtonImageRT.anchorMin = Vector2.zero; @@ -682,7 +805,7 @@ namespace UnityEditor.UI mainButtonImageRT.offsetMin = new Vector2(4f, 4f); mainButtonImageRT.offsetMax = new Vector2(22f, -4f); mainButtonImageRT.GetComponent().color = new Color(1, 1, 1, 0); - + //Setup Overlay var overlayRT = overlay.GetComponent(); @@ -741,7 +864,9 @@ namespace UnityEditor.UI arrowTextCanvasGroup.blocksRaycasts = false; Selection.activeGameObject = dropDownListRoot; } +#endregion + #region RTS Selection box [MenuItem("GameObject/UI/Extensions/Selection Box", false)] static public void AddSelectionBox(MenuCommand menuCommand) { @@ -773,7 +898,9 @@ namespace UnityEditor.UI Selection.activeGameObject = go; } +#endregion + #region Bound Tooltip [MenuItem("GameObject/UI/Extensions/Bound Tooltip/Tooltip", false)] static public void AddBoundTooltip(MenuCommand menuCommand) { @@ -781,7 +908,7 @@ namespace UnityEditor.UI var tooltip = go.AddComponent(); tooltip.text = "This is my Tooltip Text"; var boundTooltip = go.AddComponent(); - boundTooltip.sprite = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpriteResourcePath); + boundTooltip.sprite = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpriteResourcePath); // if there is no ToolTipItem add one... CreateToolTipItem(false, go); @@ -804,7 +931,7 @@ namespace UnityEditor.UI boundTooltipItemCanvasGroup.interactable = false; boundTooltipItemCanvasGroup.blocksRaycasts = false; var boundTooltipItemImage = boundTooltipItem.AddComponent(); - boundTooltipItemImage.sprite = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpriteResourcePath); + boundTooltipItemImage.sprite = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpriteResourcePath); var boundTooltipItemText = CreateUIObject("Text", boundTooltipItem); var boundTooltipItemTextRT = boundTooltipItemText.GetComponent(); boundTooltipItemTextRT.anchorMin = Vector2.zero; @@ -821,6 +948,9 @@ namespace UnityEditor.UI } } +#endregion + + #region Progress bar [MenuItem("GameObject/UI/Extensions/Progress Bar", false)] static public void AddSlider(MenuCommand menuCommand) { @@ -865,6 +995,9 @@ namespace UnityEditor.UI slider.direction = Slider.Direction.LeftToRight; SetDefaultColorTransitionValues(slider); } +#endregion + + #region Primitives [MenuItem("GameObject/UI/Extensions/Primitives/UI Line Renderer", false)] static public void AddUILineRenderer(MenuCommand menuCommand) @@ -881,7 +1014,7 @@ namespace UnityEditor.UI go.AddComponent(); Selection.activeGameObject = go; } - + [MenuItem("GameObject/UI/Extensions/Primitives/UI Circle", false)] static public void AddUICircle(MenuCommand menuCommand) { @@ -889,6 +1022,516 @@ namespace UnityEditor.UI go.AddComponent(); Selection.activeGameObject = go; } +#endregion + + #region Re-Orderable Lists + + [MenuItem("GameObject/UI/Extensions/Re-orderable Lists/Re-orderable Vertical Scroll Rect", false)] + static public void AddReorderableScrollRectVertical(MenuCommand menuCommand) + { + GameObject reorderableScrollRoot = CreateUIElementRoot("Re-orderable Vertical ScrollRect", menuCommand, s_ThickGUIElementSize); + + GameObject childContent = CreateUIObject("List_Content", reorderableScrollRoot); + + GameObject Element01 = CreateUIObject("Element_01", childContent); + GameObject Element02 = CreateUIObject("Element_02", childContent); + GameObject Element03 = CreateUIObject("Element_03", childContent); + + + // Set RectTransform to stretch + RectTransform rectTransformScrollSnapRoot = reorderableScrollRoot.GetComponent(); + rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; + rectTransformScrollSnapRoot.sizeDelta = new Vector2(150f, 150f); + + + Image image = reorderableScrollRoot.AddComponent(); + image.color = new Color(1f, 1f, 1f, 0.392f); + + ScrollRect sr = reorderableScrollRoot.AddComponent(); + sr.vertical = true; + sr.horizontal = false; + + LayoutElement reorderableScrollRootLE = reorderableScrollRoot.AddComponent(); + reorderableScrollRootLE.preferredHeight = 300; + + reorderableScrollRoot.AddComponent(); + ReorderableList reorderableScrollRootRL = reorderableScrollRoot.AddComponent(); + + //Setup Content container + RectTransform rectTransformContent = childContent.GetComponent(); + rectTransformContent.anchorMin = Vector2.zero; + rectTransformContent.anchorMax = new Vector2(1f, 1f); + rectTransformContent.sizeDelta = Vector2.zero; + VerticalLayoutGroup childContentVLG = childContent.AddComponent(); + ContentSizeFitter childContentCSF = childContent.AddComponent(); + childContentCSF.verticalFit = ContentSizeFitter.FitMode.PreferredSize; + + sr.content = rectTransformContent; + reorderableScrollRootRL.ContentLayout = childContentVLG; + + //Setup 1st Child + Image Element01Image = Element01.AddComponent(); + Element01Image.color = Color.green; + + RectTransform rectTransformElement01 = Element01.GetComponent(); + rectTransformElement01.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement01.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement01.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement01 = Element01.AddComponent(); + LEElement01.minHeight = 50; + + //Setup 2nd Child + Image Element02Image = Element02.AddComponent(); + Element02Image.color = Color.red; + + RectTransform rectTransformElement02 = Element02.GetComponent(); + rectTransformElement02.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement02.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement02.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement02 = Element02.AddComponent(); + LEElement02.minHeight = 50; + + //Setup 2nd Child + Image Element03Image = Element03.AddComponent(); + Element03Image.color = Color.blue; + + RectTransform rectTransformElement03 = Element03.GetComponent(); + rectTransformElement03.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement03.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement03.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement03 = Element03.AddComponent(); + LEElement03.minHeight = 50; + + + //Need to add example child components like in the Asset (SJ) + Selection.activeGameObject = reorderableScrollRoot; + } + + [MenuItem("GameObject/UI/Extensions/Re-orderable Lists/Re-orderable Horizontal Scroll Rect", false)] + static public void AddReorderableScrollRectHorizontal(MenuCommand menuCommand) + { + GameObject reorderableScrollRoot = CreateUIElementRoot("Re-orderable Horizontal ScrollRect", menuCommand, s_ThickGUIElementSize); + + GameObject childContent = CreateUIObject("List_Content", reorderableScrollRoot); + + GameObject Element01 = CreateUIObject("Element_01", childContent); + GameObject Element02 = CreateUIObject("Element_02", childContent); + GameObject Element03 = CreateUIObject("Element_03", childContent); + + + // Set RectTransform to stretch + RectTransform rectTransformScrollSnapRoot = reorderableScrollRoot.GetComponent(); + rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; + rectTransformScrollSnapRoot.sizeDelta = new Vector2(150f, 150f); + + + Image image = reorderableScrollRoot.AddComponent(); + image.color = new Color(1f, 1f, 1f, 0.392f); + + ScrollRect sr = reorderableScrollRoot.AddComponent(); + sr.vertical = false; + sr.horizontal = true; + + LayoutElement reorderableScrollRootLE = reorderableScrollRoot.AddComponent(); + reorderableScrollRootLE.preferredHeight = 300; + + reorderableScrollRoot.AddComponent(); + ReorderableList reorderableScrollRootRL = reorderableScrollRoot.AddComponent(); + + //Setup Content container + RectTransform rectTransformContent = childContent.GetComponent(); + rectTransformContent.anchorMin = Vector2.zero; + rectTransformContent.anchorMax = new Vector2(1f, 1f); + rectTransformContent.sizeDelta = Vector2.zero; + HorizontalLayoutGroup childContentHLG = childContent.AddComponent(); + ContentSizeFitter childContentCSF = childContent.AddComponent(); + childContentCSF.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; + + sr.content = rectTransformContent; + reorderableScrollRootRL.ContentLayout = childContentHLG; + + //Setup 1st Child + Image Element01Image = Element01.AddComponent(); + Element01Image.color = Color.green; + + RectTransform rectTransformElement01 = Element01.GetComponent(); + rectTransformElement01.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement01.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement01.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement01 = Element01.AddComponent(); + LEElement01.minWidth = 50; + + //Setup 2nd Child + Image Element02Image = Element02.AddComponent(); + Element02Image.color = Color.red; + + RectTransform rectTransformElement02 = Element02.GetComponent(); + rectTransformElement02.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement02.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement02.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement02 = Element02.AddComponent(); + LEElement02.minWidth = 50; + + //Setup 2nd Child + Image Element03Image = Element03.AddComponent(); + Element03Image.color = Color.blue; + + RectTransform rectTransformElement03 = Element03.GetComponent(); + rectTransformElement03.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement03.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement03.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement03 = Element03.AddComponent(); + LEElement03.minWidth = 50; + + + //Need to add example child components like in the Asset (SJ) + Selection.activeGameObject = reorderableScrollRoot; + } + + [MenuItem("GameObject/UI/Extensions/Re-orderable Lists/Re-orderable Grid Scroll Rect", false)] + static public void AddReorderableScrollRectGrid(MenuCommand menuCommand) + { + GameObject reorderableScrollRoot = CreateUIElementRoot("Re-orderable Grid ScrollRect", menuCommand, s_ThickGUIElementSize); + + GameObject childContent = CreateUIObject("List_Content", reorderableScrollRoot); + + GameObject Element01 = CreateUIObject("Element_01", childContent); + GameObject Element02 = CreateUIObject("Element_02", childContent); + GameObject Element03 = CreateUIObject("Element_03", childContent); + + + // Set RectTransform to stretch + RectTransform rectTransformScrollSnapRoot = reorderableScrollRoot.GetComponent(); + rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; + rectTransformScrollSnapRoot.sizeDelta = new Vector2(150f, 150f); + + + Image image = reorderableScrollRoot.AddComponent(); + image.color = new Color(1f, 1f, 1f, 0.392f); + + ScrollRect sr = reorderableScrollRoot.AddComponent(); + sr.vertical = true; + sr.horizontal = false; + + LayoutElement reorderableScrollRootLE = reorderableScrollRoot.AddComponent(); + reorderableScrollRootLE.preferredHeight = 300; + + reorderableScrollRoot.AddComponent(); + ReorderableList reorderableScrollRootRL = reorderableScrollRoot.AddComponent(); + + //Setup Content container + RectTransform rectTransformContent = childContent.GetComponent(); + rectTransformContent.anchorMin = Vector2.zero; + rectTransformContent.anchorMax = new Vector2(1f, 1f); + rectTransformContent.sizeDelta = Vector2.zero; + GridLayoutGroup childContentGLG = childContent.AddComponent(); + childContentGLG.cellSize = new Vector2(30, 30); + childContentGLG.spacing = new Vector2(10, 10); + ContentSizeFitter childContentCSF = childContent.AddComponent(); + childContentCSF.verticalFit = ContentSizeFitter.FitMode.PreferredSize; + + sr.content = rectTransformContent; + reorderableScrollRootRL.ContentLayout = childContentGLG; + + //Setup 1st Child + Image Element01Image = Element01.AddComponent(); + Element01Image.color = Color.green; + + RectTransform rectTransformElement01 = Element01.GetComponent(); + rectTransformElement01.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement01.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement01.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement01 = Element01.AddComponent(); + LEElement01.minHeight = 50; + + //Setup 2nd Child + Image Element02Image = Element02.AddComponent(); + Element02Image.color = Color.red; + + RectTransform rectTransformElement02 = Element02.GetComponent(); + rectTransformElement02.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement02.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement02.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement02 = Element02.AddComponent(); + LEElement02.minHeight = 50; + + //Setup 2nd Child + Image Element03Image = Element03.AddComponent(); + Element03Image.color = Color.blue; + + RectTransform rectTransformElement03 = Element03.GetComponent(); + rectTransformElement03.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement03.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement03.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement03 = Element03.AddComponent(); + LEElement03.minHeight = 50; + + + //Need to add example child components like in the Asset (SJ) + Selection.activeGameObject = reorderableScrollRoot; + } + + [MenuItem("GameObject/UI/Extensions/Re-orderable Lists/Re-orderable Vertical List", false)] + static public void AddReorderableVerticalList(MenuCommand menuCommand) + { + GameObject reorderableScrollRoot = CreateUIElementRoot("Re-orderable Vertial List", menuCommand, s_ThickGUIElementSize); + + GameObject childContent = CreateUIObject("List_Content", reorderableScrollRoot); + + GameObject Element01 = CreateUIObject("Element_01", childContent); + GameObject Element02 = CreateUIObject("Element_02", childContent); + GameObject Element03 = CreateUIObject("Element_03", childContent); + + + // Set RectTransform to stretch + RectTransform rectTransformScrollSnapRoot = reorderableScrollRoot.GetComponent(); + rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; + rectTransformScrollSnapRoot.sizeDelta = new Vector2(150f, 150f); + + Image image = reorderableScrollRoot.AddComponent(); + image.color = new Color(1f, 1f, 1f, 0.392f); + + LayoutElement reorderableScrollRootLE = reorderableScrollRoot.AddComponent(); + reorderableScrollRootLE.preferredHeight = 300; + + reorderableScrollRoot.AddComponent(); + ReorderableList reorderableScrollRootRL = reorderableScrollRoot.AddComponent(); + + //Setup Content container + RectTransform rectTransformContent = childContent.GetComponent(); + rectTransformContent.anchorMin = Vector2.zero; + rectTransformContent.anchorMax = new Vector2(1f, 1f); + rectTransformContent.sizeDelta = Vector2.zero; + VerticalLayoutGroup childContentVLG = childContent.AddComponent(); + ContentSizeFitter childContentCSF = childContent.AddComponent(); + childContentCSF.verticalFit = ContentSizeFitter.FitMode.PreferredSize; + + reorderableScrollRootRL.ContentLayout = childContentVLG; + + //Setup 1st Child + Image Element01Image = Element01.AddComponent(); + Element01Image.color = Color.green; + + RectTransform rectTransformElement01 = Element01.GetComponent(); + rectTransformElement01.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement01.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement01.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement01 = Element01.AddComponent(); + LEElement01.minHeight = 50; + + //Setup 2nd Child + Image Element02Image = Element02.AddComponent(); + Element02Image.color = Color.red; + + RectTransform rectTransformElement02 = Element02.GetComponent(); + rectTransformElement02.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement02.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement02.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement02 = Element02.AddComponent(); + LEElement02.minHeight = 50; + + //Setup 2nd Child + Image Element03Image = Element03.AddComponent(); + Element03Image.color = Color.blue; + + RectTransform rectTransformElement03 = Element03.GetComponent(); + rectTransformElement03.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement03.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement03.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement03 = Element03.AddComponent(); + LEElement03.minHeight = 50; + + + //Need to add example child components like in the Asset (SJ) + Selection.activeGameObject = reorderableScrollRoot; + } + + [MenuItem("GameObject/UI/Extensions/Re-orderable Lists/Re-orderable Horizontal List", false)] + static public void AddReorderableHorizontalList(MenuCommand menuCommand) + { + GameObject reorderableScrollRoot = CreateUIElementRoot("Re-orderable Horizontal List", menuCommand, s_ThickGUIElementSize); + + GameObject childContent = CreateUIObject("List_Content", reorderableScrollRoot); + + GameObject Element01 = CreateUIObject("Element_01", childContent); + GameObject Element02 = CreateUIObject("Element_02", childContent); + GameObject Element03 = CreateUIObject("Element_03", childContent); + + + // Set RectTransform to stretch + RectTransform rectTransformScrollSnapRoot = reorderableScrollRoot.GetComponent(); + rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; + rectTransformScrollSnapRoot.sizeDelta = new Vector2(150f, 150f); + + + Image image = reorderableScrollRoot.AddComponent(); + image.color = new Color(1f, 1f, 1f, 0.392f); + + LayoutElement reorderableScrollRootLE = reorderableScrollRoot.AddComponent(); + reorderableScrollRootLE.preferredHeight = 300; + + reorderableScrollRoot.AddComponent(); + ReorderableList reorderableScrollRootRL = reorderableScrollRoot.AddComponent(); + + //Setup Content container + RectTransform rectTransformContent = childContent.GetComponent(); + rectTransformContent.anchorMin = Vector2.zero; + rectTransformContent.anchorMax = new Vector2(1f, 1f); + rectTransformContent.sizeDelta = Vector2.zero; + HorizontalLayoutGroup childContentHLG = childContent.AddComponent(); + ContentSizeFitter childContentCSF = childContent.AddComponent(); + childContentCSF.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; + + reorderableScrollRootRL.ContentLayout = childContentHLG; + + //Setup 1st Child + Image Element01Image = Element01.AddComponent(); + Element01Image.color = Color.green; + + RectTransform rectTransformElement01 = Element01.GetComponent(); + rectTransformElement01.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement01.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement01.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement01 = Element01.AddComponent(); + LEElement01.minWidth = 50; + + //Setup 2nd Child + Image Element02Image = Element02.AddComponent(); + Element02Image.color = Color.red; + + RectTransform rectTransformElement02 = Element02.GetComponent(); + rectTransformElement02.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement02.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement02.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement02 = Element02.AddComponent(); + LEElement02.minWidth = 50; + + //Setup 2nd Child + Image Element03Image = Element03.AddComponent(); + Element03Image.color = Color.blue; + + RectTransform rectTransformElement03 = Element03.GetComponent(); + rectTransformElement03.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement03.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement03.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement03 = Element03.AddComponent(); + LEElement03.minWidth = 50; + + + //Need to add example child components like in the Asset (SJ) + Selection.activeGameObject = reorderableScrollRoot; + } + + [MenuItem("GameObject/UI/Extensions/Re-orderable Lists/Re-orderable Grid", false)] + static public void AddReorderableGrid(MenuCommand menuCommand) + { + GameObject reorderableScrollRoot = CreateUIElementRoot("Re-orderable Grid", menuCommand, s_ThickGUIElementSize); + + GameObject childContent = CreateUIObject("List_Content", reorderableScrollRoot); + + GameObject Element01 = CreateUIObject("Element_01", childContent); + GameObject Element02 = CreateUIObject("Element_02", childContent); + GameObject Element03 = CreateUIObject("Element_03", childContent); + + + // Set RectTransform to stretch + RectTransform rectTransformScrollSnapRoot = reorderableScrollRoot.GetComponent(); + rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); + rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; + rectTransformScrollSnapRoot.sizeDelta = new Vector2(150f, 150f); + + + Image image = reorderableScrollRoot.AddComponent(); + image.color = new Color(1f, 1f, 1f, 0.392f); + + LayoutElement reorderableScrollRootLE = reorderableScrollRoot.AddComponent(); + reorderableScrollRootLE.preferredHeight = 300; + + reorderableScrollRoot.AddComponent(); + ReorderableList reorderableScrollRootRL = reorderableScrollRoot.AddComponent(); + + //Setup Content container + RectTransform rectTransformContent = childContent.GetComponent(); + rectTransformContent.anchorMin = Vector2.zero; + rectTransformContent.anchorMax = new Vector2(1f, 1f); + rectTransformContent.sizeDelta = Vector2.zero; + GridLayoutGroup childContentGLG = childContent.AddComponent(); + childContentGLG.cellSize = new Vector2(30, 30); + childContentGLG.spacing = new Vector2(10, 10); + ContentSizeFitter childContentCSF = childContent.AddComponent(); + childContentCSF.verticalFit = ContentSizeFitter.FitMode.PreferredSize; + + reorderableScrollRootRL.ContentLayout = childContentGLG; + + //Setup 1st Child + Image Element01Image = Element01.AddComponent(); + Element01Image.color = Color.green; + + RectTransform rectTransformElement01 = Element01.GetComponent(); + rectTransformElement01.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement01.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement01.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement01 = Element01.AddComponent(); + LEElement01.minHeight = 50; + + //Setup 2nd Child + Image Element02Image = Element02.AddComponent(); + Element02Image.color = Color.red; + + RectTransform rectTransformElement02 = Element02.GetComponent(); + rectTransformElement02.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement02.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement02.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement02 = Element02.AddComponent(); + LEElement02.minHeight = 50; + + //Setup 2nd Child + Image Element03Image = Element03.AddComponent(); + Element03Image.color = Color.blue; + + RectTransform rectTransformElement03 = Element03.GetComponent(); + rectTransformElement03.anchorMin = new Vector2(0f, 0.5f); + rectTransformElement03.anchorMax = new Vector2(0f, 0.5f); + rectTransformElement03.pivot = new Vector2(0.5f, 0.5f); + + LayoutElement LEElement03 = Element03.AddComponent(); + LEElement03.minHeight = 50; + + + //Need to add example child components like in the Asset (SJ) + Selection.activeGameObject = reorderableScrollRoot; + } + + + #endregion #endregion @@ -1024,268 +1667,6 @@ namespace UnityEditor.UI } #endregion - - #region New ScrollSnapCode - static public void FixedScrollSnapBase(MenuCommand menuCommand, string name, ScrollSnap.ScrollDirection direction, int itemVisible, int itemCount, Vector2 itemSize) - { - - GameObject scrollSnapRoot = CreateUIElementRoot(name, menuCommand, s_ThickGUIElementSize); - - - - GameObject itemList = CreateUIObject("List", scrollSnapRoot); - - - - // Set RectTransform to stretch - - RectTransform rectTransformScrollSnapRoot = scrollSnapRoot.GetComponent(); - - rectTransformScrollSnapRoot.anchorMin = new Vector2(0.5f, 0.5f); - - rectTransformScrollSnapRoot.anchorMax = new Vector2(0.5f, 0.5f); - - rectTransformScrollSnapRoot.anchoredPosition = Vector2.zero; - - - - if (direction == ScrollSnap.ScrollDirection.Horizontal) - - { - - rectTransformScrollSnapRoot.sizeDelta = new Vector2(itemVisible * itemSize.x, itemSize.y); - - } - - else - - { - - rectTransformScrollSnapRoot.sizeDelta = new Vector2(itemSize.x, itemVisible * itemSize.y); - - } - - - - Image image = scrollSnapRoot.AddComponent(); - - image.sprite = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpriteResourcePath); - - image.type = Image.Type.Sliced; - - image.color = new Color(1f, 1f, 1f, 1f); - - - - Mask listMask = scrollSnapRoot.AddComponent(); - - listMask.showMaskGraphic = false; - - - - ScrollRect scrollRect = scrollSnapRoot.AddComponent(); - - scrollRect.vertical = direction == ScrollSnap.ScrollDirection.Vertical; - - scrollRect.horizontal = direction == ScrollSnap.ScrollDirection.Horizontal; - - - - ScrollSnap scrollSnap = scrollSnapRoot.AddComponent(); - - scrollSnap.direction = direction; - - scrollSnap.itemsVisibleAtOnce = itemVisible; - - - - //Setup Content container - - RectTransform rectTransformContent = itemList.GetComponent(); - - rectTransformContent.anchorMin = Vector2.zero; - - rectTransformContent.anchorMax = new Vector2(1f, 1f); - - //rectTransformContent.anchoredPosition = Vector2.zero; - - rectTransformContent.sizeDelta = Vector2.zero; - - scrollRect.content = rectTransformContent; - - - - //Setup Item list container - - if (direction == ScrollSnap.ScrollDirection.Horizontal) - - { - - itemList.AddComponent (); - - ContentSizeFitter sizeFitter = itemList.AddComponent(); - - sizeFitter.horizontalFit = ContentSizeFitter.FitMode.MinSize; - - } - - else - - { - - itemList.AddComponent (); - - ContentSizeFitter sizeFitter = itemList.AddComponent(); - - sizeFitter.verticalFit = ContentSizeFitter.FitMode.MinSize; - - } - - - - //Setup children - - - - for (var i = 0; i < itemCount; i ++) - - { - - GameObject item = CreateUIObject (string.Format("Item_{0:00}", i), itemList); - - - - GameObject childText = CreateUIObject ("Text", item); - - - - Image pageImage = item.AddComponent (); - - pageImage.sprite = AssetDatabase.GetBuiltinExtraResource (kStandardSpritePath); - - pageImage.type = Image.Type.Sliced; - - pageImage.color = s_DefaultSelectableColor; - - - - LayoutElement elementLayout = item.AddComponent (); - - if (direction == ScrollSnap.ScrollDirection.Horizontal) - - { - - elementLayout.minWidth = itemSize.x; - - } - - else - - { - - elementLayout.minHeight = itemSize.y; - - } - - - - RectTransform rectTransformPage01 = item.GetComponent (); - - rectTransformPage01.anchorMin = new Vector2 (0f, 0.5f); - - rectTransformPage01.anchorMax = new Vector2 (0f, 0.5f); - - //rectTransformPage01.anchoredPosition = Vector2.zero; - - //rectTransformPage01.sizeDelta = Vector2.zero; - - rectTransformPage01.pivot = new Vector2 (0f, 0.5f); - - - - //Setup Text on Page01 - - Text text = childText.AddComponent (); - - text.text = item.name; - - text.alignment = TextAnchor.MiddleCenter; - - text.color = new Color (0.196f, 0.196f, 0.196f); - - - - //Setup Text 1st Child - - RectTransform rectTransformPage01Text = childText.GetComponent (); - - rectTransformPage01Text.anchorMin = new Vector2 (0.5f, 0.5f); - - rectTransformPage01Text.anchorMax = new Vector2 (0.5f, 0.5f); - - //rectTransformPage01Text.anchoredPosition = Vector2.zero; - - //rectTransformPage01Text.sizeDelta = Vector2.zero; - - rectTransformPage01Text.pivot = new Vector2 (0.5f, 0.5f); - - } - - - - Selection.activeGameObject = scrollSnapRoot; - - } - - - - [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Horizontal Single Item", false)] - - static public void AddFixedItemScrollSnapHorizontalSingle(MenuCommand menuCommand) - - { - - FixedScrollSnapBase (menuCommand, "Scroll Snap Horizontal Single", ScrollSnap.ScrollDirection.Horizontal, 1, 3, new Vector2(100, 100)); - - } - - - - [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Horizontal Multiple Items", false)] - - static public void AddFixedItemScrollSnapHorizontalMultiple(MenuCommand menuCommand) - - { - - FixedScrollSnapBase (menuCommand, "Scroll Snap Horizontal Multiple", ScrollSnap.ScrollDirection.Horizontal, 3, 15, new Vector2(100, 100)); - - } - - - - [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Vertical Single Item", false)] - - static public void AddFixedItemScrollSnapVerticalSingle(MenuCommand menuCommand) - - { - - FixedScrollSnapBase (menuCommand, "Scroll Snap Vertical Multiple", ScrollSnap.ScrollDirection.Vertical, 1, 3, new Vector2(100, 100)); - - } - - - - [MenuItem("GameObject/UI/Extensions/Fixed Item Scroll/Snap Vertical Multiple Items", false)] - - static public void AddFixedItemScrollSnapVerticalMultiple(MenuCommand menuCommand) - - { - - FixedScrollSnapBase (menuCommand, "Scroll Snap Vertical Multiple", ScrollSnap.ScrollDirection.Vertical, 3, 15, new Vector2(100, 100)); - - } - - #endregion } } diff --git a/Scripts/ReorderableList.meta b/Scripts/ReorderableList.meta new file mode 100644 index 0000000..2f25e85 --- /dev/null +++ b/Scripts/ReorderableList.meta @@ -0,0 +1,5 @@ +fileFormatVersion: 2 +guid: 2172bd77b99a4a9409c147b803ae67df +folderAsset: yes +DefaultImporter: + userData: diff --git a/Scripts/ReorderableList/ReorderableList.cs b/Scripts/ReorderableList/ReorderableList.cs new file mode 100644 index 0000000..df876d7 --- /dev/null +++ b/Scripts/ReorderableList/ReorderableList.cs @@ -0,0 +1,99 @@ +/// Credit Ziboo +/// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/ + +using System; +using UnityEngine.Events; + +namespace UnityEngine.UI.Extensions +{ + [RequireComponent(typeof(RectTransform)), DisallowMultipleComponent] + [AddComponentMenu("UI/Extensions/Re-orderable list")] + public class ReorderableList : MonoBehaviour + { + [Tooltip("Child container with re-orderable items in a layout group")] + public LayoutGroup ContentLayout; + [Tooltip("Parent area to draw the dragged element on top of containers. Defaults to the root Canvas")] + public RectTransform DraggableArea; + + [Tooltip("Can items be dragged from the container?")] + public bool IsDraggable = true; + [Tooltip("Should the draggable components be removed or cloned?")] + public bool CloneDraggedObject = false; + + [Tooltip("Can new draggable items be dropped in to the container?")] + public bool IsDropable = true; + + + [Header("UI Re-orderable Events")] + public ReorderableListHandler OnElementDropped = new ReorderableListHandler(); + public ReorderableListHandler OnElementGrabbed = new ReorderableListHandler(); + public ReorderableListHandler OnElementRemoved = new ReorderableListHandler(); + + private RectTransform _content; + private ReorderableListContent _listContent; + + public RectTransform Content + { + get + { + if (_content == null) + { + _content = ContentLayout.GetComponent(); + } + return _content; + } + } + + private void Awake() + { + + if (ContentLayout == null) + { + Debug.LogError("You need to have a child LayoutGroup content set for the list: " + name, gameObject); + return; + } + if (DraggableArea == null) + { + DraggableArea = transform.root.GetComponentInChildren().GetComponent(); + } + if (IsDropable && !GetComponent()) + { + Debug.LogError("You need to have a Graphic control (such as an Image) for the list [" + name + "] to be droppable", gameObject); + return; + } + _listContent = ContentLayout.gameObject.AddComponent(); + _listContent.Init(this); + } + + #region Nested type: ReorderableListEventStruct + + [Serializable] + public struct ReorderableListEventStruct + { + public GameObject DroppedObject; + public int FromIndex; + public ReorderableList FromList; + public bool IsAClone; + public GameObject SourceObject; + public int ToIndex; + public ReorderableList ToList; + } + + #endregion + + #region Nested type: ReorderableListHandler + + [Serializable] + public class ReorderableListHandler : UnityEvent + { + } + + public void TestReOrderableListTarget(ReorderableListEventStruct item) + { + Debug.Log("Event Received"); + Debug.Log("Hello World, is my item a clone? [" + item.IsAClone + "]"); + } + + #endregion + } +} \ No newline at end of file diff --git a/Scripts/ReorderableList/ReorderableList.cs.meta b/Scripts/ReorderableList/ReorderableList.cs.meta new file mode 100644 index 0000000..0c82a31 --- /dev/null +++ b/Scripts/ReorderableList/ReorderableList.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 6b333d67eb08d464d823874f6a1666c2 +timeCreated: 1446072130 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/ReorderableList/ReorderableList.unity b/Scripts/ReorderableList/ReorderableList.unity new file mode 100644 index 0000000..2be9c02 Binary files /dev/null and b/Scripts/ReorderableList/ReorderableList.unity differ diff --git a/Scripts/ReorderableList/ReorderableList.unity.meta b/Scripts/ReorderableList/ReorderableList.unity.meta new file mode 100644 index 0000000..47431b0 --- /dev/null +++ b/Scripts/ReorderableList/ReorderableList.unity.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9b8d590979bc3264ab9a7df11a0e8c3c +timeCreated: 1446061891 +licenseType: Pro +DefaultImporter: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/ReorderableList/ReorderableListContent.cs b/Scripts/ReorderableList/ReorderableListContent.cs new file mode 100644 index 0000000..9d5b47c --- /dev/null +++ b/Scripts/ReorderableList/ReorderableListContent.cs @@ -0,0 +1,63 @@ +/// Credit Ziboo +/// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/ + +using System.Collections; +using System.Collections.Generic; + +namespace UnityEngine.UI.Extensions +{ + public class ReorderableListContent : MonoBehaviour + { + private List _cachedChildren; + private List _cachedListElement; + private ReorderableListElement _ele; + private ReorderableList _extList; + private RectTransform _rect; + + public void OnTransformChildrenChanged() + { + StartCoroutine(RefreshChildren()); + } + + public void Init(ReorderableList extList) + { + _extList = extList; + _rect = GetComponent(); + _cachedChildren = new List(); + _cachedListElement = new List(); + + StartCoroutine(RefreshChildren()); + } + + private IEnumerator RefreshChildren() + { + //Handle new chilren + for (int i = 0; i < _rect.childCount; i++) + { + if (_cachedChildren.Contains(_rect.GetChild(i))) + continue; + + //Get or Create ReorderableListElement + _ele = _rect.GetChild(i).gameObject.GetComponent() ?? + _rect.GetChild(i).gameObject.AddComponent(); + _ele.Init(_extList); + + _cachedChildren.Add(_rect.GetChild(i)); + _cachedListElement.Add(_ele); + } + + //HACK a little hack, if I don't wait one frame I don't have the right deleted children + yield return 0; + + //Remove deleted child + for (int i = _cachedChildren.Count - 1; i >= 0; i--) + { + if (_cachedChildren[i] == null) + { + _cachedChildren.RemoveAt(i); + _cachedListElement.RemoveAt(i); + } + } + } + } +} \ No newline at end of file diff --git a/Scripts/ReorderableList/ReorderableListContent.cs.meta b/Scripts/ReorderableList/ReorderableListContent.cs.meta new file mode 100644 index 0000000..513644a --- /dev/null +++ b/Scripts/ReorderableList/ReorderableListContent.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 252dd148b2c1dbe40b7d938a553e3caf +timeCreated: 1446062045 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/ReorderableList/ReorderableListDebug.cs b/Scripts/ReorderableList/ReorderableListDebug.cs new file mode 100644 index 0000000..b051c88 --- /dev/null +++ b/Scripts/ReorderableList/ReorderableListDebug.cs @@ -0,0 +1,29 @@ +/// Credit Ziboo +/// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/ + +namespace UnityEngine.UI.Extensions +{ + public class ReorderableListDebug : MonoBehaviour + { + public Text DebugLabel; + + void Awake() + { + foreach (var list in FindObjectsOfType()) + { + list.OnElementDropped.AddListener(ElementDropped); + } + } + + private void ElementDropped(ReorderableList.ReorderableListEventStruct droppedStruct) + { + DebugLabel.text = ""; + DebugLabel.text += "Dropped Object: " + droppedStruct.DroppedObject.name + "\n"; + DebugLabel.text += "Is Clone ?: " + droppedStruct.IsAClone + "\n"; + if (droppedStruct.IsAClone) + DebugLabel.text += "Source Object: " + droppedStruct.SourceObject.name + "\n"; + DebugLabel.text += string.Format("From {0} at Index {1} \n", droppedStruct.FromList.name, droppedStruct.FromIndex); + DebugLabel.text += string.Format("To {0} at Index {1} \n", droppedStruct.ToList.name, droppedStruct.ToIndex); + } + } +} \ No newline at end of file diff --git a/Scripts/ReorderableList/ReorderableListDebug.cs.meta b/Scripts/ReorderableList/ReorderableListDebug.cs.meta new file mode 100644 index 0000000..d9e54ed --- /dev/null +++ b/Scripts/ReorderableList/ReorderableListDebug.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 86c224aef3e999140b78d1d7135ba33f +timeCreated: 1446072313 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/ReorderableList/ReorderableListElement.cs b/Scripts/ReorderableList/ReorderableListElement.cs new file mode 100644 index 0000000..6d964be --- /dev/null +++ b/Scripts/ReorderableList/ReorderableListElement.cs @@ -0,0 +1,240 @@ +/// Credit Ziboo +/// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/ + +using System.Collections.Generic; +using UnityEngine.EventSystems; + +namespace UnityEngine.UI.Extensions +{ + + [RequireComponent(typeof(RectTransform))] + public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler + { + private readonly List _raycastResults = new List(); + private ReorderableList _currentReorderableListRaycasted; + private RectTransform _draggingObject; + private LayoutElement _draggingObjectLE; + private Vector2 _draggingObjectOriginalSize; + private RectTransform _fakeElement; + private LayoutElement _fakeElementLE; + private int _fromIndex; + private bool _isDragging; + private RectTransform _rect; + private ReorderableList _reorderableList; + + #region IBeginDragHandler Members + + public void OnBeginDrag(PointerEventData eventData) + { + if (_reorderableList == null) + return; + + //Can't drag, return... + if (!_reorderableList.IsDraggable) + { + _draggingObject = null; + return; + } + + //If CloneDraggedObject just set draggingObject to this gameobject + if (_reorderableList.CloneDraggedObject == false) + { + _draggingObject = _rect; + _fromIndex = _rect.GetSiblingIndex(); + //Send OnElementRemoved Event + if (_reorderableList.OnElementRemoved != null) + { + _reorderableList.OnElementRemoved.Invoke(new ReorderableList.ReorderableListEventStruct + { + DroppedObject = _draggingObject.gameObject, + IsAClone = _reorderableList.CloneDraggedObject, + SourceObject = _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject, + FromList = _reorderableList, + FromIndex = _fromIndex, + }); + } + } + //Else Duplicate + else + { + GameObject clone = (GameObject)Instantiate(gameObject); + _draggingObject = clone.GetComponent(); + } + + //Put _dragging object into the draggin area + _draggingObjectOriginalSize = gameObject.GetComponent().rect.size; + _draggingObjectLE = _draggingObject.GetComponent(); + _draggingObject.SetParent(_reorderableList.DraggableArea, false); + _draggingObject.SetAsLastSibling(); + + //Create a fake element for previewing placement + _fakeElement = new GameObject("Fake").AddComponent(); + _fakeElementLE = _fakeElement.gameObject.AddComponent(); + + + RefreshSizes(); + + //Send OnElementGrabbed Event + if (_reorderableList.OnElementGrabbed != null) + { + _reorderableList.OnElementGrabbed.Invoke(new ReorderableList.ReorderableListEventStruct + { + DroppedObject = _draggingObject.gameObject, + IsAClone = _reorderableList.CloneDraggedObject, + SourceObject = _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject, + FromList = _reorderableList, + FromIndex = _fromIndex, + }); + } + + _isDragging = true; + } + + #endregion + + #region IDragHandler Members + + public void OnDrag(PointerEventData eventData) + { + if (!_isDragging) + return; + + //Set dragging object on cursor + _draggingObject.position = eventData.position; + + //Check everything under the cursor to find a ReorderableList + EventSystem.current.RaycastAll(eventData, _raycastResults); + for (int i = 0; i < _raycastResults.Count; i++) + { + _currentReorderableListRaycasted = _raycastResults[i].gameObject.GetComponent(); + if (_currentReorderableListRaycasted != null) + { + break; + } + } + + //If nothing found or the list is not dropable, put the fake element outsite + if (_currentReorderableListRaycasted == null || _currentReorderableListRaycasted.IsDropable == false) + { + RefreshSizes(); + _fakeElement.transform.SetParent(_reorderableList.DraggableArea, false); + + } + //Else find the best position on the list and put fake element on the right index + else + { + if (_fakeElement.parent != _currentReorderableListRaycasted) + _fakeElement.SetParent(_currentReorderableListRaycasted.Content, false); + + float minDistance = float.PositiveInfinity; + int targetIndex = 0; + float dist = 0; + for (int j = 0; j < _currentReorderableListRaycasted.Content.childCount; j++) + { + var c = _currentReorderableListRaycasted.Content.GetChild(j).GetComponent(); + + if (_currentReorderableListRaycasted.ContentLayout is VerticalLayoutGroup) + dist = Mathf.Abs(c.position.y - eventData.position.y); + else if (_currentReorderableListRaycasted.ContentLayout is HorizontalLayoutGroup) + dist = Mathf.Abs(c.position.x - eventData.position.x); + else if (_currentReorderableListRaycasted.ContentLayout is GridLayoutGroup) + dist = (Mathf.Abs(c.position.x - eventData.position.x) + Mathf.Abs(c.position.y - eventData.position.y)); + + if (dist < minDistance) + { + minDistance = dist; + targetIndex = j; + } + } + + RefreshSizes(); + _fakeElement.SetSiblingIndex(targetIndex); + _fakeElement.gameObject.SetActive(true); + + } + } + + #endregion + + #region IEndDragHandler Members + + public void OnEndDrag(PointerEventData eventData) + { + _isDragging = false; + + if (_draggingObject != null) + { + //If we have a, ReorderableList that is dropable + //Put the dragged object into the content and at the right index + if (_currentReorderableListRaycasted != null && _currentReorderableListRaycasted.IsDropable) + { + RefreshSizes(); + _draggingObject.SetParent(_currentReorderableListRaycasted.Content, false); + _draggingObject.SetSiblingIndex(_fakeElement.GetSiblingIndex()); + + + //Send OnelementDropped Event + if (_reorderableList.OnElementDropped != null) + { + _reorderableList.OnElementDropped.Invoke(new ReorderableList.ReorderableListEventStruct + { + DroppedObject = _draggingObject.gameObject, + IsAClone = _reorderableList.CloneDraggedObject, + SourceObject = _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject, + FromList = _reorderableList, + FromIndex = _fromIndex, + ToList = _currentReorderableListRaycasted, + ToIndex = _fakeElement.GetSiblingIndex() - 1 + }); + } + } + //We don't have an ReorderableList + else + { + //If it's a clone, delete it + if (_reorderableList.CloneDraggedObject) + { + Destroy(_draggingObject.gameObject); + } + //Else replace the draggedObject to his first place + else + { + RefreshSizes(); + _draggingObject.SetParent(_reorderableList.Content, false); + _draggingObject.SetSiblingIndex(_fromIndex); + } + } + } + + //Delete fake element + if (_fakeElement != null) + Destroy(_fakeElement.gameObject); + } + + #endregion + + private void RefreshSizes() + { + Vector2 size = _draggingObjectOriginalSize; + + if (_currentReorderableListRaycasted != null && _currentReorderableListRaycasted.IsDropable) + { + var firstChild = _currentReorderableListRaycasted.Content.GetChild(0); + if (firstChild != null) + { + size = firstChild.GetComponent().rect.size; + } + } + + _draggingObject.sizeDelta = size; + _fakeElementLE.preferredHeight = _draggingObjectLE.preferredHeight = size.y; + _fakeElementLE.preferredWidth = _draggingObjectLE.preferredWidth = size.x; + } + + public void Init(ReorderableList reorderableList) + { + _reorderableList = reorderableList; + _rect = GetComponent(); + } + } +} \ No newline at end of file diff --git a/Scripts/ReorderableList/ReorderableListElement.cs.meta b/Scripts/ReorderableList/ReorderableListElement.cs.meta new file mode 100644 index 0000000..cd587fb --- /dev/null +++ b/Scripts/ReorderableList/ReorderableListElement.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 916e98f1b982a9a4082fcc45c87b66c5 +timeCreated: 1446072130 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: