総合カタログhttps://blog.csdn.net/qq_54263076/category_11900070.html?spm=1001.2014.3001.5482
レンダリングは次のとおりです。
1. まず、必要な空のオブジェクトを背景の空のオブジェクトの下にドラッグし、スプライト レンダラーを追加して、その中に画像を配置します (背景の空のオブジェクト サブオブジェクトとしてスプライト イメージを直接ドラッグすることもできます。レンダラーは自動的に追加されます)。 )
各背景のカバー関係の調整に注意
一部のレイヤーが順番に並べ替えられています
これらのサブオブジェクトの描画モードをタイルに変更する必要があることに注意してください。これにより、幅を変更した後、次の画像が自動的に貼り付けられます。幅は元の幅の 3 倍以上に設定する必要があります (そうしないと、スクリプトが後で完了したときに、キャラクターが背景を超えて移動したときに動きが瞬時に埋められ、幅が小さすぎると、動きは画面上で直接点滅し、画面外の背景を更新する必要があります)
2. 各子オブジェクトに BgkControl スクリプトを追加します
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BgkControl : MonoBehaviour
{
//摄像机
private Transform cameraT;//前一帧摄像机位置
private Vector3 lastCameraPoi;//后一帧摄像机位置
//子物体移动倍速
public Vector2 SpeedTime = new Vector2(1f,1f);
//纹理图宽度
private float textUnitSizex;//textUnitSizex
// Start is called before the first frame update
void Start()
{
cameraT = Camera.main.transform;
lastCameraPoi = cameraT.position;
//获取纹理图及宽度
Sprite sprite = transform.GetComponent<SpriteRenderer>().sprite;
Texture2D texture = sprite.texture;
textUnitSizex = texture.width / sprite.pixelsPerUnit;//纹理图宽度除像素宽度
}
// Update is called once per frame
void Update()
{
Vector3 delMovement = cameraT.position - lastCameraPoi;
transform. position += new Vector3(delMovement.x * SpeedTime.x,delMovement.y* SpeedTime.y,0);
lastCameraPoi = cameraT.position;//更新后一帧摄像机位置
//背景图无限
if (Mathf.Abs(cameraT.position.x - transform.position.x )> textUnitSizex)
{
//偏移量
float offsetPositionX = (cameraT.position.x - transform.position.x)%textUnitSizex;
//移动背景图
transform.position = new Vector3(cameraT.position.x+ offsetPositionX, transform.position.y);
}
}
}
各オブジェクトに小さな本を追加した後、Unity の各オブジェクトの下のスクリプトで、独自の x と y の速度に変更できます。一般的に言えば、遠くのオブジェクトの x 速度は 0 ~ 0.2 (雲、霧)、中間オブジェクトの x 速度は 0.2 ~ 0.4 (山)、近くのオブジェクトの x 速度は 0.4 ~ 0.6 (木) です。前景 オブジェクト (キャラクターを遮る背景) はカメラと反対方向に移動し、最も近い x 速度は -1.5 から -2 の間です。y 速度が遠いほどオブジェクトは大きくなり、オブジェクトが近づくほど小さくなり、ジャンプした後に背後のオブジェクトが現れる効果を形成できます (前景は負の値です)。
これで完了です。! !
次