• 竖倾斜ScrollView


    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class ObliqueScroll : MonoBehaviour,IDragHandler
    {
        public RectTransform viewport;
        public ObliqueScrollContent content;
    
        private Transform target;
        private float space_min;
        private float space_max;
    
        private Vector3 position_c;
    
        private void OnValidate()
        {
            if (content != null)
            {
                target = content.transform;
                space_min = 0;
                space_max = target.childCount * (content.v_cell.y + content.v_space) - viewport.rect.height;
                if (space_max < 0)
                {
                    space_max = 0;
                }
            }
        }
    
        public void OnDrag(PointerEventData eventData)
        {
            float move_c = eventData.delta.y;
            position_c = target.localPosition;
            position_c.y += move_c;
            position_c = CheckOut(position_c);
            target.localPosition = position_c;
        }
    
        private Vector3 CheckOut(Vector3 position)
        {
            if (position.y <= space_min)
            {
                position.y = space_min;
            }
            if (position.y >= space_max)
            {
                position.y = space_max;
            }
            return position;
        }
    }
    View Code
    using UnityEngine;
    
    public class ObliqueScrollContent : MonoBehaviour
    {
        public Vector2 v_cell;
        public float v_space;
        public float v_offset;
    
        private Vector2 half = new Vector2(0.5f, 0.5f);
    
        private void OnValidate()
        {
            Refresh();
        }
    
        private void Refresh()
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                Transform cell = transform.GetChild(i);
                float pos_x = -i * v_offset;
                float pos_y = -i * (v_cell.y + v_space);
                RefreshCell(cell, new Vector3(pos_x, pos_y, 0));
            }
        }
    
        private void RefreshCell(Transform cell, Vector3 position)
        {
            RectTransform rt = (RectTransform)cell;
            rt.anchorMin = half;
            rt.anchorMax = half;
            rt.pivot = half;
            rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, v_cell.x);
            rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, v_cell.y);
            rt.localPosition = position;
        }
    }
    View Code

    只是一个思路,拖动很生硬

    Scroll(加ObliqueScroll, 倾斜X度)

      viewport(加mask)

        content(加ObliqueScrollContent, 倾斜-X度)

          item1

          item2

          item3

  • 相关阅读:
    【缓慢的自学ing】 自己写一个《Minecraft》记录
    Unity随笔3:按钮的"导航"功能
    【随随随随笔】一些STL的糖
    【算法氵】筛法
    【纯感慨】好吃的蒟蒻
    【随随随随笔】OJ错题记录
    【纯感慨】最不擅长的就是配置软件
    【C++Primer笔记】顶层const、常量指针
    hdu 2475 动态树
    hdu 1281 棋盘游戏 网络流
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/9620197.html
Copyright © 2020-2023  润新知