Unity将图像从0无限移动到1920以产生视差效果

时间:2019-05-25 11:51:03

标签: unity3d parallax

我有5张与此asset pack不同的图片。

我想在开始屏幕中将它们用作背景。我只希望它们无限移动(视差样式)。

它们是图像而不是精灵,因此它们在画布元素上呈现。

我现在很难理解如何将它们从0移到1920,因此它们不会离开相机。我可以移动它们,但是它们会移出相机。

下面是一个示例,说明如何使用协程移动一层:

public IEnumerator PlayParallax() {
        while (true) {
            imageContainer.GetComponentInChildren<Image>().transform.position = new Vector3(imageContainer.GetComponentInChildren<Image>().transform.position.x + 1, imageContainer.GetComponentInChildren<Image>().transform.position.y, imageContainer.GetComponentInChildren<Image>().transform.position.z);
            yield return new WaitForSeconds(0.1f);
        }   
    }

1 个答案:

答案 0 :(得分:0)

我已经解决了,方法是制作每层的副本。例如,layer_0在相机视图中可见,layer_0_0在左侧。两者都移动,并且当layer_0完全离开相机时,将其放置在开头,依此类推:

public class ParalaxPlayer : MonoBehaviour {

    [SerializeField] GameObject imageContainer;

    public IEnumerator PlayParallax() {
        while (true) {
            // first layer
            imageContainer.GetComponentsInChildren<Image>()[0].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[0].transform.position.x + 0.3f, 
                imageContainer.GetComponentsInChildren<Image>()[0].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[0].transform.position.z);
            imageContainer.GetComponentsInChildren<Image>()[1].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[1].transform.position.x + 0.3f, 
                imageContainer.GetComponentsInChildren<Image>()[1].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[1].transform.position.z);

            if (imageContainer.GetComponentsInChildren<Image>()[0].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[0].transform.position = new Vector3(-1920, 0, 0);
            }

            if (imageContainer.GetComponentsInChildren<Image>()[1].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[1].transform.position = new Vector3(-1920, 0, 0);
            }

            // seconds layer
            imageContainer.GetComponentsInChildren<Image>()[2].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[2].transform.position.x + 0.7f,
                imageContainer.GetComponentsInChildren<Image>()[2].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[2].transform.position.z);
            imageContainer.GetComponentsInChildren<Image>()[3].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[3].transform.position.x + 0.7f, 
                imageContainer.GetComponentsInChildren<Image>()[3].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[3].transform.position.z);

            if (imageContainer.GetComponentsInChildren<Image>()[2].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[2].transform.position = new Vector3(-1920, 0, 0);
            }

            if (imageContainer.GetComponentsInChildren<Image>()[3].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[3].transform.position = new Vector3(-1920, 0, 0);
            }

            // third layer
            imageContainer.GetComponentsInChildren<Image>()[4].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[4].transform.position.x + 1.1f,
                imageContainer.GetComponentsInChildren<Image>()[4].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[4].transform.position.z);
            imageContainer.GetComponentsInChildren<Image>()[5].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[5].transform.position.x + 1.1f,
                imageContainer.GetComponentsInChildren<Image>()[5].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[5].transform.position.z);

            if (imageContainer.GetComponentsInChildren<Image>()[4].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[4].transform.position = new Vector3(-1920, 0, 0);
            }

            if (imageContainer.GetComponentsInChildren<Image>()[5].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[5].transform.position = new Vector3(-1920, 0, 0);
            }

            // fourth layer
            imageContainer.GetComponentsInChildren<Image>()[6].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[6].transform.position.x + 1.5f,
                imageContainer.GetComponentsInChildren<Image>()[6].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[6].transform.position.z);
            imageContainer.GetComponentsInChildren<Image>()[7].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[7].transform.position.x + 1.5f,
                imageContainer.GetComponentsInChildren<Image>()[7].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[7].transform.position.z);

            if (imageContainer.GetComponentsInChildren<Image>()[6].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[6].transform.position = new Vector3(-1920, 0, 0);
            }

            if (imageContainer.GetComponentsInChildren<Image>()[7].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[7].transform.position = new Vector3(-1920, 0, 0);
            }

            // fifth layer
            imageContainer.GetComponentsInChildren<Image>()[8].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[8].transform.position.x + 1.9f,
                imageContainer.GetComponentsInChildren<Image>()[8].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[8].transform.position.z);
            imageContainer.GetComponentsInChildren<Image>()[9].transform.position = new Vector3(imageContainer.GetComponentsInChildren<Image>()[9].transform.position.x + 1.9f,
                imageContainer.GetComponentsInChildren<Image>()[9].transform.position.y, imageContainer.GetComponentsInChildren<Image>()[9].transform.position.z);

            if (imageContainer.GetComponentsInChildren<Image>()[8].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[8].transform.position = new Vector3(-1920, 0, 0);
            }

            if (imageContainer.GetComponentsInChildren<Image>()[9].transform.position.x >= 1920) {
                imageContainer.GetComponentsInChildren<Image>()[9].transform.position = new Vector3(-1920, 0, 0);
            }

            yield return new WaitForSeconds(0.005f);
        }   
    }

    void Start() {
        StartCoroutine(PlayParallax());
    }

    void Update() {

    }
}