private void Start() { if (!scrollRect) { scrollRect = GetComponent <UnityEngine.UI.ScrollRect>(); } }
/// <summary> /// Scrolls immediately to a given object in the scrollview. /// /// <b>Important:</b> /// Unity UI's scrolling component and UI setup is complex and full of variations. Therefore this /// function may not always give the expected results. Simply try it. If it does not work, you may copy the /// implementation of this function into your own code and customize as needed. /// </summary> /// <param name="scrollRect">ScrollView that should perform the scrolling.</param> /// <param name="focusObject">Object to scroll to. Must be a child of scrollRect's content object.</param> /// <param name="keepX">If true, scrolling does not apply to X.</param> /// <param name="keepY">If true, scrolling does not apply to Y.</param> /// <seealso cref="https://stackoverflow.com/questions/30766020/how-to-scroll-to-a-specific-element-in-scrollrect-with-unity-ui"/> public static void scrollJumpTo(UnityEngine.UI.ScrollRect scrollRect, GameObject focusObject, bool keepX = false, bool keepY = false) { RectTransform contentPanel = scrollRect.content; RectTransform target = focusObject.GetComponent <RectTransform>(); Canvas.ForceUpdateCanvases(); Vector2 result = (Vector2)scrollRect.transform.InverseTransformPoint(contentPanel.position) //current scrollpos - ( (Vector2)scrollRect.transform.InverseTransformPoint((Vector2)target.position) //... pos of object ); result.x += (scrollRect.GetComponent <RectTransform>().rect.width / 2); //try to scroll object into center result.y -= (scrollRect.GetComponent <RectTransform>().rect.height / 2); //try to scroll object into center //Debug.Log("scrollRect.rect="+(scrollRect.GetComponent<RectTransform>().rect)+", result="+result); result.x = Mathf.Clamp(result.x, 0, contentPanel.rect.width); result.y = Mathf.Clamp(result.y, -contentPanel.rect.height, 0); if (keepX) { result.x = contentPanel.anchoredPosition.x; } if (keepY) { result.y = contentPanel.anchoredPosition.y; } //scrollRect.inertia = false; contentPanel.anchoredPosition = result; scrollRect.StopMovement(); //Important: Stop any flow that may come from inertia, because it will falsify the programmatic scrolling results here (as we want a hard but reliable jump to a fixed position) //Debug.Log("-> result=" + result +", c.ap="+ contentPanel.anchoredPosition+"c.rs="+contentPanel.rect.height); }
protected override void ClearUIComponents() { ImgWaterMask = null; IntegralScrollView = null; BtnBack = null; mData = null; }
/// <summary> /// 构建水平滑动,主要是添加自动布局 /// </summary> /// <param name="scrollRect"></param> /// <param name="layer"></param> public void BuildHorizonScrollView(UnityEngine.UI.ScrollRect scrollRect, Layer layer) { scrollRect.vertical = false; scrollRect.horizontal = true; //水平默认从左向右滑动 scrollRect.content.anchorMin = Vector2.zero; scrollRect.content.anchorMax = new Vector2(0, 1); scrollRect.content.pivot = new Vector2(0, 0.5f); //锚定左侧,否则动态增长时会从两边添加cell var contentSizeFilter = scrollRect.content.GetComponent <ContentSizeFitter>(); contentSizeFilter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; contentSizeFilter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; var hLayout = scrollRect.content.gameObject.AddComponent <HorizontalLayoutGroup>(); //添加水平布局组件 if (layer.arguments.Length < 4) { Debug.LogWarning("ScrollView arguments error !"); } else { float spacing; if (float.TryParse(layer.arguments[1], out spacing)) { hLayout.spacing = spacing; } int left, top; if (int.TryParse(layer.arguments[2], out left) && int.TryParse(layer.arguments[3], out top)) { hLayout.padding = new RectOffset(left, left, top, top); } } }
protected override void OnInit() { _uiTile = GetComponent <UnityEngine.UI.LayoutGroup>(); cachedTransform = _uiTile.transform; if (Widget.parent != null && Widget.parent.parent != null) { scrollView = Widget.parent.parent.gameObject.GetComponentInChildren <UnityEngine.UI.ScrollRect>(); //GetComponent<ScrollRect>(); } if (scrollView == null) { scrollView = Widget.parent.parent.gameObject.GetComponent <UnityEngine.UI.ScrollRect>(); } if (scrollView != null) { UnityAction <Vector2> valueChange = new UnityAction <Vector2>(OnValueChanged); scrollView.onValueChanged.AddListener(valueChange); } GridLayoutGroup tempGroup = _uiTile as GridLayoutGroup; if (isHorizontal) { nLineWidth = tempGroup.cellSize.x + tempGroup.spacing.x; } else { nLineWidth = tempGroup.cellSize.y + tempGroup.spacing.y; } base.OnInit(); }
public void DrawLayer(Layer layer, GameObject parent) { //UnityEngine.UI.ScrollRect temp = AssetDatabase.LoadAssetAtPath(PSDImporterConst.PREFAB_PATH_SCROLLVIEW, typeof(UnityEngine.UI.ScrollRect)) as UnityEngine.UI.ScrollRect; UnityEngine.UI.ScrollRect scrollRect = PSDImportUtility.LoadAndInstant <UnityEngine.UI.ScrollRect>(PSDImporterConst.ASSET_PATH_SCROLLVIEW, layer.name, parent); //scrollRect.transform.SetParent(parent.transform, false); //parent = parent.transform; RectTransform rectTransform = scrollRect.GetComponent <RectTransform>(); rectTransform.sizeDelta = new Vector2(layer.size.width, layer.size.height); rectTransform.anchoredPosition = new Vector2(layer.position.x, layer.position.y); if (layer.layers != null) { string type = layer.arguments[0].ToUpper(); switch (type) { case "V": //scrollRect.vertical = true; //scrollRect.horizontal = false; if (layer.arguments.Length > 4) { BuildGridScrollView(scrollRect, layer, parent, true); return; } else { BuildVerticalScrollView(scrollRect, layer); } break; case "H": //scrollRect.vertical = false; //scrollRect.horizontal = true; if (layer.arguments.Length > 4) { BuildGridScrollView(scrollRect, layer, parent, false); return; } else { BuildHorizonScrollView(scrollRect, layer); } break; default: break; } ctrl.DrawLayers(layer.layers, scrollRect.content.gameObject); } }
protected override void ClearUIComponents() { ImgWaterMask = null; ExchangeRecordPanel = null; ExchangeScrollView = null; ExchangeNODataContent = null; IntegralRecordPanel = null; IntegralGetScrollView = null; RecordNODataContent = null; IntegralExChangelToggle = null; IntegralRecorderToggle = null; BtnBack = null; mData = null; }
/// <summary> /// scrollrect 与 grid layout 搭配 /// </summary> /// <param name="scrollRect"></param> /// <param name="layer"></param> public void BuildGridScrollView(UnityEngine.UI.ScrollRect scrollRect, Layer layer, GameObject parent, bool vertical) { scrollRect.vertical = vertical; scrollRect.horizontal = !vertical; //水平默认从左向右滑动 scrollRect.content.anchorMin = Vector2.zero; scrollRect.content.anchorMax = new Vector2(0, 1); scrollRect.content.pivot = new Vector2(0, 1); //锚定左侧,否则动态增长时会从两边添加cell var contentSizeFilter = scrollRect.content.GetComponent <ContentSizeFitter>(); contentSizeFilter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; contentSizeFilter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; UnityEngine.UI.GridLayoutGroup gridLayoutGroup = scrollRect.content.gameObject.AddComponent <GridLayoutGroup>(); //添加水平布局组件 if (vertical) { gridLayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount; gridLayoutGroup.constraintCount = System.Convert.ToInt32(layer.arguments[5]); } else { gridLayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount; gridLayoutGroup.constraintCount = System.Convert.ToInt32(layer.arguments[4]); } RectTransform rectTransform = gridLayoutGroup.GetComponent <RectTransform>(); rectTransform.sizeDelta = Vector2.zero; //new Vector2(layer.size.width, layer.size.height); rectTransform.anchoredPosition = Vector2.zero; //new Vector2(layer.position.x, layer.position.y); //int left, top; //if (int.TryParse(layer.arguments[2], out left) && int.TryParse(layer.arguments[3], out top)) //{ // gridLayoutGroup.padding = new RectOffset(left, left, top, top); //} float width, height; if (float.TryParse(layer.arguments[6], out width) && float.TryParse(layer.arguments[7], out height)) { gridLayoutGroup.cellSize = new Vector2(width, height); } gridLayoutGroup.spacing = new Vector2(System.Convert.ToInt32(layer.arguments[8]), System.Convert.ToInt32(layer.arguments[9])); ctrl.DrawLayers(layer.layers, gridLayoutGroup.gameObject); }
static int get_movementType(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.UI.ScrollRect.MovementType ret = obj.movementType; ToLua.Push(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index movementType on a nil value" : e.Message)); } }
static int set_horizontalScrollbar(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.UI.Scrollbar arg0 = (UnityEngine.UI.Scrollbar)ToLua.CheckUnityObject(L, 2, typeof(UnityEngine.UI.Scrollbar)); obj.horizontalScrollbar = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index horizontalScrollbar on a nil value" : e.Message)); } }
static int get_content(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.RectTransform ret = obj.content; ToLua.Push(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index content on a nil value" : e.Message)); } }
static int get_horizontal(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; bool ret = obj.horizontal; LuaDLL.lua_pushboolean(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index horizontal on a nil value" : e.Message)); } }
static int get_flexibleHeight(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; float ret = obj.flexibleHeight; LuaDLL.lua_pushnumber(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index flexibleHeight on a nil value" : e.Message)); } }
static int set_verticalNormalizedPosition(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; float arg0 = (float)LuaDLL.luaL_checknumber(L, 2); obj.verticalNormalizedPosition = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index verticalNormalizedPosition on a nil value" : e.Message)); } }
static int set_horizontalScrollbarSpacing(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; float arg0 = (float)LuaDLL.luaL_checknumber(L, 2); obj.horizontalScrollbarSpacing = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index horizontalScrollbarSpacing on a nil value" : e.Message)); } }
static int get_onValueChanged(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.UI.ScrollRect.ScrollRectEvent ret = obj.onValueChanged; ToLua.PushObject(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index onValueChanged on a nil value" : e.Message)); } }
static int get_velocity(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.Vector2 ret = obj.velocity; ToLua.Push(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index velocity on a nil value" : e.Message)); } }
static int set_verticalScrollbarVisibility(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.UI.ScrollRect.ScrollbarVisibility arg0 = (UnityEngine.UI.ScrollRect.ScrollbarVisibility)ToLua.CheckObject(L, 2, typeof(UnityEngine.UI.ScrollRect.ScrollbarVisibility)); obj.verticalScrollbarVisibility = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index verticalScrollbarVisibility on a nil value" : e.Message)); } }
static int set_inertia(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; bool arg0 = LuaDLL.luaL_checkboolean(L, 2); obj.inertia = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index inertia on a nil value" : e.Message)); } }
static int set_movementType(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.UI.ScrollRect.MovementType arg0 = (UnityEngine.UI.ScrollRect.MovementType)ToLua.CheckObject(L, 2, typeof(UnityEngine.UI.ScrollRect.MovementType)); obj.movementType = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index movementType on a nil value" : e.Message)); } }
static int get_layoutPriority(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; int ret = obj.layoutPriority; LuaDLL.lua_pushinteger(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index layoutPriority on a nil value" : e.Message)); } }
static int get_GetScrollRect(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UITableView obj = (UITableView)o; UnityEngine.UI.ScrollRect ret = obj.GetScrollRect; ToLua.Push(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index GetScrollRect on a nil value")); } }
static int set_normalizedPosition(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.Vector2 arg0 = ToLua.ToVector2(L, 2); obj.normalizedPosition = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index normalizedPosition on a nil value" : e.Message)); } }
static int set_viewport(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.RectTransform arg0 = (UnityEngine.RectTransform)ToLua.CheckUnityObject(L, 2, typeof(UnityEngine.RectTransform)); obj.viewport = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index viewport on a nil value" : e.Message)); } }
static int get_verticalNormalizedPosition(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; float ret = obj.verticalNormalizedPosition; LuaDLL.lua_pushnumber(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index verticalNormalizedPosition on a nil value" : e.Message)); } }
static int set_onValueChanged(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; UnityEngine.UI.ScrollRect.ScrollRectEvent arg0 = (UnityEngine.UI.ScrollRect.ScrollRectEvent)ToLua.CheckObject(L, 2, typeof(UnityEngine.UI.ScrollRect.ScrollRectEvent)); obj.onValueChanged = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index onValueChanged on a nil value" : e.Message)); } }
public void DrawVarList() { DialogBox db = new DialogBox(new Vector2(UIScaler.GetHCenter(2f), 0.5f), new Vector2(16, 24.5f), StringKey.NULL); db.AddBorder(); db.background.AddComponent <UnityEngine.UI.Mask>(); UnityEngine.UI.ScrollRect scrollRect = db.background.AddComponent <UnityEngine.UI.ScrollRect>(); GameObject scrollArea = new GameObject("scroll"); RectTransform scrollInnerRect = scrollArea.AddComponent <RectTransform>(); scrollArea.transform.parent = db.background.transform; scrollInnerRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 16f * UIScaler.GetPixelsPerUnit()); scrollInnerRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 1); scrollRect.content = scrollInnerRect; scrollRect.horizontal = false; // List of vars float offset = 1; valueDBE = new Dictionary <string, DialogBoxEditable>(); foreach (KeyValuePair <string, float> kv in Game.Get().quest.vars.vars) { string key = kv.Key; db = new DialogBox( new Vector2(UIScaler.GetHCenter(2.5f), offset), new Vector2(12, 1.2f), new StringKey(null, key, false), Color.black, Color.white); db.textObj.GetComponent <UnityEngine.UI.Text>().material = (Material)Resources.Load("Fonts/FontMaterial"); db.background.transform.parent = scrollArea.transform; db.AddBorder(); // Variables value modify dont need localization DialogBoxEditable dbe = new DialogBoxEditable( new Vector2(UIScaler.GetHCenter(14.5f), offset), new Vector2(3, 1.2f), kv.Value.ToString(), delegate { UpdateValue(key); }, Color.black, Color.white); dbe.setMaterialAndBackgroundTransformParent((Material)Resources.Load("Fonts/FontMaterial"), scrollArea.transform); dbe.AddBorder(); valueDBE.Add(key, dbe); offset += 1.4f; } scrollInnerRect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, (offset - 1) * UIScaler.GetPixelsPerUnit()); }
private void OnGameDidLoad() { if (screens != null) { UnityEngine.UI.ScrollRect scrollRect = gameObject.GetComponent <UnityEngine.UI.ScrollRect>(); ScrollRectSnap scrollSnap = gameObject.GetComponent <ScrollRectSnap>(); foreach (GameObject screenPrefab in screens) { GameObject screenObject = Instantiate(screenPrefab); screenObject.transform.SetParent(screenGrid.transform, false); BaseScrollScreen scrollScreen = screenObject.GetComponent <BaseScrollScreen>(); if (scrollScreen != null && scrollRect != null && scrollSnap != null) { scrollScreen.ScrollContainer = scrollRect; scrollScreen.ScrollSnapContainer = scrollSnap; } } } }
private System.Collections.IEnumerator start() { /* XXX: This must be delayed, otherwise unity ignores the changes to * the UI ¯\_(ツ)_/¯ */ yield return(null); int count = this.lastIdx - this.curIdx; float size = 1.0f / (float)count; this.view = this.gameObject.GetComponentInChildren <View>(); const Axis ax = Axis.Vertical; this.viewWidth = this.view.content.rect.width; this.viewHeight = elementDist + count * (elementHeight + elementDist); this.view.content.SetSizeWithCurrentAnchors(ax, this.viewHeight); this.view.verticalScrollbar.size = size; yield return(this.startLoadLevel()); }
static int set_horizontalScrollbar(IntPtr L) { object o = L.ToUserData(1); UnityEngine.UI.ScrollRect obj = (UnityEngine.UI.ScrollRect)o; if (obj == null) { LuaTypes types = L.Type(1); if (types == LuaTypes.LUA_TTABLE) { LuaDLL.luaL_error(L, "unknown member name horizontalScrollbar"); } else { LuaDLL.luaL_error(L, "attempt to index horizontalScrollbar on a nil value"); } } obj.horizontalScrollbar = L.ToComponent(3, typeof(UnityEngine.UI.Scrollbar)) as UnityEngine.UI.Scrollbar; return(0); }