123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- using BeinLab.Util;
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using XRTool.UI;
- using XRTool.Util;
- namespace XRTool.WorldUI
- {
-
-
-
- public enum LayoutType
- {
- None = -2,
- UnKnow = -1,
- GridLayoutGroup = 0,
- VerticalLayoutGroup = 1,
- HorizontalLayoutGroup = 2,
- }
-
-
-
-
-
-
- public class XRScrollRect : ScrollRect
- {
- public bool isLoop;
- public LayoutType layoutType = LayoutType.UnKnow;
- private int allCount;
- private int showCount;
- private Vector2 cellSize;
- private Vector2 cellSpace;
-
- private ItemCell superCell;
- private LayoutGroup layoutGroup;
- private List<ItemCell> cellList;
- private Bounds viewBounds;
-
-
- protected override void Start()
- {
- base.Start();
- viewBounds = new Bounds(viewRect.rect.center, viewRect.rect.size);
- }
- public LayoutGroup LayoutGroup
- {
- get
- {
- if (!layoutGroup)
- {
- layoutGroup = content.GetComponent<LayoutGroup>();
- }
- return layoutGroup;
- }
- }
- public List<ItemCell> CellList { get => cellList; set => cellList = value; }
- protected override void LateUpdate()
- {
- if ( velocity.magnitude > 66 || !IsMoveCalculatePosition())
- {
- base.LateUpdate();
- XRUpdateItemVisibility();
- }
- }
-
-
-
- private void XRUpdateItemVisibility()
- {
-
- if (allCount > 0 && showCount < allCount && content.childCount > 0)
- {
- for (int i = 0; i < content.childCount; i++)
- {
- RectTransform child = content.GetChild(i) as RectTransform;
- if (child.anchoredPosition.x < 0)
- {
- child.GetChild(0).gameObject.SetActive(false);
- }
- }
- }
- }
-
-
-
-
- public bool IsMoveCalculatePosition()
- {
- return false;
- }
-
-
-
-
-
-
-
-
- public void InitData(int count, int showCount, Vector2 cellSize, Vector3 cellSpace, ItemCell prefab)
- {
- this.allCount = count;
- this.showCount = showCount;
- this.cellSize = cellSize;
- this.cellSpace = cellSpace;
- this.superCell = prefab;
- CreateScrollList();
- }
- public void ClearScrollList()
- {
- if (content)
- {
- for (int i = 0; i < content.childCount; i++)
- {
- Destroy(content.GetChild(i).gameObject);
- }
- }
- CellList.Clear();
- }
-
-
-
- private void CreateScrollList(bool isClear = true)
- {
- if (CellList == null)
- {
- CellList = new List<ItemCell>();
- }
- if (isClear)
- {
- ClearScrollList();
- }
- GameObject prefab = superCell.GetInstance();
-
- if (prefab)
- {
- if (!isLoop)
- {
- int instanceCount = showCount;
- if (showCount >= allCount)
- {
- instanceCount = allCount;
- }
- for (int i = 0; i < instanceCount; i++)
- {
- GameObject obj = Instantiate(prefab);
- UnityUtil.SetParent(content, obj.transform);
- CellList.Add(obj.GetComponent<ItemCell>());
- }
- RectTransform body = GetComponent<RectTransform>();
- Vector2 size = body.sizeDelta;
- if (horizontal && !vertical)
- {
- size.y = cellSize.y;
-
- }
- else if (vertical && !horizontal)
- {
- size.x = cellSize.x;
- }
- }
- }
- if (LayoutGroup)
- {
- if (LayoutGroup is GridLayoutGroup)
- {
- var lay = (LayoutGroup as GridLayoutGroup);
- lay.cellSize = this.cellSize;
- lay.spacing = this.cellSpace;
- }
- else if (LayoutGroup is VerticalLayoutGroup)
- {
- var lay = (LayoutGroup as VerticalLayoutGroup);
- lay.spacing = this.cellSpace.y;
- }
- else if (LayoutGroup is HorizontalLayoutGroup)
- {
- var lay = (LayoutGroup as HorizontalLayoutGroup);
- lay.spacing = this.cellSpace.x;
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- public override void OnDrag(PointerEventData eventData)
- {
- base.OnDrag(eventData);
- }
- protected override void SetContentAnchoredPosition(Vector2 position)
- {
- base.SetContentAnchoredPosition(position);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- protected override void OnRectTransformDimensionsChange()
- {
- base.OnRectTransformDimensionsChange();
- viewBounds = new Bounds(viewRect.rect.center, viewRect.rect.size);
- }
- private static float XRRubberDelta(float overStretching, float viewSize)
- {
- return (1 - (1 / ((Mathf.Abs(overStretching) * 0.55f / viewSize) + 1))) * viewSize * Mathf.Sign(overStretching);
- }
- private Vector2 XRCalculateOffset(Vector2 delta)
- {
- return XRInternalCalculateOffset(ref viewBounds, ref m_ContentBounds, horizontal, vertical, movementType, ref delta);
- }
- internal static Vector2 XRInternalCalculateOffset(ref Bounds viewBounds, ref Bounds contentBounds, bool horizontal, bool vertical, MovementType movementType, ref Vector2 delta)
- {
- Vector2 offset = Vector2.zero;
- if (movementType == MovementType.Unrestricted)
- return offset;
- Vector2 min = contentBounds.min;
- Vector2 max = contentBounds.max;
-
- if (horizontal)
- {
- min.x += delta.x;
- max.x += delta.x;
- float maxOffset = viewBounds.max.x - max.x;
- float minOffset = viewBounds.min.x - min.x;
- if (minOffset < -0.001f)
- offset.x = minOffset;
- else if (maxOffset > 0.001f)
- offset.x = maxOffset;
- }
- if (vertical)
- {
- min.y += delta.y;
- max.y += delta.y;
- float maxOffset = viewBounds.max.y - max.y;
- float minOffset = viewBounds.min.y - min.y;
- if (maxOffset > 0.001f)
- offset.y = maxOffset;
- else if (minOffset < -0.001f)
- offset.y = minOffset;
- }
- return offset;
- }
- }
- }
|