발단 : 당일 생일인 친구한테 색다른 편지를 보내고 싶은 생각
전개 : 시간이 없으니 아주 간단하게 dialog system을 구현
위기 : 오브젝트를 못 찾는 문제 발생
절정 : 문제 해결, 좀 더 편리한 입력을 위해 스크립트 수정, 프로젝트 빌드, github 올리기
결말 : github 통해 친구에게 공유 (모든 것이 어색한 작업이었지만 끝마친 첫 번째 작업)
구현하고 싶었던 부분
- 버튼을 눌렀을 때, 누른 버튼은 사라지고 dialogue 창 띄우기
- 애플리케이션 종료하는 버튼 만들기
불편했던 부분
- 인스펙터 창에서 text(편지글)를 입력할 때 '줄바꿈'을 못하고 길게 쓰면 내용이 보이지 않음
(개선)
나중에 좀 더 추가한 부분
- 페이지 넘김 화살표
- UI Object 관리를 위한 panel 사용
(문제) 비활성화된 오브젝트를 다시 활성화 시키는데 오류 발생
▶ NullReferenceException: Object reference not set to an instance of an object


▶ Button 관련 스크립트
public class DialogueTrigger : MonoBehaviour
{
public void StartPlaying()
{
FindObjectOfType<Dialogue>().StartDialogue();
}
public void IsFade()
{
gameObject.SetActive(false);
}
public void ShutdownGame()
{
Application.Quit();
}
}
▶ Dialogue 출력 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Dialogue : MonoBehaviour
{
public TextMeshProUGUI textComponent;
[TextArea]
public string[] lines;
public float textSpeed;
private int index;
WaitForSeconds wait;
void Start()
{
gameObject.SetActive(false);
textComponent.text = string.Empty;
}
...
public void StartDialogue()
{
index = 0;
wait = new WaitForSeconds(textSpeed);
gameObject.SetActive(true); // !! 문제의 부분 !!
StartCoroutine(TypeLine()); // text 출력하는 Coroutine
}
...
}
(해결) 부모 오브젝트를 찾아서 자식 오브젝트 활성화 ### 참고 https://artiper.tistory.com/114
▶ Dialogue Trigger 스크립트
public class DialogueTrigger : MonoBehaviour
{
public void StartPlaying()
{
GameObject.Find("Canvas").transform.GetChild(0).gameObject.SetActive(true); // 해결
FindObjectOfType<Dialogue>().StartDialogue();
}
public void IsFade()
{
gameObject.SetActive(false);
}
public void ShutdownGame()
{
Application.Quit();
}
}
▶ Dialogue 출력 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Dialogue : MonoBehaviour
{
public TextMeshProUGUI textComponent;
[TextArea]
public string[] lines;
public float textSpeed;
private int index;
WaitForSeconds wait;
void Start()
{
gameObject.SetActive(false);
textComponent.text = string.Empty;
}
...
public void StartDialogue()
{
index = 0;
wait = new WaitForSeconds(textSpeed);
// gameObject.SetActive(true); !! 문제의 부분 !!
StartCoroutine(TypeLine()); // text 출력하는 Coroutine
}
...
}
개선한 부분
- panel 사용
- 페이지 넘김 화살표(Arrow) 추가
- TextArea

=> DialoguePanel로 DialogueBox와 넘김 표시 화살표(Arrow)를 묶어 'dialogue UI'를 관리한다.
▶ 수정한 Dialogue Script
...
public class Dialogue : MonoBehaviour
{
...
[TextArea(3, 5)]
public string[] lines;
...
GameObject arrow;
void Start()
{
arrow = GameObject.Find("Arrow"); // new
this.gameObject.SetActive(false); // gameObject -> this.gameObject
arrow.SetActive(false); // new
...
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
if (textComponent.text == lines[index])
{
arrow.SetActive(false); // new
...
}
else
{
...
arrow.SetActive(true); // new
}
}
}
...
}
play 영상
동영상 서비스가 종료되어 해당 콘텐츠를 재생할 수 없습니다.
Dialogue System 구현 참고
https://www.youtube.com/watch?v=8oTYabhj248
'Unity' 카테고리의 다른 글
| Unity > Raycast (0) | 2024.03.12 |
|---|