개발 일지 [3D | Dream Forest]
2024-05-31 | 3D에서 2D 스프라이트 화면에 맞게 나타내기
moonee-e3
2024. 6. 22. 14:06
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class StartStoryPlay : MonoBehaviour
{
//참고 링크 : https://youtu.be/icYDYFFBVCc?si=x6FVV7yZNFtz5KgR
private void Awake() // 카메라에 오브젝트 크기 맞추기
{
float spritex = SR.sprite.bounds.size.x;
float spritey = SR.sprite.bounds.size.y;
float screenY = Camera.main.orthographicSize * 2;
float screenX = screenY / Screen.height * Screen.width;
transform.localScale = new Vector3(Mathf.Ceil(screenX / spritex), Mathf.Ceil(screenY / spritey));
}
public SpriteRenderer SR;
public Sprite[] IntroScene; // 씬 넣을 배열
int index = 0;
public Sprite Intro3;
public Sprite Intro6;
private void Start()
{
index = 0;
SR = GetComponent<SpriteRenderer>();
}
private void Update()
{
SceneStart();
}
void SceneStart()
{
if (Input.GetKeyDown(KeyCode.Space))
{
index++; // index가 점점 늘어남
//Debug.Log(IntroScene.Length + "///////////" + index);
SR.sprite = IntroScene[index]; //image의 sprite를 불러와서 index 순으로 함
if (index == 1 || index == 3)
{
StartCoroutine(AnimaIntro());
}
if (IntroScene.Length <= index + 1) //sprite의 길이가 index의 수만큼
{
SceneManager.LoadScene("StartCinema");
}
}
}
IEnumerator AnimaIntro()
{
while (index == 1)
{
SR.sprite = IntroScene[1];
yield return new WaitForSeconds(1f);
if(index == 1) // 한 번더 확인 해줌으로써 코드가 꼬이지 않게 함
{
SR.sprite = Intro3;
yield return new WaitForSeconds(1f);
}
else
{
break;
}
}
while (index == 3)
{
SR.sprite = IntroScene[3];
yield return new WaitForSeconds(1f);
if (index == 3)
{
SR.sprite = Intro6;
yield return new WaitForSeconds(1f);
}
else
{
break;
}
}
yield return null;
}
}