앱 만들기/유니티2D

유니티-화면 전환하기

나도 처음이야 2021. 6. 19.

유니티에서 화면을 전환하는 주요한 스크립트는 하기와 같다.

using UnityEngine.SceneManagement;

SceneManager.LoadScene(<씬 이름>);

 

물론 게임 내에서 여러 이벤트에 따라 씬이 전환되겠지만, 오늘은 마우스 터치시 전환되도록 해보자.

그러기 위해서는 OnMouseDown()에 전환코드를 적용하면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class OnMouseDown_SwitchScene : MonoBehaviour
{
 
    public string sceneName;

// 마우스 터치시
    void OnMouseDown()
    {
//씬을 전환하자.
        SceneManager.LoadScene(sceneName);
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
 
 
}
 
cs

//씬을 전환하자.

SceneManager.LoadScene(sceneName);

sceneName 은 Inspector View 에서 설정을 해주면 된다.

이 코드는 여러 화면의 각기 다른 오브젝트에도 동일하게 적용이 가능하다.

 

적용된 예시)

 

반응형

댓글