강의, 책/[Unity] C#과 유니티로 만드는 MMORPG 게임 개발 시리즈 22

Section 12. Data - Data Manager

Assets > Resources > Data > StatData.json (유니티 상에서 추가가 안 되니 폴더로 직접 이동하여 만들어준다) { "stats": [ { "level": "1", "hp": "100", "attack": "10" }, { "level": "2", "hp": "150", "attack": "15" }, { "level": "3", "hp": "200", "attack": "20" } ] } Assets > Scripts > Data > Data.Content.cs (클래스명에 .이 들어가서 오류가 뜨지만, 클래스 사용 안 할 거기 때문에 무시) * data 파일을 어떤 포맷으로 읽어들일 것인가를 나타내기 위한 스크립트 파일 #region Stat [Serializable] p..

Section 11. Coroutine

코루틴을 사용하면 이전에 함수에서 진행 중이던 상황을 저장했다가 복원하여 그대로 이어서 수행하게 할 수 있다 * yield break; 를 하게 되면 함수를 완전히 빠져나오게 된다 // 코루틴 사용 예 StartCoroutine("CoExplodeAfterSeconds", 4.0f); IEnumerator CoExplodeAfterSeconds(float seconds) { Debug.Log("Explode Enter"); yield return new WaitForSeconds(seconds); Debug.Log("Explode Execute!"); } // 코루틴 사용 및 삭제 Coroutine co = StartCoroutine("CoExplodeAfterSeconds", 4.0f); StartCo..

Section 10. Object Pooling - Pool Manager

오브젝트를 재사용 하기 위해 pool을 사용한다(소규모 게임에서는 굳이 필요 없으나, 규모가 커지고 성능을 높여야 할 때 유용) 풀링할 객체 vs 안 할 객체 구분법 : 특정 컴포넌트의 존재 여부로 판단한다 Assets > Scripts > Managers > Poolable.cs public class Poolable : MonoBehaviour { public bool isUsing; } 객체를 구별하는 용도의 컴포넌트로 사용한다 Assets > Scripts > Managers > PoolManager.cs (Resource Manager를 보조하는 역할로 사용) public class PoolManager { class Pool { public GameObject Original { get; pr..

Section 9. Sound - Sound Manager(2)

Effect의 경우, 같은 effect를 여러 번 불러오게 되면, 불러올 때마다 manager를 호출하는 게 부하를 줄 수 있으므로 캐싱을 해둔다 public class SoundManager { Dictionary _audioClips = new Dictionary(); public void Init() { // Init } // Scene 이동 시 캐싱된 audio source&clip 초기화 public void Clear() { foreach (AudioSource audioSource in _audioSources) { audioSource.clip = null; audioSource.Stop(); } _audioClips.Clear(); } public void Play(string path,..

Section 9. Sound - Sound Manager(1)

AudioSource audio = GetComponent(); audio.PlayOneShot(audioClip); audio.PlayOneShot(audioClip2); PlayOneShot 함수: clip을 한번 재생시킨다(여러 개 겹쳐서 실행시키면 그대로 겹쳐서 재생시킴) * 만약 audio clip이 재생되고 있는 중에 해당 클립을 들고 있는 게임 오브젝트가 destroy된다면 중간에 사운드가 끊기게 된다 -> 클립을 게임 오브젝트가 들고 있는 게 아니라, manager가 관리하게 만든다 Assets > Scripts > Utils > Define.cs에 Sound 열거체를 추가해준다 public enum Sound { Bgm, Effect, MaxCount // 열거체 내 오브젝트의 개수 파악..

Section 8. Scene - Scene Manager

Assets > Scripts > Utils > Define.cs에 씬과 관련된 열거체를 추가로 정의해준다 public enum Scene { Unknown, Login, Lobby, Game } 그리고 씬을 관리하기 위한 새로운 스크립트 Assets > Scripts > Scenes > BaseScene.cs를 생성해준다 public abstract class BaseScene : MonoBehaviour { public Define.Scene SceneType { get; protected set; } = Define.Scene.Unknown; void Awake() { Init() } protected virtual void Init() { Object obj = GameObject.FindObje..

Section 7. UI - 코드 정리

1. getOrAddComponent 함수를 extension method로 변경해준다 Assets > Scripts > Utils > Extension.cs public static class Extension { public static T GetOrAddComponent(this GameObject go) where T : UnityEngine.Component { return Util.GetOrAddComponent(go); } public static void AddUIEvent(this GameObject go, Action action, Define.UIEvent type = Define.UIEvent.Click) { UI_Base.AddUIEvent(go, action, type); } }..

Section 7. UI - 인벤토리 실습

인벤토리 패널 산하에 들어가는 Inven_Item 스크립트는 UI_Popup도, UI_Panel도 아니기 때문에 UI_Base를 상속받게 되는데, 그럼 팝업과 씬에서 만들어두었던 Init 함수를 사용하지 못하기 때문에 그 부분을 수정해준다 public abstract class UI_Base : MonoBehaviour { public abstract void Init(); } UI_Base 스크립트에 이렇게 추가해준다 이때 virtual로 선언한 것과 달리, abstract로 선언한 경우, 자식 클래스에서 반드시 재선언을 해주어야 사용할 수가 있다 그리고 Ui_Popup.cs, UI_Scene.cs에서 기존에 virtual Init으로 선언했던 함수를 override Init으로 변경해주면 된다

Section 7. UI - UI Manager

UI를 팝업 UI와 고정형 UI로 구분하여 생각하기 UI > Popup > UI_Popup.cs, UI > Scene > UI_Scene.cs 스크립트를 각각 만들어주고, 이 베이스 스크립트들은 MonoBehaviour 대신 UI_Base를 상속받는다 - 팝업과 씬 스크립트를 새로 만들 때 위 베이스 스크립트를 상속 받아서 사용 - Popup, Scene 폴더 내 스크립트들은 이제 UI_Base 대신 UI_Popup, UI_Scene 을 상속받으면 됨 UIManager _ui = new UIManager(); public static UIManager UI { get { return Instance._ui; } } UIManager 스크립트를 만들어준 뒤, Manager.cs에 연결시켜준다 public ..

Section 7. UI - UI 자동화(이벤트)

Assets > Scripts > UI > UI_EventHandler.cs (EventSystem이 클릭을 감지하여 이벤트를 뿌려주면 UI에서 캐치해서 콜백 함수를 날려주는 역할, 드래그 이벤트를 감지할 오브젝트에 부착해주면 된다) public class UI_EventHandler : MonoBehaviour, IPointerClickHandler, IDragHandler { public Action OnClickHandler = null; public Action OnDragHandler = null; public void OnPointerClick(PointerEventData eventData) { if (OnClickHandler != null) OnClickHandler.Invoke(even..