|
@@ -1,116 +1,116 @@
|
|
|
-using UnityEngine;
|
|
|
+锘縰sing UnityEngine;
|
|
|
using System.Collections;
|
|
|
using UnityEngine.UI;
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public delegate void OnUpdateItem(GameObject item, int wrapIndex, int realIndex);
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public delegate void OnEatUpItem();
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public enum ArrangeType
|
|
|
{
|
|
|
- Horizontal = 0,
|
|
|
- Vertical = 1,
|
|
|
+ Horizontal = 0,
|
|
|
+ Vertical = 1,
|
|
|
}
|
|
|
|
|
|
public class UIScrollScript : MonoBehaviour
|
|
|
{
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public int cell_x = 100;
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public int cell_y = 100;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public int minIndex = 0;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public int maxCount = 0;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public ArrangeType arrangeType = ArrangeType.Horizontal;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public int ConstraintCount = 0;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
RectTransform mRTrans;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public ScrollRect mScroll;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
Vector2 pivotChildFactor = new Vector2(0.5f, 0.5f);
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
bool mHorizontal;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
List<Transform> ChildList = new List<Transform>();
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
private float extents = 0;
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
Vector2 SR_size = Vector2.zero;
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
Vector3[] conners = new Vector3[4];
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
Vector2 startPos;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public OnUpdateItem onUpdateItem;
|
|
|
|
|
@@ -126,19 +126,19 @@ public class UIScrollScript : MonoBehaviour
|
|
|
[SerializeField] private float bottomOffset = 0;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
[SerializeField] private float XSpacing = 0;
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
[SerializeField] private float YSpacing = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public void Init()
|
|
|
{
|
|
@@ -147,14 +147,14 @@ public class UIScrollScript : MonoBehaviour
|
|
|
|
|
|
SR_size = mScroll.transform.GetComponent<RectTransform>().rect.size;
|
|
|
|
|
|
-
|
|
|
+
|
|
|
conners[0] = new Vector3(-SR_size.x / 2f, SR_size.y / 2f, 0);
|
|
|
conners[1] = new Vector3(SR_size.x / 2f, SR_size.y / 2f, 0);
|
|
|
conners[2] = new Vector3(-SR_size.x / 2f, -SR_size.y / 2f, 0);
|
|
|
conners[3] = new Vector3(SR_size.x / 2f, -SR_size.y / 2f, 0);
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
{
|
|
|
Vector3 temp = mScroll.transform.TransformPoint(conners[i]);
|
|
@@ -162,14 +162,14 @@ public class UIScrollScript : MonoBehaviour
|
|
|
conners[i].y = temp.y;
|
|
|
}
|
|
|
|
|
|
- mRTrans.pivot = new Vector2(0, 1);
|
|
|
- mScroll.onValueChanged.AddListener(delegate { WrapContent(); });
|
|
|
+ mRTrans.pivot = new Vector2(0, 1);
|
|
|
+ mScroll.onValueChanged.AddListener(delegate { WrapContent(); });
|
|
|
startPos = this.transform.localPosition;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public void ResetPosition()
|
|
|
{
|
|
@@ -189,7 +189,7 @@ public class UIScrollScript : MonoBehaviour
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
public void InitList(int minIndex, int maxIndex)
|
|
@@ -198,13 +198,13 @@ public class UIScrollScript : MonoBehaviour
|
|
|
this.minIndex = minIndex;
|
|
|
this.maxCount = maxIndex;
|
|
|
|
|
|
-
|
|
|
+
|
|
|
if (ConstraintCount <= 0)
|
|
|
{
|
|
|
ConstraintCount = 1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
if (this.minIndex > this.maxCount)
|
|
|
{
|
|
|
this.minIndex = this.maxCount;
|
|
@@ -232,7 +232,7 @@ public class UIScrollScript : MonoBehaviour
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -246,29 +246,29 @@ public class UIScrollScript : MonoBehaviour
|
|
|
|
|
|
void ResetChildPosition()
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
int rows = 1, cols = 1;
|
|
|
|
|
|
-
|
|
|
+
|
|
|
Vector2 startAxis = new Vector2((cell_x / 2f) + leftOffset, (-cell_y / 2f) - topOffset);
|
|
|
|
|
|
if (arrangeType == ArrangeType.Horizontal)
|
|
|
{
|
|
|
rows = ConstraintCount;
|
|
|
cols = (int)Mathf.Ceil((float)ChildList.Count / rows);
|
|
|
-
|
|
|
+
|
|
|
extents = ((cols * cell_x) + (XSpacing * (cols - 1) + leftOffset + rightOffset)) * 0.5f;
|
|
|
}
|
|
|
else if (arrangeType == ArrangeType.Vertical)
|
|
|
{
|
|
|
cols = ConstraintCount;
|
|
|
rows = (int)Mathf.Ceil((float)ChildList.Count / (float)cols);
|
|
|
-
|
|
|
+
|
|
|
extents = ((rows * cell_y) + (YSpacing * (rows - 1) + topOffset + bottomOffset)) * 0.5f;
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
for (int i = 0; i < ChildList.Count; i++)
|
|
|
{
|
|
|
Transform temp = ChildList[i];
|
|
@@ -278,19 +278,19 @@ public class UIScrollScript : MonoBehaviour
|
|
|
trf.pivot = pivotChildFactor;
|
|
|
trf.anchoredPosition3D = Vector3.zero;
|
|
|
|
|
|
- int x = 0, y = 0;
|
|
|
+ int x = 0, y = 0;
|
|
|
Vector2 pos = Vector2.zero;
|
|
|
if (arrangeType == ArrangeType.Vertical)
|
|
|
{
|
|
|
- x = i / cols;
|
|
|
- y = i % cols;
|
|
|
+ x = i / cols;
|
|
|
+ y = i % cols;
|
|
|
pos = new Vector2((startAxis.x + y * cell_x) + y * XSpacing, (startAxis.y - x * cell_y) - (x * YSpacing));
|
|
|
}
|
|
|
|
|
|
else if (arrangeType == ArrangeType.Horizontal)
|
|
|
{
|
|
|
- x = i % rows;
|
|
|
- y = i / rows;
|
|
|
+ x = i % rows;
|
|
|
+ y = i / rows;
|
|
|
pos = new Vector2((startAxis.x + y * cell_x) + y * XSpacing, (startAxis.y - x * cell_y) - (x * YSpacing));
|
|
|
}
|
|
|
|
|
@@ -298,7 +298,7 @@ public class UIScrollScript : MonoBehaviour
|
|
|
if (minIndex == maxCount || (i >= minIndex && i < maxCount))
|
|
|
{
|
|
|
temp.gameObject.SetActive(true);
|
|
|
- UpdateRectsize(pos);
|
|
|
+ UpdateRectsize(pos);
|
|
|
UpdateItem(temp, i, i);
|
|
|
temp.name = i.ToString();
|
|
|
}
|
|
@@ -311,7 +311,7 @@ public class UIScrollScript : MonoBehaviour
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
void UpdateRectsize(Vector2 pos)
|
|
@@ -331,7 +331,7 @@ public class UIScrollScript : MonoBehaviour
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
public void WrapContent(bool isRefresh = false)
|
|
|
{
|
|
@@ -347,20 +347,20 @@ public class UIScrollScript : MonoBehaviour
|
|
|
temp.gameObject.SetActive(true);
|
|
|
if (isRefreshRect)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
UpdateRectsize(pos);
|
|
|
}
|
|
|
|
|
|
temp.localPosition = pos;
|
|
|
if (temp.name != realIndex.ToString())
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
UpdateItem(temp, i, realIndex);
|
|
|
temp.name = realIndex.ToString();
|
|
|
}
|
|
|
else if (temp.name == realIndex.ToString() && isRefresh)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
UpdateItem(temp, i, realIndex);
|
|
|
temp.name = realIndex.ToString();
|
|
|
}
|
|
@@ -369,8 +369,8 @@ public class UIScrollScript : MonoBehaviour
|
|
|
{
|
|
|
if (!isHandEatup)
|
|
|
{
|
|
|
- Debug.Log("WrapContent===>数据已用完");
|
|
|
-
|
|
|
+ Debug.Log("WrapContent===>鏁版嵁宸茬敤瀹�");
|
|
|
+
|
|
|
if (onEatUpItem != null)
|
|
|
{
|
|
|
onEatUpItem();
|
|
@@ -385,7 +385,7 @@ public class UIScrollScript : MonoBehaviour
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -398,7 +398,7 @@ public class UIScrollScript : MonoBehaviour
|
|
|
{
|
|
|
conner_local[i] = this.transform.InverseTransformPoint(conners[i]);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
Vector2 center = (conner_local[3] + conner_local[0]) / 2f;
|
|
|
Vector2 pos = Vector2.zero;
|
|
|
if (mHorizontal)
|
|
@@ -438,7 +438,7 @@ public class UIScrollScript : MonoBehaviour
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -447,8 +447,8 @@ public class UIScrollScript : MonoBehaviour
|
|
|
int x = (int)Mathf.Ceil((-pos.y - topOffset + YSpacing) / (cell_y + YSpacing)) - 1;
|
|
|
int y = (int)Mathf.Ceil((pos.x - leftOffset + XSpacing) / (cell_x + XSpacing)) - 1;
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
int realIndex;
|
|
|
if (arrangeType == ArrangeType.Vertical)
|
|
|
{
|
|
@@ -464,7 +464,7 @@ public class UIScrollScript : MonoBehaviour
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|