Skip to content

Commit

Permalink
剥离DataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
qiankanglai committed Jul 18, 2017
1 parent dfc2743 commit e113137
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
37 changes: 37 additions & 0 deletions Assets/Scripts/LoopScrollDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using UnityEngine;
using System.Collections;

namespace UnityEngine.UI
{
public abstract class LoopScrollDataSource
{
public abstract void ProvideData(Transform transform, int idx);
}

public class LoopScrollSendIndexSource : LoopScrollDataSource
{
public static readonly LoopScrollSendIndexSource Instance = new LoopScrollSendIndexSource();

LoopScrollSendIndexSource(){}

public override void ProvideData(Transform transform, int idx)
{
transform.SendMessage("ScrollCellIndex", idx);
}
}

public class LoopScrollArraySource<T> : LoopScrollDataSource
{
T[] objectsToFill;

public LoopScrollArraySource(T[] objectsToFill)
{
this.objectsToFill = objectsToFill;
}

public override void ProvideData(Transform transform, int idx)
{
transform.SendMessage("ScrollCellContent", objectsToFill[idx]);
}
}
}
12 changes: 12 additions & 0 deletions Assets/Scripts/LoopScrollDataSource.cs.meta

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

37 changes: 18 additions & 19 deletions Assets/Scripts/LoopScrollRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ public abstract class LoopScrollRect : UIBehaviour, IInitializePotentialDragHand
public int totalCount;
[HideInInspector]
public int poolSize = 5;
[HideInInspector]
[NonSerialized]
public object[] objectsToFill = null;

[HideInInspector]
[NonSerialized]
public LoopScrollDataSource dataSource = LoopScrollSendIndexSource.Instance;
public object[] objectsToFill
{
// wrapper for forward compatbility
set
{
if(value != null)
dataSource = new LoopScrollArraySource<object>(value);
else
dataSource = LoopScrollSendIndexSource.Instance;
}
}

[Tooltip("Threshold for preloading")]
public float threshold = 100;
[Tooltip("Reverse direction for dragging")]
Expand Down Expand Up @@ -269,20 +282,6 @@ protected LoopScrollRect()
}

//==========LoopScrollRect==========
private void SendMessageToNewObject(Transform go, int idx)
{
go.SendMessage("ScrollCellIndex", idx);
if (objectsToFill != null)
{
object o = null;
if (idx >= 0 && idx < objectsToFill.Length)
{
o = objectsToFill[idx];
}
go.SendMessage("ScrollCellContent", o, SendMessageOptions.DontRequireReceiver);
}
}

private void ReturnObjectAndSendMessage(Transform go)
{
go.SendMessage("ScrollCellReturn", SendMessageOptions.DontRequireReceiver);
Expand Down Expand Up @@ -314,7 +313,7 @@ public void RefreshCells()
{
if (itemTypeEnd < totalCount)
{
SendMessageToNewObject(content.GetChild(i), itemTypeEnd);
dataSource.ProvideData(content.GetChild(i), itemTypeEnd);
itemTypeEnd++;
}
else
Expand Down Expand Up @@ -536,7 +535,7 @@ private RectTransform InstantiateNextItem(int itemIdx)
RectTransform nextItem = ResourceManager.Instance.GetObjectFromPool(name, true, count).GetComponent<RectTransform>();
nextItem.transform.SetParent(content, false);
nextItem.gameObject.SetActive(true);
SendMessageToNewObject(nextItem, itemIdx);
dataSource.ProvideData(nextItem, itemIdx);
return nextItem;
}
//==========LoopScrollRect==========
Expand Down

0 comments on commit e113137

Please sign in to comment.