天天看點

unity中實作背景滾動

讓圖檔上下/左右滾動

1.導入一張需要滾動的背景,将Texture Type設定為Defauit

unity中實作背景滾動

2.建立一個材質Material将Shader設定為Unit/Texture,并将上面設定好的背景圖檔拖入

unity中實作背景滾動

3.建立一個3D物體Quad,設定款高比例和背景圖檔相同,将上面設定好的Material拖入Quad,此時控制Offset參數就可以實作背景滾動效果。

unity中實作背景滾動

4.給背景Quad上Scripts

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BackgroundScroll : MonoBehaviour
{
    [SerializeField]
    Vector2 ScorllSpeed;

    private Material BackgroundMaterial;
    void Start()
    {
        BackgroundMaterial = GetComponent<Renderer>().material;
    }

    void Update()
    {
        BackgroundMaterial.mainTextureOffset += ScorllSpeed * Time.deltaTime;
    }
}
           

5.運作如果沒有反應調整代碼參數X和Y的方向速度。

unity中實作背景滾動