首页 > 其他分享 >自制滑动块滑动界面

自制滑动块滑动界面

时间:2023-01-29 09:44:14浏览次数:33  
标签:界面 自制 private content Slider using 组件 滑动

1、新建组件Scroll View,调整适合大小,修改组件参数

(垂直滑动,取消自带水平,垂直滑动块)

 

2、新建Slider组件,调整大小及组件参数

 

 3、编写脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScrollViewManager : MonoBehaviour
{
    private Slider m_Slider;
    private RectTransform content;
    private RectTransform m_Transform;

    private void Start()
    {
        m_Slider = transform.Find("Slider").GetComponent<Slider>();
        content = transform.Find("Viewport/Content").GetComponent<RectTransform>();
        m_Transform = GetComponent<RectTransform>();

        //设置滑动条最大距离
        float maxValue = content.sizeDelta.y - m_Transform.sizeDelta.y;

        m_Slider.minValue = 0;
        m_Slider.maxValue = maxValue;

        //滑动条赋值滑动显示界面位置
        m_Slider.onValueChanged.AddListener(delegate (float value)
        {
            content.anchoredPosition = new Vector2(0, m_Slider.value);  
        });
    }

    private void Update()
    {

        m_Slider.value = content.anchoredPosition.y;
      
    }
}

 

标签:界面,自制,private,content,Slider,using,组件,滑动
From: https://www.cnblogs.com/syq208/p/17071781.html

相关文章