UNITY/Unity Study

유니티 시민강좌 3일

GREEN나무 2023. 7. 19. 14:13
728x90

씬 구성
Inpo
Main
Play

 

씬 이동

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class UImanager : MonoBehaviour
{
    private void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    public void MoveScene(int num)
    {
        SceneManager.LoadScene(num);
    }

    public void OnClickQuit()
    {
        // Application.Quit();  // 이거는 빌드해야 나가기 가능

       // 빌드 없이 게임종료 기능함
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public GameObject pauseUI;
    public Slider progressSlider;
    public float distence,speed;

    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1;
        pauseUI.SetActive(false);
        distence = 0;
        progressSlider.value = 0;
    }

    // Update is called once per frame
    void Update()
    {
        distence += (Time.deltaTime * speed);
        progressSlider.value = distence;
    }

    // public void OnClick버튼이름(); 도 가능...
    public void OnPauseButton()
    {
        pauseUI.SetActive(true);
        Time.timeScale = 0; // 일시정지
    }

    public void OnContinueButton()
    {
        pauseUI.SetActive(false);
        Time.timeScale = 1;
    }
}

 

로딩 바 만들기(UI 슬라이더)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement; // 씬이동하려면 있어야함

public class Intro : MonoBehaviour
{
    private float loadingTime;
    public Slider loadingBar;
    public float delayTime;

    // Start is called before the first frame update
    void Start()
    {
        loadingBar.value = 0;
    }

    // Update is called once per frame
    void Update()
    {
        if (loadingBar.value >= 1)
            // 스크롤바 이동 끝나면(0~1까지 있음)
        {
            // SceneManager.LoadScene("Main"); //  씬 이름
            SceneManager.LoadScene(1); // 빌드세팅의넘버
        }
        else
        {
            loadingTime += Time.deltaTime;
            loadingBar.value = loadingTime / delayTime; 
            // delaytime만큼 로딩하기
        }
    }
}

 

Play Scene

공룡 자동 생성(여러마리 리스트로)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using Random = UnityEngine.Random;

public class spawnManager : MonoBehaviour
{
    public bool enableSpawn = true;
    public List<GameObject> enemyPrefabs; // 적 프리팹들의 리스트
    public List<GameObject> spawnedEnemies; // 생성된 적 오브젝트들을 저장할 리스트

    void SpawnEnemy()
    {
        if (enableSpawn && enemyPrefabs.Count > 0)
        {
            int randomIndex = Random.Range(0, enemyPrefabs.Count); // 리스트에서 무작위로 적 프리팹을 선택합니다.
            Vector3 spawnPosition = new Vector3(8.5f, 0.56f, Random.Range(-1.5f, 1.5f));
            Quaternion spawnRotation = Quaternion.Euler(0, 90, 0); // y축으로 90도 회전한 Quaternion

            GameObject enemy = Instantiate(enemyPrefabs[randomIndex], spawnPosition, spawnRotation);
            spawnedEnemies.Add(enemy); // 생성된 적을 리스트에 추가합니다.
        }
    }

    void Start()
    {
        spawnedEnemies = new List<GameObject>();
        InvokeRepeating("SpawnEnemy", 1, 2); // 1초 후부터, 2초마다 SpawnEnemy 함수를 반복해서 실행합니다.
        // 다음 시간에는 단계별로 빨라지게 만들기
    }


	// 이게 안먹어서 위에 InvokeRepeating 사용
    void OnBecameInvisible() //화면 밖으로 나가 보이지 않게 되면 호출이 된다.
    {
        Destroy(this.gameObject); //객체를 삭제한다.
    }
}