[Unity] Unity3d 게임 엔진 공부 - 1 비기너 요약
2011. 4. 20. 16:39ㆍC# And Unity
요새 열씸히 공부중에 있습니다.
시작한지 며칠이 안되긴 했지만 쉽네요 ㅎㅎ
http://www.unity3dstudent.com/ 의 비기너 과정을 학습하고 정리했습니다.
시작한지 며칠이 안되긴 했지만 쉽네요 ㅎㅎ
http://www.unity3dstudent.com/ 의 비기너 과정을 학습하고 정리했습니다.
1. Mesh에 물리 적용하기
- Mesh에 Rigidbody 적용 (Component/Physics/Rigidbody)
Use Gravity : 중력 적용
2. Cube Mesh 충돌 검사하기.
// 충돌 했을때 호출
void OnCollisionEnter(Collision col){
Debug.Log("Hit "+col.gameObject.name);
}
3. 입력 키 캐치 하기
Edit/Project Setting/Input/Axes/중 선택
void Update()
{
// 예> Space키를 띌경우,
if (Input.GetButtonUp("Jump"))
{
Debug.Log("Input- Jump");
}
}
4. Prefabs
Create>Prefabs (Hierarchy 에는 파란색으로 인스턴스가 표현된다.)
5. Destory
충돌 되었을때 해당 충돌물체를 제거 한다.
void OnCollisionEnter(Collision col)
{
Destroy(this); //현재 스크립트 제거
Destroy(col.gameObject, 3); //3초뒤 삭제
Destroy(col.gameObject); // Object 즉시 삭제
Destroy(rigidbody); // 물리 삭제
Debug.Log("Destoryed " + col.gameObject.name);
}
6. 실시간 인스턴스 생성(Mesh 생성)
// 예> Space키를 띌경우
if (Input.GetButtonUp("Jump"))
{
Debug.Log("Input- Jump");
// 새로운 Object 생성 (생성 Object, 생성 위치, 회전각도)
Instantiate(prefab, transform.position, transform.rotation);
}
7. 위치 이동 시키기
go.transform.Translate(100*Vector3.up * Time.deltaTime);
위쪽으로 1초당 100 unit 이동시킨다.
8. 힘 가하기 (왼손 좌표계)
go.rigidbody.AddForce(transform.right * 1000);
forward : z축
right : x축
up : y 축
9. Material 생성하기
- Create>Material 후 텍스쳐 적용. 이후 Mesh 에 적용
10. Audio Play 하기
// 특정좌표에서 Audio 를 플레이 한다.
AudioClip clip;
AudioSource.PlayClipAtPoint(clip, new Vector3(-98, 12, 4));
11. Asset에서 필요한 개체를 찾는 방법
- Resources.Load("glass", typeof(Texture2D));
(이건 반드시 Asset 안에 Resources 폴더내에 있어야 한다. Load시에는 확장자를 입력하면 안된다.
- AssetBundle 이용
12. Script 에서 상위 attribute 접근시 오류나는경우
http://unity3d.com/support/documentation/ScriptReference/RequireComponent.html
[RequireComponent(typeof(AudioSource))] 를 클래스 앞에 선언해 줘야 한다.
13. Joint 물리 적용하기
두개의 Object를 연결한다.
기본이 되는 물리가 적용된(Rigidbody)Object 를 Hinge Joint 가 있는 Object에 등록하면 적용된다.
RigidBody와 HingeJoint는 같이 사용될수 없다. (Component/Physics/Hinge Joint)
14. Input GetAxis 사용하기
수평이동값을 가져와서 Object를 이동시킨다.
float value = Input.GetAxis("Horizontal");
transform.Translate(Vector3.right * value*Time.deltaTime);
15. GUIText 사용하기
Game Object>Create>GUI Text를 이용해 생성
해당 GUI를 컨트롤 하는 스크립트를 생성하여 등록
update 메소드에서 다음을 호출
guiText.text = "Count=" + count;
16. 실시간으로 Script or 물리 적용시키기
아래는 특정 물체와 충돌되었을때 실시간으로 현재 Object에 강체 물리를 등록해준다.
void OnCollisionEnter(Collision col)
{
// 충돌될때 현재 Object에 강체 물리를 적용시킨다.
if (gameObject.rigidbody == null)
{
gameObject.AddComponent<Rigidbody>();
}
}
아래 방법도 가능하다.
gameObject.AddComponent("FoobarScript");
sc = gameObject.AddComponent("SphereCollider") as SphereCollider;
17. 다음 Scene으로 넘기기
Scene번호는 File/Build Setting을 확인하면 된다.
// 충돌될때 다음 Scene로 이동시킨다.
int level = Application.loadedLevel; // 현재 레벨
Application.LoadLevel("scene2");
//Application.LoadLevel(1); //이것도 가능
18. 특정 Script의 메소드 호출
// 충돌 했을때 호출
void OnCollisionEnter(Collision col){
Debug.Log("Hit "+col.gameObject.name);
// 현 스크립트가 붙어있는 Object안의 Script의 메소드 호출
//GetComponent<GuiTestScript>().stop();
// 특정 Object안에 있는 Script의 메소드 호출
GameObject go = GameObject.Find("Gui_helloworld");
Debug.Log("Found object=" + go.name);
GuiTestScript gts = go.GetComponent<GuiTestScript>();
gts.stop();
}
19. 특정 Object에 카메라 따라가기
public class CameraLookAt : MonoBehaviour {
public Transform ts; // 따라 움직일 물체
// Use this for initialization
void Start () {
transform.LookAt(ts);
}
20. 두 Object 사이의 거리 구하기
Vector3.Distance(box.position, transform.position);
// 충돌 물체와의 거의 표시
float dis = Vector3.Distance(col.gameObject.transform.position, transform.position);
GuiTestScript.get().guiText.text = "거리="+dis;
21.corutine 을 통한 특정 로직 잠시 멈추기
IEnumerator OnCollisionEnter(Collision col)
{
yield return new WaitForSeconds(5);
- 리턴형이 IEnumerator 으로 선언해야 한다.
22. 파티클 적용하기
// 부딪쳤을때 파티클 인스턴스 생성
public ParticleEmitter particleEmitter;
void OnCollisionEnter(Collision col)
{
// 지정된 Object 생성
Instantiate(particleEmitter, transform.position, transform.rotation);
GameObject>Particle System 생성
Tangent Velocity : 탄젠트 속도 (퍼져나가는 속도)
One Shot : 딱 한번 끝까지 보여주기(반복은 됨)
Autodestruct : 한번 실행후 자동 Object 파괴
23. GUI Texture 와 Mouse Event
GameObject>Create>GUI Texture
void OnMouseEnter () {
Debug.Log("mouse enter");
}
void OnMouseExit(){
Debug.Log("mouse exit");
}
void OnMouseDown(){
Debug.Log("mouse down");
Application.LoadLevel(1); //클릭하면 다음 씬으로 이동
}
void OnMouseUp()
{
Debug.Log("mouse up");
}
25. 화면 렌더링 갱신 속도 결정
Time.timeScale = 0; // 0~1 조정 1:정상, 0:멈춤
26. 특정 Object 안에 있는 Script의 메소드 호출하기
GameObject go = GameObject.Find(next);
if (go)
{
go.SendMessage("react");
}