[Unity] 유용한 정보 모음 (기능 / 스크립트 구분) - Ver. 2019.11.24
유니티 개발 하면서 확인한 유용한 정보 모음입니다.
클릭하시면 해당 정보로 이동합니다.
------------Unity 2019.2.9f1 Ver.
기능
충돌 Trigger | Animation (Animator) | Gradient (연속 색깔 변화) | 광고 추가 (ADMOB) |
스크립트
Update 종류, 구분 | 유니티 배열 선언 (Array) | 특수 기호 사용 방법 | ★Coroutine |
모바일 뒤로가기(Back) 버튼 | 기타 (정리 중) | ||
기능
충돌 (Trigger)

Rigidbody가 적어도 한쪽에 있어야 하고
Trigger함수가 있는 스크립트를 컴포넌트로 가지고 있는 게임오브젝트의 IsTrigger 체크

OnTriggerEnter는 트리거 상태의 무언가와 충돌했을 때 호출되는 함수
OnTriggerStay는 충돌 중일 때 호출되는 함수
OnTriggerExit은 충돌이 끝났을 때 호출되는 함수
물리충돌*
OnCollisionEnter
OnCollisionStay
OnCollisionExit
Gameobject - Sprite Renderer Sprite Source
Animator Controller - 상태 별 애니메이션 지정

Animation - Sprite의 연속

어차피 자주 안쓰면 까먹겠지만 이해해야 할 것은
Animator Controller는
대기 상태 → 걷는 상태를 Walk 변수가 True가 되면 변화시켜주는 것
New State라는 임의의 상태에서 Explosion이라는 Animation 상태로 가려면
Conditions에서 boom이라는 변수가 true가 되어야 한다.


변수를 이용한 상태변화는 Parameters에서 변수를 추가한 뒤 사용한다.
Animation은
대기 상태를 표현하는 연속된 그림의 모음
걷는 상태를 표현하는 연속된 그림의 모음
Gradient
Gradient 클래스로 연속적인 색깔 변화를 주고 싶다면

*[Range(0,1)]로 t에 필드 할당
t값에 따라 Sprite Color 변경

public으로 선언하면 Inspector 창에서 색깔 변화를 보면서 설정이 가능하다.
*로드씬을 하면 이전 화면은 사라진다.
스크립트
Update 종류 / 구분

유니티 배열 선언 (Array)
1차원 배열 선언

사용 방법
선언한 클래스에선 사용하지 못하고
클래스 안에 있는 메서드에서 x[1] 로 사용
2차원 배열 선언

사용 방법
1차원과 마찬가지
a[2,1] 식으로 접근

특수 기호 사용 방법
string으로 선언한 변수, " "안에서 사용
" | ' | 백슬래시 | |||||||
\" | \' | \\ |
SetActive 활용
버튼 클릭 시 팝업 창 On / Off

★Coroutine 함수

메서드명으로도 호출 가능하고 메서드로도 호출 가능하다.
변수를 포함하는 메서드 호출


메서드 이름으로 외부에서 호출 할 때

이런식으로 사용하면 외부에서 호출이 가능하다.
//보호수준에 따라 public으로 선언했으면 메서드명으로도 사용 가능하다.

*참고
여러 번 실행되면 원하는 결과를 얻지 못할수도있으니
1회만 실행 되는 곳에 놓는게 좋다.
모바일 뒤로가기(Back) 버튼
뒤로가기 버튼 종료 Event

두 번 눌렸을 때 종료 Event

소스
private void Quit()
{
if (Application.platform == RuntimePlatform.Android && Input.GetKeyDown(KeyCode.Escape))
{
_count += 1;
//화면 하단에 Toast 문구 추가
}
if (_count == 2) // 1초안에 2번 눌리면
Application.Quit();
if (exit_time > 1)
{
_count = 0;
exit_time = 0;
}
else
{
exit_time += Time.deltaTime;
}
}

기타(작업 중)
형변환
int – string

string - int

안드로이드 기능
터치

(사실상 Input.GetMouseButtonDown(0)으로도
터치 기능을 수행하기 때문에 잘 사용 안함)
진동
Handheld.Vibrate(); 1초간 진동 (코드 메인카메라에 삽입)
Script 팁 (정리 필요)
Find 는 Active 상태의 오브젝트만 찾을 수 있다.
Instantiate 함수 사용법
Instantiate (GameObject, transform.position, transform.rotation);
HP Bar 갱신
UI – Image / Source Image 삽입, Image Type / Filled
Fill Method Horizontal, Vertical, Radial 90, 180, 360
Fill Amount (0~1)
난수 생성
x = Random.Range(a, b)
Int 형이면 정수
float형이면 범위가 0.1f ~ 2.0f 여도 1.58573 이렇게 자세하게 나온다.
예외 처리 이벤트
using System; 맨 위에 추가
Try{
}
Catch(Exception e){
}
이동
transform.Translate(x,y,z) (transform에 있는 translate에 접근을 한다. x,y,z를 변경