2d场景背景无限滚动
之前都是直接借用的DoTween插件,两个背景无限交替位置进行,还有就是三个背景在利用Trigger进行判断显示与否循环;
示例脚本:
private List<RectTransform> m_ScrollGroup; private RectTransform[] _ts; void Awake() { m_ScrollGroup = new List<RectTransform>(); _ts = transform.GetComponentsInChildren<RectTransform>(); foreach(RectTransform t in _ts) { if(t.name.StartsWith("Background")) { m_ScrollGroup.Add(t); } } //Debug.Log(m_ScrollGroup.Count + ":" + m_ScrollGroup[0]);//2 object } private Vector2 m_StartPos = new Vector2(788, 0); private int m_Speed = 50; void Update () { PosLimit(); m_ScrollGroup[0].transform.Translate(Vector3.left * Time.deltaTime * m_Speed); m_ScrollGroup[1].transform.Translate(Vector3.left * Time.deltaTime * m_Speed); //m_ScrollGroup[0].position = Vector2.Lerp(new Vector2(-11, 0), new Vector2(-800, 0), 60); } public void PosLimit() { //width 800 height600 if (m_ScrollGroup[0].position.x <= -400f) { m_ScrollGroup[0].position = new Vector2(1198f, m_ScrollGroup[0].position.y); } if (m_ScrollGroup[1].position.x <= -400f) { m_ScrollGroup[1].position = new Vector2(1198f, m_ScrollGroup[1].position.y); } }
不积跬步,无以至千里;不积小流,无以成江海。