美文网首页
Unity制作滚动视图窗口

Unity制作滚动视图窗口

作者: 我一点也不瓜 | 来源:发表于2019-04-09 17:05 被阅读0次

    创建滚动视图。创建image作为画布的子对象,命名为"ScrollView"。为其添加Scroll Rect组件和Mask组件。(Scroll Rect组件为滚动组件,Mask为遮罩)
    因为只需要上下滚动,所以取消Scroll Rect组件的Horizontal勾选。


    1.png

    创建空的游戏对象为"ScrollView"的子元素,命名为"content",将其锚点设置为父元素的顶端。
    (可以从Rect Transform的Anchor Presets中,按住Alt和Shift同时选择center-top)

    2.png

    为"content"添加Vertical Layout Group组件和Content Size Fitter组件。
    Vertical Layout Group组件可以使"content"下的对象垂直排布。(取消Child Force Expand的Height勾选可以使对象不均匀排布)
    Content Size Fitter组件的Vertical Fit设置为Preferred Size可以让content根据对象的大小自动改变尺寸


    3.png

    将"content"设置为ScrollView附加的Scroll Rect组件的Content属性。


    4.png

    在"content"下添加对象便完成了滚动页面的制作。

    为了使滚动文本自动滚到底部,需要添加以下代码

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class ScrollText : MonoBehaviour           //滚动文本
    {
        public ScrollRect sc;       //获取滚动组件
        public RectTransform content;       //获取文本框
        private void Update()
        {
    
            if (content.rect.height >= 120)         //文本框会随text增加而加长,当大于某一值时,滚动到底部
            {
                sc.verticalNormalizedPosition = Mathf.Lerp(sc.verticalNormalizedPosition, 0f, Time.deltaTime * 10); //滚轮平滑向下滚动           
            }
           
        }
    
      
    

    相关文章

      网友评论

          本文标题:Unity制作滚动视图窗口

          本文链接:https://www.haomeiwen.com/subject/ytciiqtx.html