Skip to content

Commit

Permalink
Merge pull request #23 from zhaozilong1988/release/v202409221240
Browse files Browse the repository at this point in the history
New Features: Add NestedScrollRect for implementing Netflix-like list
  • Loading branch information
zhaozilong1988 authored Sep 22, 2024
2 parents 0930608 + 1ee62a5 commit c2d7030
Show file tree
Hide file tree
Showing 11 changed files with 889 additions and 38 deletions.
619 changes: 619 additions & 0 deletions Assets/Scenes/Prefabs/SampleNestedScrollRectCell.prefab

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Assets/Scenes/Prefabs/SampleNestedScrollRectCell.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 29 additions & 25 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -205,8 +204,8 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 1698825100}
m_HandleRect: {fileID: 1698825099}
m_Direction: 2
m_Value: 1.0000074
m_Size: 0.98424566
m_Value: 1
m_Size: 0.98424554
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down Expand Up @@ -419,7 +418,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0.5}
m_AnchorMax: {x: 1, y: 0.5}
m_AnchoredPosition: {x: -1.9572144, y: 566.1}
m_AnchoredPosition: {x: -1.9572754, y: 566.1}
m_SizeDelta: {x: -37.055664, y: 50}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &317559963
Expand Down Expand Up @@ -508,7 +507,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: -0.00018310547, y: 50}
m_AnchoredPosition: {x: -0.00024414062, y: 50}
m_SizeDelta: {x: 0.38000488, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!1 &665227619
Expand Down Expand Up @@ -926,7 +925,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0.5}
m_AnchorMax: {x: 1, y: 0.5}
m_AnchoredPosition: {x: -60.987976, y: 0.000040412}
m_AnchoredPosition: {x: -60.988037, y: 0.000040412}
m_SizeDelta: {x: -288.7829, y: 86.152}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &735990436
Expand Down Expand Up @@ -1106,9 +1105,9 @@ RectTransform:
m_Father: {fileID: 1519432740}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_SizeDelta: {x: -17, y: 0}
m_Pivot: {x: 0, y: 1}
--- !u!114 &871036446
MonoBehaviour:
Expand Down Expand Up @@ -1313,7 +1312,7 @@ RectTransform:
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 1}
m_Pivot: {x: 1, y: 1}
--- !u!114 &935592392
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1477,7 +1476,7 @@ RectTransform:
m_Father: {fileID: 1556120408}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMax: {x: 0.22222222, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 10, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
Expand Down Expand Up @@ -1557,7 +1556,7 @@ RectTransform:
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 1}
m_Pivot: {x: 0, y: 0}
--- !u!114 &1010389357
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1733,9 +1732,10 @@ MonoBehaviour:
_viewport: {fileID: 935592391}
_content: {fileID: 879252848}
_direction: 3
_useNestedScrollRect: 0
_ignoreCellLifeCycle: 0
_tag: 0
keepClickEvenIfBeginDrag: 0
preserveClickEvenDragBegins: 0
_clickableCell: {fileID: 952083237558582737, guid: f63765b6dadb145f8a2fa180f201cf54,
type: 3}
_scrollToCell: {fileID: 8395097826947521673, guid: ea70a421904f64df580308872df66ead,
Expand Down Expand Up @@ -2151,8 +2151,8 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 1207748989}
m_HandleRect: {fileID: 1207748988}
m_Direction: 2
m_Value: 1.0000074
m_Size: 0.98424566
m_Value: 1
m_Size: 0.98424554
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down Expand Up @@ -2440,7 +2440,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &1519432740
RectTransform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2546,10 +2546,11 @@ MonoBehaviour:
_scrollRect: {fileID: 1519432744}
_viewport: {fileID: 871036445}
_content: {fileID: 1187111710}
_direction: 0
_direction: 2
_useNestedScrollRect: 0
_ignoreCellLifeCycle: 0
_tag: 0
keepClickEvenIfBeginDrag: 0
preserveClickEvenDragBegins: 0
_gridCell: {fileID: 8669581284404242529, guid: 34756287df3954d08a084bfea8364c58,
type: 3}
_emptyCell: {fileID: 9128829208683471527, guid: 34b5a7c3dfb7e465b9e6299654072206,
Expand Down Expand Up @@ -2688,8 +2689,8 @@ RectTransform:
m_Children: []
m_Father: {fileID: 845901574}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMin: {x: 0, y: 0.015754461}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 20, y: 20}
m_Pivot: {x: 0.5, y: 0.5}
Expand Down Expand Up @@ -2770,9 +2771,9 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0.025146484, y: -149.97498}
m_AnchoredPosition: {x: 0.025146484, y: 0.0501709}
m_SizeDelta: {x: 0, y: -300.05}
m_Pivot: {x: 0.5, y: 0.5}
m_Pivot: {x: 0.5, y: 0}
--- !u!114 &1780625969
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2856,10 +2857,11 @@ MonoBehaviour:
_scrollRect: {fileID: 1780625971}
_viewport: {fileID: 1010389356}
_content: {fileID: 686237908}
_direction: 0
_direction: 2
_useNestedScrollRect: 0
_ignoreCellLifeCycle: 0
_tag: 0
keepClickEvenIfBeginDrag: 0
preserveClickEvenDragBegins: 0
_imageCellPrefab: {fileID: 6296921478661641863, guid: 5a54b65bbf2034e0c90a804a1cfa9477,
type: 3}
_textCellPrefab: {fileID: 4096419684011045254, guid: 02911a5be46854bbbafe8c571686f8f7,
Expand All @@ -2868,6 +2870,8 @@ MonoBehaviour:
type: 3}
_chatCellPrefab: {fileID: 1994050357953850282, guid: 26fbf4e29086542479d6974264bb7cb5,
type: 3}
_nestedScrollRectCellPrefab: {fileID: 6737104380962007161, guid: 261b2d4ab7f8e43369bcc3efa671eb17,
type: 3}
--- !u!1 &1815890945
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2900,8 +2904,8 @@ RectTransform:
m_Children: []
m_Father: {fileID: 1093677553}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchorMin: {x: 0.22222222, y: 0}
m_AnchorMax: {x: 0.22222222, y: 1}
m_AnchoredPosition: {x: 16.23352, y: 0}
m_SizeDelta: {x: 52.4669, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using UIKit;
using UnityEngine;
using UnityEngine.UI;

public class SampleNestedScrollRectCell : UITableViewCell, IUITableViewDataSource
{
[SerializeField] Text _indexText;
[SerializeField] UITableView _nestedTable;
[SerializeField] SampleGridCell _cell;

int _nestedCellCount;

public void UpdateData(int cellIndex)
{
_indexText.text = cellIndex.ToString();
_nestedCellCount = 10;
_nestedTable.dataSource = this;
_nestedTable.ReloadData();
}

public UITableViewCell CellAtIndexInTableView(UITableView tableView, int cellIndex)
{
var cell = tableView.ReuseOrCreateCell(_cell);
cell.UpdateData(cellIndex, SampleGridView.Mode.Normal);
return cell;
}

public int NumberOfCellsInTableView(UITableView tableView)
{
return _nestedCellCount;
}

public float LengthForCellInTableView(UITableView tableView, int cellIndex)
{
return cellIndex % 2 == 0 ? 200f : 250f;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 48 additions & 7 deletions Assets/Scenes/Scripts/SampleClickableTableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnityEngine;
using UnityEngine.EventSystems;

public class SampleClickableTableView : UITableView, IUITableViewDataSource, IUITableViewDelegate, IUITableViewClickable, IUITableViewMargin
public class SampleClickableTableView : UITableView, IUITableViewDataSource, IUITableViewDelegate, IUITableViewClickable, IUITableViewMargin, IUITableViewFlickable, IUITableViewMagneticAlignment
{
[SerializeField] SampleClickableCell _clickableCell;
[SerializeField] SampleScrollToCell _scrollToCell;
Expand All @@ -25,6 +25,9 @@ protected override void Awake()
@delegate = this;
marginDataSource = this;
clickable = this;

magneticAlignment = this;
flickable = this;
}

public void ReloadDataForGridView()
Expand All @@ -35,7 +38,7 @@ public void ReloadDataForGridView()

public void ReloadDataForTableView()
{
Reload(Title.TableOrGrid, Title.ReverseDirection, Title.Append, Title.Prepend, Title.ScrollTo);
Reload(Title.TableOrGrid, Title.ReverseDirection, Title.Append, Title.Prepend);
ReloadData(0);
}

Expand Down Expand Up @@ -63,7 +66,7 @@ public int NumberOfCellsInTableView(UITableView tableView)

public float LengthForCellInTableView(UITableView tableView, int index)
{
return 200f;
return 300f;
}

public void CellAtIndexInTableViewWillAppear(UITableView tableView, int index)
Expand All @@ -88,7 +91,8 @@ public void TableViewOnPointerDownCellAt(UITableView tableView, int index, Point

public void TableViewOnPointerClickCellAt(UITableView tableView, int index, PointerEventData eventData)
{
Debug.Log($"{_metaList[index].title} cell is clicked.");
Debug.Log($"{index} cell is clicked.");

switch (_metaList[index].title) {
case Title.TableOrGrid:
this.onClickTableOrGridViewCell.Invoke();
Expand Down Expand Up @@ -130,9 +134,6 @@ public void TableViewOnPointerUpCellAt(UITableView tableView, int index, Pointer

public float LengthForUpperMarginInTableView(UITableView tableView, int rowIndex)
{
if (rowIndex == 0) {
return 10f;
}
return 5f;
}

Expand Down Expand Up @@ -163,4 +164,44 @@ public Meta(Title t)
this.selected = false;
}
}

public (float lower, float upper) FlickDistanceRangeOfTriggerFlickInTableView(UITableView tableView)
{
return DEFAULT_FLICK_DISTANCE_RANGE_OF_TRIGGER_FLICK;
}

public float FlickTimeOfTriggerFlickInTableView(UITableView tableView)
{
return DEFAULT_FLICK_TIME_OF_TRIGGER_FLICK;
}

public void TableViewOnDidFlick(UITableView tableView, int? indexOfFlickedCell, UITableViewDirection direction)
{
if (indexOfFlickedCell.HasValue) {
var toIndex = direction.IsTopToBottomOrRightToLeft() ? indexOfFlickedCell.Value + 1 : indexOfFlickedCell.Value - 1;
toIndex = Mathf.Clamp(toIndex, 0, _metaList.Count - 1);
ScrollToCellAt(toIndex, 0.5f);
}
}

public float SpeedOfTriggerMagneticAlignmentInTableView(UITableView tableView)
{
return DEFAULT_SPEED_OF_TRIGGER_MAGNETIC_ALIGNMENT;
}

public float SpeedOfCompleteMagneticAlignmentInTableView(UITableView tableView)
{
return DEFAULT_SPEED_OF_COMPLETE_MAGNETIC_ALIGNMENT;
}

public Vector2 CalibrationPointOfMagneticAlignmentInTableView(UITableView tableView)
{
// セルの基準点は中央なので、左寄せの場合、「セルの長さ * 0.5 / viewportの幅」はページングの照準点になる。
return new Vector2(LengthForCellInTableView(this, 0) * 0.5f / scrollRect.viewport.rect.width, 0.5f);
}

public void MagneticStateDidChangeInTableView(UITableView tableView, int ofCellIndex, UITableViewMagneticState state)
{
// Debug.Log("MagneticStateDidChangeInTableView: " + ofCellIndex);
}
}
1 change: 1 addition & 0 deletions Assets/Scenes/Scripts/SampleData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum SampleType
Image,
Tab,
Chat,
NestedScrollRect,
}

public SampleType sampleType;
Expand Down
15 changes: 14 additions & 1 deletion Assets/Scenes/Scripts/SampleTableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SampleTableView : UITableView, IUITableViewDataSource, IUITableView
[SerializeField] SampleTextCell _textCellPrefab;
[SerializeField] SampleTabCell _tabCellPrefab;
[SerializeField] SampleChatCell _chatCellPrefab;
[SerializeField] SampleNestedScrollRectCell _nestedScrollRectCellPrefab;

readonly List<SampleData> _tab1DataList = new List<SampleData>();
readonly List<SampleData> _tab2DataList = new List<SampleData>();
Expand Down Expand Up @@ -59,7 +60,12 @@ IEnumerable<SampleData> CreateSamples(int count)
for (var i = 0; i < count; i++)
{
var data = new SampleData();
if (Random.Range(i, count) % 2 == 0)
if (Random.Range(i, count) % 3 == 0)
{
data.sampleType = SampleData.SampleType.NestedScrollRect;
data.scalar = 300f;
}
else if (Random.Range(i, count) % 3 == 1)
{
data.sampleType = SampleData.SampleType.Text;
data.scalar = 75f + Random.Range(0f, 100f);
Expand Down Expand Up @@ -149,6 +155,8 @@ public UITableViewCell CellAtIndexInTableView(UITableView tableView, int index)
return tableView.ReuseOrCreateCell(_tabCellPrefab, UITableViewCellLifeCycle.RecycleWhenReloaded);
case SampleData.SampleType.Chat:
return tableView.ReuseOrCreateCell(_chatCellPrefab);
case SampleData.SampleType.NestedScrollRect:
return tableView.ReuseOrCreateCell(_nestedScrollRectCellPrefab);
default:
throw new ArgumentOutOfRangeException();
}
Expand Down Expand Up @@ -193,6 +201,11 @@ public void CellAtIndexInTableViewWillAppear(UITableView tableView, int index)
chatCell.UpdateData(index, data.text);
lifeCycle = chatCell.lifeCycle;
break;
case SampleData.SampleType.NestedScrollRect:
var nestedScrollRectCell = tableView.GetLoadedCell<SampleNestedScrollRectCell>(index);
nestedScrollRectCell.UpdateData(index);
lifeCycle = nestedScrollRectCell.lifeCycle;
break;
default:
throw new ArgumentOutOfRangeException();
}
Expand Down
Loading

0 comments on commit c2d7030

Please sign in to comment.