- 添加背景图片,当然也可以把背景资源放到空物体里。然后复制出来一份;
- 然后将两份背景资源拖到一个空物体里
- 添加脚本
说明:首先获取两个背景的位置分别存储。移动过程中,当后一个背景的X超过前一个背景开始存储的X。 则将前一个背景位置设为后一个背景开始的位置。然后交换,前变后,后变前。进入下一轮循环。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BGxunhuan : MonoBehaviour
{
public int speed = 4;
public Vector3 flag1,flag2;
public GameObject bg1;
public GameObject bg2;
public GameObject swap;
// Start is called before the first frame update
void Start()
{
start = bg1.transform.position;
xunhuan = bg2.transform.position;
}
void Update()
{
transform.Translate(Vector3.left * Time.deltaTime * speed, Space.World);
if (bg2.transform.position.x < start.x)
{
bg1.transform.position = xunhuan;
swap = bg1;
bg1 = bg2;
bg2 = swap;
}
}
}
动图展示:
录完屏转格式就花了。右上角有标记,至少循环了3次。