DontDestroyOnReoad : Scene이 바뀌어도 제거되지 않음
! Scene 만들면 가장 먼저 Scene이름의 Empty, Script 만들기 !
lifetime
<Awake - Init - Start - Update>
transform.SetParent(transform)
Camera.main.orthographicSize -> bullet.transform.position.y가 카메라 사이즈를 넘어가면 사라지게 활용
App
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class App : MonoBehaviour
{
private string version = "Version 0.0.1";
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
void Start()
{
AsyncOperation oper = SceneManager.LoadSceneAsync("TitleScene");
oper.completed += (obj) => {
TitleMain titlemain = GameObject.FindObjectOfType<TitleMain>();
titlemain.Init(this.version);
};
}
}
Title Main
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TitleMain : MonoBehaviour
{
public Text txtVersion;
public void Init(string message)
{
this.txtVersion.text = message;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene("LobbyScene");
}
}
}
LobbyMain
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LobbyMain : MonoBehaviour
{
public Button btnStart;
public Button btnGun1; //기본
public Button btnGun2; //멀티
public Text textSelectedGun;
private GameEnums. eGunType selectedGunType;
public GameEnums.eGunType SelectedGunType
{
set
{
this.selectedGunType = value;
this.textSelectedGun.text = string.Format("{0}을 선택했습니다. ", this.selectedGunType);
}
}
void Start()
{
this.SelectedGunType = GameEnums.eGunType.Default;
this.btnStart.onClick.AddListener(() =>
{
Debug.Log("게임 시작;");
//선택된 건의 타입 출력
AsyncOperation oper = SceneManager.LoadSceneAsync("GameScene");
oper.completed += (obj) =>
{
//Game씬일 로드됨
GameMain gameMain = GameObject.FindObjectOfType<GameMain>();
gameMain.Init(selectedGunType);
};
});
this.btnGun1.onClick.AddListener(() =>
{
Debug.Log("기본 Gun을 선택했습니다.");
this.SelectedGunType = GameEnums.eGunType.Default;
});
this.btnGun2.onClick.AddListener(() =>
{
Debug.Log("MultiGun을 선택했습니다.");
this.SelectedGunType = GameEnums.eGunType.Multiple;
});
}
}
GameMain
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameMain : MonoBehaviour
{
public GameObject[] gunPrefabs;
public PlayerController player;
//Awake - Init - Start - Update
public void Init(GameEnums.eGunType selectedGunType)
{
//총을 만든다
Gun gun =this.CreateGun(selectedGunType);
//플레이어에게 총을 지급한다
this.player.Init(gun);
}
private Gun CreateGun(GameEnums.eGunType gunType)
{
int index = (int)gunType;
GameObject prefab = this.gunPrefabs[index];
GameObject go = Instantiate(prefab);
Gun gun = go.GetComponent<Gun>();
return gun;
}
}
GameEnums
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameEnums : MonoBehaviour
{
public enum eGunType
{
Default,
Multiple
}
}
PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject bulletPrefab;
public GameObject bullet2Prefab;
public float speed = 1f;
public Gun gun;
public void Init(Gun gun)
{
this.gun = gun;
this.gun.transform.SetParent(this.transform);
this.gun.transform.localPosition = Vector3.zero;
}
void Start()
{
}
void Update()
{
float h = Input.GetAxisRaw("Horizontal"); //-1, 0, 1
float v = Input.GetAxisRaw("Vertical");
Vector3 dir = new Vector3(h, v, 0);
this.transform.Translate(dir.normalized * speed * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
this.Shoot();
}
}
private void Shoot()
{
//내가 가지고 있는 Gun이 총알을 발사
this.gun.Shoot();
}
}
PlayerBullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBullet : MonoBehaviour
{
public float speed;
private float cameraSize;
private void Start()
{
cameraSize = Camera.main.orthographicSize;
}
void Update()
{
this.transform.Translate(this.transform.up * this.speed * Time.deltaTime);
if (this.transform.position.y >= cameraSize)
{
Destroy(this.gameObject);
}
}
}
Gun
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
public Transform firePoint1;
public GameObject bulletPrefab;
public virtual void Shoot()
{
//총알 생성
GameObject go = Instantiate(this.bulletPrefab);
//위치 설정
go.transform.position = this.firePoint1.position;
}
}
MultiGun
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MultiGun : Gun
{
[Tooltip("right fire point")]
public Transform firePoint2; //right
public override void Shoot()
{
//총알 생성
GameObject go1 = Instantiate(this.bulletPrefab); //left
GameObject go2 = Instantiate(this.bulletPrefab); //right
//위치 설정
go1.transform.position = this.firePoint1.position;
go2.transform.position = this.firePoint2.position;
}
}
'Unity > 수업내용' 카테고리의 다른 글
[Unity UI] 1일차 Menu (0) | 2023.02.06 |
---|---|
[Unity UI] 1일차 Button (0) | 2023.02.06 |
[Unity 3D / 유니티 교과서] 7장 예제 Bamsongi (0) | 2023.02.01 |
[Unity 2D / 유니티 교과서] 6장 예제 ClimbCloud (0) | 2023.02.01 |
[Unity 2D / 유니티 교과서] 5장 예제 (0) | 2023.01.31 |