public static GameObject CreateLoopHorizontalScrollRect(DefaultControls.Resources resources) { GameObject root = CreateUIElementRoot("LoopHorizontalScrollRect", new Vector2(200, 200)); root.GetAddComponent <Image>().color = Color.gray; GameObject viewPort = CreateViewPort(root.transform); GameObject content = CreateUIObject("Content", viewPort); RectTransform contentRT = content.GetComponent <RectTransform>(); contentRT.anchorMin = new Vector2(0, 1f); contentRT.anchorMax = new Vector2(1, 1f); contentRT.sizeDelta = new Vector2(0, 200); contentRT.pivot = new Vector2(0, 1f); // Setup UI components. LoopHorizontalScrollRect scrollRect = root.AddComponent <LoopHorizontalScrollRect>(); scrollRect.content = contentRT; scrollRect.viewport = null; scrollRect.horizontalScrollbar = null; scrollRect.verticalScrollbar = null; scrollRect.horizontal = true; scrollRect.vertical = false; scrollRect.horizontalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent; scrollRect.verticalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent; scrollRect.horizontalScrollbarSpacing = 0; scrollRect.verticalScrollbarSpacing = 0; CreateHorizontalDefaultScrollBar(root.transform, scrollRect); CreateVerticalDefaultScrollBar(root.transform, scrollRect); Image img = viewPort.AddComponent <Image>(); img.raycastTarget = true; Mask mask = viewPort.AddComponent <Mask>(); mask.showMaskGraphic = false; HorizontalLayoutGroup layoutGroup = content.AddComponent <HorizontalLayoutGroup>(); layoutGroup.childAlignment = TextAnchor.MiddleLeft; layoutGroup.childForceExpandWidth = false; layoutGroup.childForceExpandHeight = true; ContentSizeFitter sizeFitter = content.AddComponent <ContentSizeFitter>(); sizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; sizeFitter.verticalFit = ContentSizeFitter.FitMode.Unconstrained; return(root); }
public static GameObject CreateLoopHorizontalScrollRect(DefaultControls.Resources resources) { GameObject root = CreateUIElementRoot("Loop Horizontal Scroll Rect", new Vector2(200, 200)); GameObject content = CreateUIObject("Content", root); RectTransform contentRT = content.GetComponent <RectTransform>(); contentRT.anchorMin = new Vector2(0, 0.5f); contentRT.anchorMax = new Vector2(0, 0.5f); contentRT.sizeDelta = new Vector2(0, 200); contentRT.pivot = new Vector2(0, 0.5f); // Setup UI components. LoopHorizontalScrollRect scrollRect = root.AddComponent <LoopHorizontalScrollRect>(); scrollRect.content = contentRT; scrollRect.viewport = null; scrollRect.horizontalScrollbar = null; scrollRect.verticalScrollbar = null; scrollRect.horizontal = true; scrollRect.vertical = false; scrollRect.horizontalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent; scrollRect.verticalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent; scrollRect.horizontalScrollbarSpacing = 0; scrollRect.verticalScrollbarSpacing = 0; root.AddComponent <RectMask2D>(); root.AddComponent <YYEmpty4RaycastBtn>(); //为了在任何view的位置都可以拖动 HorizontalLayoutGroup layoutGroup = content.AddComponent <HorizontalLayoutGroup>(); layoutGroup.childAlignment = TextAnchor.MiddleLeft; layoutGroup.childControlHeight = true; layoutGroup.childControlWidth = true; layoutGroup.childForceExpandWidth = false; layoutGroup.childForceExpandHeight = false; ContentSizeFitter sizeFitter = content.AddComponent <ContentSizeFitter>(); sizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; sizeFitter.verticalFit = ContentSizeFitter.FitMode.Unconstrained; return(root); }