[유니티] 관련 코드 모음

  • 코르틴(Corotine)
StartCoroutine(Do());
protected IEnumerator Do()
{    
    yield return null;                                  // 다음 프레임까지 대기
    yield return new WaitForSeconds(float seconds);     // 지정된 초 만큼 대기
    yield return new WaitForFixedUpdate();              // 다음 물리프레임까지 대기
    yield return new WaitForEndOfFrame();               // 모든 렌더링작업이 끝날 때까지 대기
    yield return StartCoroutine(IEnumerator routine);   // 다른 코르틴이 끝날 때까지 대기
    yield return new WWW(string url);                   // 웹 통신 작업이 끝날 때까지 대기
    yield return new AsyncOperation;                    // 비동기 작업이 끝날 때까지 대기(씬로딩)
    
    do
    {
        yield return null;                         // 무한 루프 방지
    } while (false);
}
  • Mathf   // 유니티 수학관련 클래스
// 입력 된 value 값이 최소 값(_minRadius), 최대 값(_maxRadius)을 범위로 제한
public float _radius, _minRadius, _maxRadius;
public float redius
{
    get { return _radius; }
    private set
    {
        _radius = Mathf.Clamp(value, _minRadius, _maxRadius);
    }
}

// 입력 된 value 값이 최대 값 이후 최소 부터 다시 시작 
public float _azimuth, _maxAzimuth, _minAzimuth;
public float azimuth 
{
    get { return _azimuth; }
    private set
    {
        _azimuth = Mathf.Repeat(value, _maxAzimuth - _maxAzimuth);
    }
}
  • object(C# 참조)
    class ObjectTest
    {
       public int i = 10;
    }
    
    class MainClass2
    {
       static void Main()
       {
          object a;
          a = 1;   // an example of boxing
          Console.WriteLine(a);
          Console.WriteLine(a.GetType());
          Console.WriteLine(a.ToString());
    
          a = new ObjectTest();
          ObjectTest classRef;
          classRef = (ObjectTest)a;
          Console.WriteLine(classRef.i);
       }
    }
    /* Output
        1
        System.Int32
        1
     * 10
    */
    

    • int? i  // 변수 i가 NULL 값을 취할 수 있다
    int?, double?, bool?, char?
    int? i = 10;
    int? b = null;
    i = i + b; // i == null
    
    • "??" 연산자는 null 병합 연산자라고 합니다. 이 연산자는 피연산자가 null이 아닐 경우 왼쪽 피연산자를 반환하고 null일 경우 오른쪽 피연산자를 반환합니다.
    class NullCoalesce
    {
        static int? GetNullableInt()
        {
            return null;
        }
    
        static string GetStringValue()
        {
            return null;
        }
    
        static void Main()
        {
            int? x = null;
    
            // Set y to the value of x if x is NOT null; otherwise,
            // if x = null, set y to -1.
            int y = x ?? -1;
    
            // Assign i to return value of the method if the method's result
            // is NOT null; otherwise, if the result is null, set i to the
            // default value of int.
            int i = GetNullableInt() ?? default(int);
    
            string s = GetStringValue();
            // Display the value of s if s is NOT null; otherwise, 
            // display the string "Unspecified".
            Console.WriteLine(s ?? "Unspecified");
        }
    }

    댓글

    이 블로그의 인기 게시물

    [유니티] 오류 사례 "Moving file failed", "Temp"

    [Flutter] flutter setup error, "Unable to locate Android SDK.", "[!] Connected device"

    [유니티] 구글 지도 출력 샘플