씬이 넘어가지 않는 건 일반 매서드라서 실행되자마자 클릭을 받을 새 없이 넘어간 거였다.
클릭을 받았는지 아닌지 감지하려면 업데이트 매서드나 코루틴을 사용해야 한다.
string.IsNullOrEmpty(OutputText.text) : 빈 문자열인지 확인 가능
인풋필드가 채워지면~ 씬 전환이 가능하게 함
- Roullet
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Roullet : MonoBehaviour
{
float rotSpeed;
float delta = 0;
public bool isStop = false; // 멈추면 트루로 변경해서 니들이 받음
void Update()
{
// 마우스를 클릭하면 지정 속도로 룰렛이 돈다.
if(Input.GetMouseButtonUp(0))
{
this.rotSpeed = Random.Range(5000, 50001); // 사이의 수를 돌리는 속도로 함
Debug.Log(rotSpeed);
delta += Time.deltaTime;
}
// Debug.Log(delta);
transform.Rotate(0, 0, rotSpeed);
// 속도를 줄인다.
this.rotSpeed *= 0.96f;
// Debug.Log(this.rotSpeed);
if (this.rotSpeed < 2 && delta !=0)
{
this.rotSpeed = 0f;
isStop = true;
// Debug.Log($"<color=yellow>멈춤</color>");
}
}
}
- needle
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
public class needle : MonoBehaviour
{
public GameObject Roullet;
float ldelTime = 0;
public GameObject Good;
public GameObject Bad;
public GameObject Great;
public GameObject GBad;
public GameObject So;
public GameObject CARE;
private void Update()
{
if((Roullet.GetComponent<Roullet>().isStop == true))
{
// Debug.Log($"<color=blue>왔어</color>");
ldelTime += Time.deltaTime;
if (ldelTime > 0.2)
{
// Debug.Log("1초");
// 니들 콜라이더 활성화
this.GetComponent<Collider2D>().enabled = true;
}
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.name == "bedcoll")
{
Debug.Log("운수 나쁨");
Bad.SetActive(true);
}
if (collision.name == "Goodcoll")
{
Debug.Log("운수대통");
Great.SetActive(true);
}
if (collision.name == "Bedcolll")
{
Debug.Log("운수 매우 나쁨");
GBad.SetActive(true);
}
if (collision.name == "socoll")
{
Debug.Log("운수 보통");
So.SetActive(true);
}
if (collision.name == "bbedcoll")
{
Debug.Log("운수 조심");
CARE.SetActive(true);
}
if (collision.name == "goodcoll")
{
Debug.Log("운수 좋음");
Great.SetActive(true);
}
this.GameOver();
}
void GameOver()
{
Time.timeScale = 0f; // 작동 멈춤
// Debug.Log("게임오버");
//if (Input.GetMouseButtonDown(1))
//{
//// Debug.Log("탭탭");
// SceneManager.LoadScene("RoulletName");
//}
}
}
- GoRoulletScene
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
using UnityEngine.UI;
public class GoRoulletScene : MonoBehaviour
{
public Button Button;
public TMP_InputField InputText;
public TMP_Text OutputText;
public GameObject clickTXT;
public void Start()
{
// Debug.Log($"1출력: {OutputText.text} 입력: {InputText.text}"); // 텍스트를 적어놓지 마 ㅋ 입력됐다고 하잖아 ㅋㅋ
this.Button.onClick.AddListener(() =>
{
OutputText.text = InputText.text;
InputText.text = "";
// Debug.Log($"2출력: {OutputText.text} 입력: {InputText.text}");
clickTXT.GetComponent<clickTXT>().FadeStart();
});
}
void Update() // 그냥 매서드로 만들면 바로 실행해서 클릭을 감지할 시간이 없음 ㅠ
{
// OutputText가 비어 있지 않고, 마우스 오른쪽 클릭이 감지될 때
if (!string.IsNullOrEmpty(OutputText.text) && Input.GetMouseButtonUp(1))
{
Debug.Log("이동");
SceneManager.LoadScene("SampleScene");
DontDestroyOnLoad(gameObject); // 아웃풋 캔버스를 유지함
}
}
}
- clickTXT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class clickTXT : MonoBehaviour
{
TMP_Text tmpTxt;
public GameObject right;
private void Start()
{
tmpTxt = GetComponent<TMP_Text>();
}
public void FadeStart()
{
// Debug.Log("오네");
right.SetActive(true);
}
}