본문 바로가기

-----Unity 3d----/Info

[Info][펌]유니티에서 C#으로 코딩할때 주의할 점

아래 내용은 ☞ http://blog.naver.com/leonhong/20146039629 에서 가지고 왔습니다.


1. MonoBehaviour, ScriptableObject 로 부터 ( 직접 혹은 간접으로 ) 상속 받아야 한다.

     ps. GameObject 에 Attach 하려면 MonoBehavior로 부터 상속받고,

           일반 로직등에 필요한 클래스는 ScriptableObject로 부터 상속 받는다. 

 

2. 클래스 이름과 파일 이름이 일치해야 한다. 

 

3. namespace를 사용할 수 없다.  -> 사용할 수 있지 않나요?

 

4. MonoBehaviour 일 경우, 멤버 변수만 Inspector 에 나타난다.

 

5. MonoBehaviour 일 경우, 생성자를 사용하지 말고,  Awake( 이게 생성자 ), Start 함수를 사용한다.

 

6. ScriptableObject일 경우, MonoBehaviour 상속받은 스크립트에서 인스턴스 생성시 new를 사용하지 말고,ScriptableObject.CreateInstance("클래스명") as 클래스형을 사용한다.

 

7. ScriptableObject일 경우,  인스턴스 생성후에 새로운 객체를 재할당 할 때는, GameObject.DestroyImmediate(인스턴스) 호출하여 메모리를 꼭 해지하고 재할당.

 

8. Corutine은 다른 문법으로 사용된다.

반환형은 IEnumerator를 사용해야 하며, yield ... 를 사용할 때, yield return ... 을 사용한다.

 

using System.Collections;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
    // C# coroutine
    IEnumerator SomeCoroutine () {
    // Wait for one frame
    yield return 0;
   
    // Wait for two seconds
    yield return new WaitForSeconds (2);
}

}