본문 바로가기

유니티/유니티 문법?6

싱글톤 (점수 관리 같은거 할 때) static 변수를 선언하고 instance에 this를 해줌으로써 다른 함수에서도 편히 쓸수 있도록 해줌. 이렇게 하면 score를 public ScoreManager scoreManager해서 끌어올 필요가 없다. using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScoreManager : MonoBehaviour { public static ScoreManager instance; private int score = 0; //★핵심★ void Awake() // { instance = this; } public int GetScore() { return score; } public vo.. 2021. 4. 19.
코루틴 (대기시간 주는 것) 서서히 UI가 사라지는 Fade-In 기능을 사용하려고 아래와 같이 코드를 작성하였다. 하지만 너무 빠르게 컴퓨터가 동작해 바로 화면이 불투명해지게 변한다. 이를 해결하기 위해 함수에 대기시간을 주는 "코루틴" 이라는 함수를 이용하였다. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;//UI안에 있는 함수를 쓰기 위해 선언 public class Fade : MonoBehaviour { public Image fadeImage; void Start() { FadeIn(); } void FadeIn() { Color startColor = fadeImage.color; //처.. 2021. 4. 19.
리스트 (실시간으로 입력 받음) 배열은 수를 정해준 값에 한정해서 숫자를 입력받고 해야지만 리스트는 실시간으로 입력받는다. using System.Collections; using System.Collections.Generic; using UnityEngine; public class Score : MonoBehaviour { //public int[] score = new int[10]; //배열 public List score = new List(); //리스트 (실시간으로 방의 개수가 생김) // Update is called once per frame void Update() { if(Input.GetMouseButtonDown(0)) { int randomNumber = Random.Range(0, 100); score.Ad.. 2021. 4. 18.
인스턴스화 Spawner라는 c# 스크립트 생성 후 아래 코드를 넣어준다. public class Spawner : MonoBehaviour { public GameObject target; void Start() { Instantiate(target); } } 새로운 Empty GameObject 생성후 작성했던 c# 스크립트를 넣어주고 target에는 Sphere 3D 오브젝트를 만들고 이름을 변경 후 넣어준다. 실행시켜보면 Ball(Clone)이라는 오브젝트가 하나 더 생성된 것이 보인다. 생성될 위치를 정해주기 1.새로운 Empty GameObject 생성 후 spawnPosition에 넣어준다. (위치 회전축 임의로 설정) 2.Instantitate안에 spawnPosition의 position과 rota.. 2021. 4. 12.