입력 글자 수 제한 InputField.characterLimit
삭제하지 않고 넘긴 이름 필드가 남는 문제를 다시 돌아올 때, 캔버스 인스턴스를 삭제해 수정
- 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;
using UnityEngine.UIElements;
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;
public GameObject restart;
GoRoulletScene roullet;
bool isTrigger = false;
private void Start()
{
roullet = FindObjectOfType<GoRoulletScene>(); // 씬 변경 시, 파괴되지 않은 오브젝트라 찾아서 변수에 넣음 // 다시 돌아갈 때 삭제해야 함
}
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; // 니들 콜라이더2D 활성화
}
}
if (isTrigger == true)
{
Time.timeScale = 0f; // 룰렛 돌리기 작동 멈춤
restart.SetActive(true); // 탭 키 눌러 재시작 게임오브젝트 출력
if (Input.GetKeyDown(KeyCode.Tab))
{
Time.timeScale = 1; // 재시작 시 룰렛 돌리기 작동
Destroy(this.roullet.gameObject); // 인스턴스로 생성된 캔버스 게임 오브젝트 삭제
// Debug.Log("탭탭");
SceneManager.LoadScene("RoulletName");
}
}
}
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);
}
isTrigger = true; // 트리거를 많이 쓰면 정확하고 깔끔해지더라..
}
}
- 이름 입력 씬
- 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;
int maxTLimite = 6;
public void Start()
{
InputText.characterLimit = maxTLimite; // 인풋 글자 수 제한
OutputText.text = "";
// 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) && SceneManager.GetActiveScene().name == "roulletName")
{
// 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);
}
}
처음으로 스스로 납득할만한 버그 없이 작동하는 프로그램 제작 완료.
InputField.characterLimit
DontDestroyOnLoad(gameObject);
string.IsNullOrEmpty(OutputText.text)
SceneManager.GetActiveScene().name == "Name"
GetComponent<Collider2D>().enabled = true;
collision.name == "name"
을 처음 써봤다.
정확도를 높이기 위해 bool을 더 많이 써야겠다.
어느 게임 오브젝트에 스크립트가 들어있는지 햇갈렸다. 다음엔 UI컨트롤이나 메인 컨트롤 등을 따로 제작해서 잘 관리해야겠다.