| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using LuaInterface;
- public delegate void LuaAutoMoveCallback(UnityEngine.UI.Text Text);
- public class UiLoopAutoMove : MonoBehaviour
- {
- public GameObject[] mArrayItem;
- public RectTransform[] mArrayRect;
- public float[] defaultPosArr;
- public float fHeight = 40.0f;
- private float fAllHeight = 40.0f;
- public float fMoveSpeed = 10.0f;
- private bool bIsRun = false;
- private LuaAutoMoveCallback mLuaOnSelectedCallback = null;
- void Start()
- {
- mArrayRect = new RectTransform[mArrayItem.Length];
- defaultPosArr = new float[mArrayItem.Length];
- for (int i = 0; i < mArrayItem.Length; i++)
- {
- mArrayRect[i] = mArrayItem[i].GetComponent<RectTransform>();
- defaultPosArr[i] = mArrayRect[i].localPosition.y;
- }
- }
- void Update()
- {
- if(bIsRun)
- {
- Move();
- }
- }
- public void Reset() {
- for (int i = 0; i < mArrayItem.Length; i++)
- {
- Vector2 v2MovePos = mArrayRect[i].localPosition;
- v2MovePos.y = defaultPosArr[i];
- mArrayRect[i].localPosition = v2MovePos;
- UnityEngine.UI.Text UiText = mArrayItem[i].GetComponentInChildren<UnityEngine.UI.Text>();
- if (mLuaOnSelectedCallback != null)
- {
- mLuaOnSelectedCallback.DynamicInvoke(UiText);
- }
- }
- }
- public void SetText(int nIndex ,string strInfo)
- {
- if (null != mArrayItem)
- {
- fAllHeight = fHeight * mArrayItem.Length;
- }
- if (nIndex > mArrayItem.Length)
- return;
-
- UnityEngine.UI.Text UiText = mArrayItem[nIndex].GetComponentInChildren<UnityEngine.UI.Text>();
- if (null != UiText)
- {
- UiText.text = strInfo;
- }
- }
- public void Init(string[] ArrayStrInfo)
- {
- if (null != mArrayItem)
- {
- fAllHeight = fHeight * mArrayItem.Length;
- }
- int nMinLenth = ArrayStrInfo.Length > mArrayItem.Length ? mArrayItem.Length : ArrayStrInfo.Length;
- for (int i = 0; i < nMinLenth; i++)
- {
- UnityEngine.UI.Text UiText = mArrayItem[i].GetComponentInChildren<UnityEngine.UI.Text>();
- if (null != UiText)
- {
- UiText.text = ArrayStrInfo[i];
- }
- }
- }
- public void RunMove()
- {
- bIsRun = true;
- }
- public void StopMove()
- {
- bIsRun = false;
- }
- public void SetChangeInfoCb(LuaAutoMoveCallback cb)
- {
- mLuaOnSelectedCallback = cb;
- }
- private void Move()
- {
- if (null != mArrayItem)
- for(int i=0;i< mArrayItem.Length;i++)
- {
- RectTransform RectTrans = mArrayItem[i].GetComponentInChildren<RectTransform>();
- Vector2 v2MovePos = RectTrans.anchoredPosition;
- v2MovePos.y += fMoveSpeed * Time.deltaTime;
- RectTrans.anchoredPosition = v2MovePos;
- }
- Refresh();
- }
- private void Refresh()
- {
- for (int i = 0; i < mArrayItem.Length; i++)
- {
- RectTransform RectTrans = mArrayItem[i].GetComponentInChildren<RectTransform>();
- Vector2 v2MovePos = RectTrans.anchoredPosition;
- if(v2MovePos.y > fHeight * 0.5f)
- {
- ChangeInfo(i);
- }
- }
- }
- private void ChangeInfo(int nIndex)
- {
- if(null != mArrayItem)
- if (mArrayItem.Length > nIndex)
- {
- //set Pos
- int nNext = GetNext(nIndex);
- //Debug.Log("Change"+nIndex.ToString());
- //Debug.Log("nNext" + nNext.ToString());
- RectTransform RectNextTrans = mArrayItem[nNext].GetComponentInChildren<RectTransform>();
- RectTransform RectTrans = mArrayItem[nIndex].GetComponentInChildren<RectTransform>();
- Vector2 v2MovePos = RectTrans.anchoredPosition;
- float fOffset = fHeight;
- v2MovePos.y = RectNextTrans.anchoredPosition.y - fOffset;
- RectTrans.anchoredPosition = v2MovePos;
-
- UnityEngine.UI.Text UiText = mArrayItem[nIndex].GetComponentInChildren<UnityEngine.UI.Text>();
- if (mLuaOnSelectedCallback != null)
- {
- mLuaOnSelectedCallback.DynamicInvoke(UiText);
- }
- }
- }
- private int GetNext(int nIndex)
- {
- nIndex += 1;
- int nNext = ((mArrayItem.Length - 1) + nIndex) % mArrayItem.Length;
- nNext -= 1;
- if (nNext < 0)
- nNext = mArrayItem.Length - 1;
- return nNext;
- }
- }
|