Add travel map flow and update docs
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
|
||||
// 왼쪽 끝으로 이동한 배경을 오른쪽 끝으로 재배치하는 스크립트
|
||||
[ExecuteAlways]
|
||||
public class BackgroundLoop : MonoBehaviour {
|
||||
public Sprite[] backgroundSprites; // 순서대로 교체할 공항 배경 스프라이트
|
||||
public int startSpriteIndex = 0; // 이 배경 오브젝트가 시작할 스프라이트 번호
|
||||
@@ -9,19 +10,26 @@ public class BackgroundLoop : MonoBehaviour {
|
||||
private float width; // 배경의 가로 길이
|
||||
private int currentSpriteIndex; // 현재 표시 중인 스프라이트 번호
|
||||
private SpriteRenderer spriteRenderer; // 배경을 표시하는 스프라이트 렌더러
|
||||
private BoxCollider2D backgroundCollider; // 루프 폭 계산용 콜라이더
|
||||
|
||||
private void Awake() {
|
||||
// 가로 길이를 측정하는 처리
|
||||
InitializeBackground();
|
||||
}
|
||||
|
||||
BoxCollider2D backgroundCollider = GetComponent<BoxCollider2D>();
|
||||
width = backgroundCollider.size.x;
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
private void OnEnable() {
|
||||
InitializeBackground();
|
||||
}
|
||||
|
||||
currentSpriteIndex = NormalizeSpriteIndex(startSpriteIndex);
|
||||
ApplyBackgroundSprite(currentSpriteIndex);
|
||||
private void OnValidate() {
|
||||
InitializeBackground();
|
||||
}
|
||||
|
||||
private void Update() {
|
||||
if(!Application.isPlaying)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 현재 위치가 원점에서 왼쪽으로 width 이상 이동했을때 위치를 리셋
|
||||
if(transform.position.x <= -width)
|
||||
{
|
||||
@@ -29,6 +37,55 @@ public class BackgroundLoop : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeBackground() {
|
||||
CacheComponents();
|
||||
|
||||
currentSpriteIndex = NormalizeSpriteIndex(startSpriteIndex);
|
||||
ApplyBackgroundSprite(currentSpriteIndex);
|
||||
SyncColliderSize();
|
||||
RefreshWidth();
|
||||
}
|
||||
|
||||
private void CacheComponents() {
|
||||
if(spriteRenderer == null)
|
||||
{
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
}
|
||||
|
||||
if(backgroundCollider == null)
|
||||
{
|
||||
backgroundCollider = GetComponent<BoxCollider2D>();
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshWidth() {
|
||||
if(spriteRenderer != null && spriteRenderer.sprite != null)
|
||||
{
|
||||
width = spriteRenderer.bounds.size.x;
|
||||
return;
|
||||
}
|
||||
|
||||
if(backgroundCollider != null)
|
||||
{
|
||||
width = backgroundCollider.size.x * Mathf.Abs(transform.lossyScale.x);
|
||||
return;
|
||||
}
|
||||
|
||||
width = 0f;
|
||||
}
|
||||
|
||||
private void SyncColliderSize() {
|
||||
if(spriteRenderer == null || spriteRenderer.sprite == null || backgroundCollider == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 spriteSize = spriteRenderer.sprite.bounds.size;
|
||||
backgroundCollider.size = spriteSize;
|
||||
backgroundCollider.offset = Vector2.zero;
|
||||
backgroundCollider.isTrigger = true;
|
||||
}
|
||||
|
||||
// 위치를 리셋하는 메서드
|
||||
private void Reposition() {
|
||||
Vector2 offset = new Vector2(width * 2f, 0);
|
||||
@@ -36,6 +93,8 @@ public class BackgroundLoop : MonoBehaviour {
|
||||
|
||||
currentSpriteIndex = NormalizeSpriteIndex(currentSpriteIndex + spriteAdvanceOnLoop);
|
||||
ApplyBackgroundSprite(currentSpriteIndex);
|
||||
SyncColliderSize();
|
||||
RefreshWidth();
|
||||
}
|
||||
|
||||
private int NormalizeSpriteIndex(int index) {
|
||||
@@ -64,7 +123,9 @@ public class BackgroundLoop : MonoBehaviour {
|
||||
|
||||
if(backgroundSprite != null)
|
||||
{
|
||||
spriteRenderer.enabled = true;
|
||||
spriteRenderer.sprite = backgroundSprite;
|
||||
spriteRenderer.color = Color.white;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user