블로그 이미지
Every unexpected event is a path to learning for you. blueasa

카테고리

분류 전체보기 (2862)
Unity3D (899)
Script (95)
Extensions (16)
Effect (3)
NGUI (81)
UGUI (9)
Physics (2)
Shader (43)
Math (1)
Design Pattern (2)
Xml (1)
Tips (204)
Link (26)
World (1)
AssetBundle (25)
Mecanim (2)
Plugins (87)
Trouble Shooting (71)
Encrypt (7)
LightMap (4)
Shadow (4)
Editor (12)
Crash Report (3)
Utility (9)
UnityVS (2)
Facebook SDK (2)
iTween (3)
Font (18)
Ad (14)
Photon (2)
IAP (1)
Google (12)
URP (6)
Android (54)
iOS (47)
Programming (479)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (192)
협업 (65)
3DS Max (3)
Game (12)
Utility (142)
Etc (99)
Link (34)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (16)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (20)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday

이전에 Coroutine을 사용했을때는 그냥 사용했는데.. 어디에 제대로 사용하는 것이 맞는것일까? 고민도 하구..
unityStudy 사이트에 너무 잘 설명해 놓은 글이 있어서 차라리 그 글을 한번 정독하면 큰 도움이 될것입니다
.

URL : 
[스크립팅] 코루틴(Coroutine) 기본 개념  활용 
두번째 해당 사이트는 영문입니다. : http://unitygems.com/coroutines/

아래는 백과사전 및 위 사이트에 대한 부분을 이해하기 위해 정리했습니다.

 

1.    동시 실행(협동) 루틴이라 불리 우는 Coroutine 정의

A.     호출 루틴과 피호출 루팅이 대등 관계를 유지하는 처리 절차.
부차적 프로그램의 수행이 완전히 끝나기 전에 제어가 호출 루틴으로 돌아가는 것이 동시 실행 과정이다. 그리고 제어가 부차적 프로그램으로 돌아 왔을 때는 중단된 부분부터 다시 수행이 계속 된다. 주종 관계를 갖지 않고 서로 호출하는 둘 이상의 모듈들.서브루틴의 제어 전달 개념과 유사한것. 각 호출에서 초기화되는 서브루틴과는 달리, 호출 시 관련된 모든 정보를 보존하는 능력을 갖는다. 그리고 다음에 다시 시작할 때에는 이전에 실행했던 다음부터 실행 할 수 있는 논리를 갖는다.

B.     프로그램에서 순서는 일반적으로 불려지는 쪽이 부르는 쪽에 속하고 있는 것이 대부분이지만 어느 쪽도 종속 관계가 아니라 대등한 관계로 서로 호출하는 것이다.
예를 들면, 게임 프로그램에서 각 플레이어 루틴은 서로 Coroutine 된다. 복수 프로세스 간에서 한정된 형태의 통신을 행하는 프로그램을 순차 제어로 실현한 것으로 볼 수 있다.

C.     C언어 등에서 일반적으로 사용하는 함수는 시작할 때 진입하는 지점이 하나 존재하고 함수가 모두 실행되거나,  return 구문에 의해서 종료되는 지점을 설정 할 수 있다.
이러한 함수를 서브 루틴(subroutine)이라 부르는데, 코루틴은 이를 더 일반화한 개념으로 진입하는 지점까지 여러 개를 가질 수 있는 함수를 의미. (서브 루틴도 코루틴의 한 종류)

D.     Coroutine 은 프로그램 상에서 보면 멀티 쓰레드와 비슷한 개념이다.
별도의 스택과 지역변수들을 가진 실행 중, 각자의 메모리 영역과 실행 명령 포인터를 가지고 있어
,
재개(Resume)와 양보(yield)가 가능한 구조로 되어 있다
.
중지(yield)하면 실제 프로그램(함수)는 종료되지 않고 위치를 대기시킨 후 값이 만들어지는 시점부터 다시 시작(resume) 할수 있다는 말이다.

 

2.    왜 필요한 것인가?

유니티에서 코루틴과 상호 작용하는 주체는 엔진이다
.
코루틴을 사용 할 때는 C#의 언어적인 기능을 익혀서 모든 것을 해결한다는 개념보다 스크립트에서 엔진이 가지는 기능을 활용하는 또 한가지의 관점으로 접근해야 한다
.
스크립트에서 StartCoroutine 함수를 통해 코루틴을 구동하면 코루틴은 첫 번째 데이터를 엔진에 전달하고 이 데이터를 분석한 엔진은 내부 루푸를 돌면서 필요한 때가 되면 다음 데이터를 전달하도록 코루틴을 다시 호출해 준다. 이때 엔진에 전달할 데이터가 정말 중요한데, 데이터들은 코루틴이 유니티 엔진에게 보내는 일종의 명령으로 생각하면 좋다
.



 

3.    자주 등장하는 조연 배우들…

A.     Yield 
Coroutine 
은 Yield 를 사용하여 중간에 제어권을 다른 함수에 넘겨주는 역할을 한다.
yield 를 사용하여 일정시간 대기 할 수 있는 함수도 있다
.
yield return new WaitForSecond(3.0f); 
3초  대기 후에 리턴을 해주는 순서다.  여기서 기본적인 추가 특징으로 update 함수상에서는 yield 문의 직접적인 사용이 금지가 됩니다.

B.     IEnumerator
(msdn) 
제네릭이 아닌 컬렉션을 단순하게 반복할 수 있도록 지원하는 인터페이스
우리말로 열거자 라고 하는데 , 데이터의 목록을 하나씩 넘겨 줄 때 사용되는 인터페이스 이다.
코루틴은 호출한 함수와 서로 상호작용하면서 진행하도록 설계되어 있습니다
.
코루틴은 자신을 호출한 함수에 데이터를 하나 넘겨주고 쉰다
.
받은 측에서는 데이터를 받고 나서 처리한 후에 코루틴에게 다음 데이터를 달라고 깨운다
.
쉬고 있던 코루틴은 일어나서 다시 데이터를 전달하고 .. 이를 계속 반복하는 구조로 동작한다
.
이런 작업에 적절한 인터페이스가 IEnumerator이며, C# 에서 코루틴을 사용할 때는 이를 사용한다
.
일반적으로 호출한 함수에게 데이터를 전달 할때, return 구문을 사용하게 되면 데이터를 전달하고 함수는 종료 된다.

C.     StartCoroutine 함수
IEnumerator 반환형을 받아 yield 이 만날 때 함수를 일시 정지하고, 다른 스크립트나 함수에게 활동을 넘긴다. 다시 말해 코루틴 함수를 구동하는 함수다.



 

4.    엔진이 제공하는 데이터들과 이를 수행하는 명령들.

A.     Yield return null : 다음 프레임까지 대기

B.     Yield return new WaitForSeconds(flat) : 지정된 초(float) 만큼 대기

C.     Yield return new WaitForFixedUpdate() : 다음 물리 프레임까지 대기

D.     Yield return new WaitForEndOfFrame() : 모든 렌더링 작업이 끝날 때 까지 대기

E.     Yield return StartCoroutine(string) : 다른 코루틴이 끝날 때 까지 대기

F.      Yield return new WWW(string) : 웹 통신 작업이 끝날 때까지 대기

G.     Yield return new AsyncOperation : 비동기 작업이 끝날 때 까지 대기 (씬로딩)

•null - the coroutine executes the next time that it is eligible

•WaitForEndOfFrame - the coroutine executes on the frame, after all of the rendering and GUI is complete

•WaitForFixedUpdate - causes this coroutine to execute at the next physics step, after all physics is calculated

•WaitForSeconds - causes the coroutine not to execute for a given game time period

•WWW - waits for a web request to complete (resumes as if WaitForSeconds or null)

•Another coroutine - in which case the new coroutine will run to completion before the yielder is resumed

 


 

 

 

5.    Coroutine 의 장점

A.     성능
yield return new WaitForSecond(3.0f); 
이라는 명령을 수행하면, 코루틴은 유니티 엔진에게 WaitForSeconds(3.0f) 라는 데이터를 보내고 쉬기 시작합니다.
코루틴이 없이 일반적으로 이를 구현한다면, Update 문에서 Time.deltaTime 을 사용하여 매 프레임마다 시간을 더해서 10초가 지났는지 감지해야 하는데, 프레임의 평균 소요 시간이 0.01초 라고 한다면 아이러니하게 10초 동안 대기 하기 위해 스크립트는Update 함수를 1000번 호출해야 합니다
.
특히 모바일 기기에서 코루틴의 활용은 성능 향상에 큰 영향을 미친다
.

B.     가독성
코루틴을 사용하면 읽기 좋은 코드가 만들어 진다. 같은 결과물을 나타내는 두개의 코드로 비교하면

//update 상에서 코루틴을 사용하지 않고 직접 구현시

public class UpdateTimer : MonoBehaviour {
    float accumulator = 0.0f;
    bool wait1Finished = false;
    bool wait2Finished = false;
    float waitTime1 = 1.0f;
    float waitTime2 = 2.0f;

    void Start() {
        Debug.Log ("Action Start!");
    }
    void Update () {
        accumulator += Time.deltaTime;
        if(!wait1Finished && !wait2Finished) {
            if(accumulator >= waitTime1) {
                Debug.Log ("Action1 End");
                wait1Finished = true;
                accumulator = 0.0f;  
            }
        } else if(wait1Finished) {
            if(accumulator >= waitTime2) {
                Debug.Log ("Action2 End");
                wait2Finished = true;
                accumulator = 0.0f;  
            }
        }
    }
}

//Start 
함수를 IEnumerator 로 선언시. (엔진이 자동으로 코루틴으로 실행해줌.
public class CoRoutineTimer : MonoBehaviour {
    float waitTime1 = 1.0f;
    float waitTime2 = 2.0f;   

    IEnumerator Start () {
        Debug.Log ("Action Start");
        yield return new WaitForSeconds(waitTime1);
        Debug.Log ("Action1 End");
        yield return new WaitForSeconds(waitTime2);
        Debug.Log ("Action2 End");
    }     
}

 

6.    특징 및 활용

A.     가장 많이 사용되는 건 WaitForSeconds 명령

멀티 쓰레드 프로그래밍의 Thread.Sleep 함수 처럼 원하는 시간 만큼 잘수 있다. 하지만 더욱 좋은 점은 엔진과 동일한 싱글 쓰레드로 돌아가기 때문에, 멀티 쓰레드 프로그래밍의 어려운 부분인 자원 관리 및 컨텍스트 스위칭(Context Switching)과 같은 다양한 고려 사항의 필요가 없음.

B.     두번째로 많인 사용하는 부분은 비동기 작업

웹에서의 데이터 수신, 씬 로딩과 같이 오래 걸리는 작업은 엔진에게 넘기고, 자신은 진행상황을 점검하면서 UI 처리만 수행하는 형태로 사용합니다.
아래와 같이 WWW 클래스가 대표적인 비동기 작업인데, 웹에서 데이터를 다운로드 받기 위해 스크립트는 엔진에게 www 클래스를 만들어 데이터를 받아올 URL과 함께 넘기고 자신은 쉰다
.
이를 받은 엔진은 비동기 처리를 위한 쓰레드를 생성하고, 네이티브 라이브러리를 사용하여 웹에 접속한  후 데이터를 다운로드 하면서 스크립트로부터 넘겨 받은 www 객체에 직장 상사에게 보고하듯 현재 진행상황을 저장해 둔다.(이 때 클래스의 레퍼런스를 넘겼기 때문에 다른 스크립트에서 진행상황을 읽어 들이는 것이 가능함
.)
엔진에서 다운로드가 끝나면 www 객체에는 완료 상황보고와 함께 받은 데이터가 저장되며, 엔진은 스크립트를 깨운다. 스크립트에서는 바로 그 떄부터 그것을 가지고 사용한다
.

//코루틴이 쉬는 동안 엔진은 열심히 다운로드 받고, Update 함수는 이를 보면서 열심히 진행상황을 로그에 남기는 예제임
.
public class WebBasic : MonoBehaviour {
    public string url;
    WWW www;
    bool isDownloading = false;

    IEnumerator Start()
    {
        www = new WWW(url);
        isDownloading = true;
        yield return www;
        isDownloading = false;
        Debug.Log ("Download Completed!");
    }

    void Update()
    {
        if(isDownloading)
            Debug.Log (www.progress); 
    }
}

//update 에서 이런 처리 부담스러워…프로그래스 바를 넣는식으로 변경하는 방법으로
..
public class WebAdvanced : MonoBehaviour {
    public string url;
    WWW www;

    IEnumerator Start()
    {
       www = new WWW(url);
       StartCoroutine("CheckProgress");
       yield return www;
       Debug.Log ("Download Completed!");
    }

    IEnumerator CheckProgress()
    {
        Debug.Log (www.progress);
        while(!www.isDone)
        {
            yield return new WaitForSeconds(0.5f);
            Debug.Log (www.progress);
        }
    }
}

C.     마지막으로 코루틴을 사용하여 작업을 원하는 대로 순차적으로 수행 할수 있다.

C#과 같은 고급 언어(High Level Language) 에서 지원하는 리플렉션(Reflection) 기능과 코루틴 기능이 혼합된 활용성에 있어서 백미라고 할수 있다
.
이의 활용 예를 위해 RPG 게임등에서 흔히 보이는 패턴인 A 와 B 중 하나를 선택하게 하는 다이얼로그를 띄운 후 , 사용자의 결정에 따라 A 를 선택하면 ActionA 를 수행하고  B 를 선택하면 ActionB를 수행하는 구문을 프로그래밍 할 때 이를 코루틴으로 구현하기 위해, 먼저 모든 행동을 코루틴으로 정의해 준다
.
그리고 각 행동들의 순서를 제어하는 코루틴을 만들어주고, 코루틴 안에서 행동들이 순차적으로 실행되도록 yield return new StartCoroutine 구문으로 차례대로 지정해 준다
.
첫번째 코루틴의 결과에 따라 다음 행동이 A 가 될지 B 가 될지 결정되므로, 첫 번째 결과 값을 string 변수로 지정하고, 변수 값을 StartCoroutine 에 넣으면 실시간으로 결과에 따른 해당 코루틴이 실시간으로 실행 된다. 아래는 실 구현 예제 


public class DialogExample : MonoBehaviour {
    bool showDialog = false;
    string answer = "";

    IEnumerator Start()
    {
        yield return StartCoroutine("ShowDialog");
        yield return StartCoroutine(answer);
    }

    IEnumerator ShowDialog()
    {
        showDialog = true;
        do
        {
            yield return null;
        } while(answer == "");
        showDialog = false;
    }

    IEnumerator ActionA()
    {
        Debug.Log ("Action A");
        yield return new WaitForSeconds(1f);
    }

    IEnumerator ActionB()
    {
        Debug.Log ("Action B");
        yield return new WaitForSeconds(2f);
    }
    void OnGUI()
    {
        if(showDialog)
        {
            if(GUI.Button(new Rect(10f, 10f, 100f, 20f), "A"))
            {
                answer = "ActionA";   
            } else if(GUI.Button(new Rect(10f, 50f, 100f, 20f), "B")) {
                answer = "ActionB";
            }
        }
    }
}

//
결과 값의 유효성을 강화하고 싶다면 아래와 같이 enum 를 보조적으로 사용하는 것도 좋은 방법이 될수 있다.

using UnityEngine;
using System.Collections;

public class DialogExampleEnum : MonoBehaviour {
    enum DialogActions {
        ShowDialog,
        ActionA,
        ActionB
    };

    bool showDialog = false;
    DialogActions action = DialogActions.ShowDialog;
    IEnumerator Start()
    {
        yield return StartCoroutine(action.ToString());
        yield return StartCoroutine(action.ToString());
    }

    IEnumerator ShowDialog()
    {
        showDialog = true;
        do
        {
            yield return null;
        } while(action == DialogActions.ShowDialog);
        showDialog = false;
    }
    IEnumerator ActionA()
    {
        Debug.Log ("Action A");
        yield return new WaitForSeconds(1f);
    }
    IEnumerator ActionB()
    {
        Debug.Log ("Action B");
        yield return new WaitForSeconds(2f);
    }
    void OnGUI()
    {
        if(showDialog)
        {
            if(GUI.Button(new Rect(10f, 10f, 100f, 20f), "A"))
            {
                action = DialogActions.ActionA;         
            } else if(GUI.Button(new Rect(10f, 50f, 100f, 20f), "B")) {
                action = DialogActions.ActionB;
            }
        }
    }
}
이러한 특징을 잘 홀용하면 AI 를 위한 유한상태 기계(Finite State Machine) 을 구현할 때에도 별도의 클래스를 만들지 않고 쉽게 코딩이 가능해 진다
.

D.     Coroutine 는 for 루프 내에서는 사용할 수 없지만, while 문이나 foreach 문에서는 사용이 가능하다.
IEnumerator Foo()
{
 while(someCondition)
 { 
   // 여기서 시간이 오래 걸리는 처리를 수행한다
.
   // 다음 프레임 전에 일시 정지
.
 }
}


*Tip : 이전부터 Debug.Log 을 사용하는 것을 꺼려 했다. 실제 빌드 후에 자동으로 사라지는 것이 아니기 때문에,
이 함수를 별도로 따로 빼서 클래스를 생성해서, 그 클래스를 직접 사용하는 편이 가장 좋다
.
http://answers.unity3d.com/questions/126315/debuglog-in-build.html

public static void Log (String method)
  {
       Debug.Log (method);
    }



[출처] http://kimseunghyun76.tistory.com/304

반응형
Posted by blueasa
, |
 

在Unity3D的Legacy动画系统中应用Root Motion

标签: unity3d动画Root Motion
 5591人阅读 评论(2) 收藏 举报
 分类:
 

目录(?)[+]

最近仔细比较了Unity3D目前版本中的两套动画系统:Legacy和Mecanim。Mecanim系统功能较之Legacy要强大很多,但是使用AnimatorController着实不方便(尽管使用AnimatorOverrideController可以避免重复编辑状态机),是因为游戏逻辑层面往往要用一个状态机或者类似的机制来控制角色的状态,而角色层面的状态逻辑和动画层面是无法一一对应的,两套复杂的状态机要配合起来。。。想想就觉得蛋疼啊!难怪很多朋友现在还在使用Legacy动画系统。Legacy动画系统其实功能也很全面了,包括Layer、过渡混合、上下身混合之类的功能完全能够胜任,而且控制起来就直接的多了。唯独Root Motion这个我很需要特性没有支持,本文就探讨一下如何在Legacy动画系统之上附加Root Motion功能,其实很简单大笑

何谓Root Motion

在不使用Root Motion的情况下,类似走、跑这样的位移控制是这样的:
  1. 请美术在导出动画时把位移去掉;
  2. 在程序代码里控制角色移动的速度,在播放动画的同时,计算其位移。
这种做法其实挺不科学的,程序控制的角色,只能当做一个质点来处理,并且大多数时候都是匀速运动,而动画中的角色的移动往往很难跟这个匹配。所以需要比较良好的计算和比较好的美术技巧才能避免角色“滑步”的现象。在“跑”这种快速移动这,滑步还比较好处理,如果是慢速移动。。。。再厉害的美术也爱莫能助了。这种情况下,最好还是使用Root Motion:
  1. 美术在导出动画的时候是附带位移的;
  2. 程序把动画的每一帧的位移是从动画中读取出来,再应用到角色上的,这样就能达到动画和位移的完美匹配了。

在Legacy中添加Root Motion功能

了解了Root Motion的概念之后,在Unity3D引擎中我们很简单就可以实现此功能了。Unity3D有一个统一的对象层次结构设计,这点非常赞,我们可以很简单找到角色的根骨骼,然后把其中的Transform变换读取出来,请见以下示例代码:
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //-- 计算当前帧的Root Motion  
  2.             Vector3 rootPos = m_rootBone.localPosition;  
  3.             m_rootMotion = rootPos - m_lastRootPos;  
  4.             m_lastRootPos = rootPos;  
  5.             rootPos.x = 0;  
  6.             rootPos.z = 0;  
  7.             m_rootMotion.y = 0;  
  8.             m_rootBone.localPosition = rootPos;  
请注意,我们在后续的代码中要把m_rootMotion附加的角色对象上,所以m_rootBone的postion被reset了。
在读取了此帧的Root Motion,在可以把它应用到当前对象之上了:
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //-- Apply Root Motion  
  2.     Vector3 nextPos = this.transform.position + m_rootMotion;  
  3.         this.transform.position = nextPos;  
另外,一个细节需要处理一下,在动画循环的那一帧,需要特殊处理一下。好的,看一下完整的源代码吧:
[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class ApplyRootMotion : MonoBehaviour   
  5. {  
  6.     public Transform m_flagObject;  // 用来测试位置的一个对象  
  7.   
  8.     //-- Root Motion 控制变量  
  9.     Transform m_rootBone;  
  10.     Vector3 m_lastRootPos;  
  11.     Vector3 m_rootMotion;  
  12.     int m_lastAnimTime;  
  13.   
  14.     void Start ()   
  15.     {  
  16.         //-- 从SkinnedMeshRenderer中读取Root Bone  
  17.         SkinnedMeshRenderer skinMesh = this.gameObject.GetComponentInChildren<SkinnedMeshRenderer>();  
  18.         m_rootBone = skinMesh.rootBone;  
  19.   
  20.         //-- 变量初始化  
  21.         m_rootMotion = Vector3.zero;  
  22.         m_lastRootPos = m_rootBone.localPosition;  
  23.         m_lastAnimTime = 0;  
  24.     }  
  25.       
  26.     void Update ()   
  27.     {  
  28.         //-- Apply Root Motion  
  29.         Vector3 nextPos = this.transform.position + m_rootMotion;  
  30.         this.transform.position = nextPos;  
  31.   
  32.         //-- 测试代码:更新测试物体的位置  
  33.         Vector3 flagPos = m_flagObject.position;  
  34.         flagPos.x = nextPos.x;  
  35.         flagPos.z = nextPos.z;  
  36.         m_flagObject.position = flagPos;  
  37.   
  38.         //-- 测试代码:更新摄像机  
  39.         Camera.main.transform.LookAt(this.transform);  
  40.     }  
  41.   
  42.     void LateUpdate()  
  43.     {  
  44.         AnimationState animState = this.animation["walking"];  
  45.   
  46.         if ((int)animState.normalizedTime > m_lastAnimTime)  
  47.         {  
  48.             //-- 动画循环处理  
  49.             m_lastRootPos = m_rootBone.localPosition;  
  50.             m_rootMotion = Vector3.zero;  
  51.         }  
  52.         else  
  53.         {  
  54.             //-- 计算当前帧的Root Motion  
  55.             Vector3 rootPos = m_rootBone.localPosition;  
  56.             m_rootMotion = rootPos - m_lastRootPos;  
  57.             m_lastRootPos = rootPos;  
  58.             rootPos.x = 0;  
  59.             rootPos.z = 0;  
  60.             m_rootMotion.y = 0;  
  61.             m_rootBone.localPosition = rootPos;  
  62.         }  
  63.         m_lastAnimTime = (int)animState.normalizedTime;  
  64.     }  
  65. }  

最后是截图。。。好吧,静态图片看不出效果,可以下载完整Demo(请使用Unity 4.6版本打开),角色移动非常平滑,毫无滑步。

请移步百度网盘:http://pan.baidu.com/s/1o6kJsIe 密码:osoc






반응형
Posted by blueasa
, |

BigNumber

Unity3D/Script / 2016. 2. 4. 16:49

[파일]


BigNumber.cs



[출처]

기억안남..;;

소스 제작자 분을 찾습니다~ -ㅁ-;

반응형
Posted by blueasa
, |

GameObject.active = true;

  • StartCoroutine : OK
  • Coroutine Method : OK
  • StopAllCoroutine : OK
  • Invoke : OK
  • InvokeRepeating : OK
  • CancelInvoke : OK


GameObject.active = false;

  • StartCoroutine : Error
  • Coroutine Method : Stop (active 상태에서 돌고 있는 상태였다면 정지된다.)
  • StopAllCoroutine : OK
  • Invoke : OK
  • InvokeRepeating : OK
  • CancelInvoke : OK

** 결론

  • StartCoroutine 으로 코루틴 메소드를 열심히 돌리다가 해당 GameObject 의 active 가 꺼지면 돌던 코루틴 메소드는 정지되고 다시 active 가 켜지더라도 재실행되지 않는다.....
  • 코루틴을 많이 사용하는 로직(Async 로 돌리는 함수들..(WWW, ... 같은...))에서는 꼭 염두해 두어야 한다..
  • OnDisable() {} 에서 관련 처리를 해 주는게 좋다.
  • Invoke 관련은 GameObject 의 active 유무랑 관계없이 돌아간다..


출처 : http://limchaeng.tistory.com/34

반응형

'Unity3D > Script' 카테고리의 다른 글

[펌] Root Motion in Legacy Animation  (0) 2016.03.28
BigNumber  (0) 2016.02.04
임의시간 받아서 시간 표현하기  (0) 2015.10.28
Control ParticleSystem RenderQueue  (0) 2015.09.24
[공유] UnityBoot 프로젝트  (0) 2015.08.30
Posted by blueasa
, |
using System;

// 1200초(20분) 받아서 시간:분:초 표시하기
TimeSpan ts = new TimeSpan(0, 0, 1200);
string strTemp = string.Format("{0:HH:mm:ss}", ts);

Debug.Log(strTemp);


[날짜/시간 출력 참조]

http://www.csharp-examples.net/string-format-datetime/


반응형
Posted by blueasa
, |
using UnityEngine;

public class SetRenderQueue : MonoBehaviour
{
    public int renderQueue = 3000;

    Material mMat;

    void Start ()
    {
        Renderer ren = renderer;

        if (ren == null)
        {
            ParticleSystem sys = GetComponent< ParticleSystem >();
            if (sys != null) ren = sys.renderer;
        }

        if (ren != null)
        {
            mMat = new Material(ren.sharedMaterial);
            mMat.renderQueue = renderQueue;
            ren.material = mMat;
        }
    }

    void OnDestroy () { if (mMat != null) Destroy(mMat); }
}



출처 : http://www.tasharen.com/forum/index.php?topic=776.msg34546#msg34546

반응형
Posted by blueasa
, |

[링크] http://www.gamecodi.com/board/zboard-id-GAMECODI_Talkdev-no-3545-z-6.htm


게임코디 '뜻밖의'님이 좋은 소스를 공유해 주셔서 링크해 놓습니다.




 Link : https://github.com/Raindayus/unityboot

새프로젝트 만들때마다, 공용 유틸클래스들과, 
프로젝트의 와꾸등을 이전 프로젝트에서 복사해왔는데, 
이게 귀찮아서, 공통 작업들을 "spring boot"같이 "unityboot"라는 프로젝트로 한번 묶어 봤습니다. 
링크를 누르시면 github으로 연결됩니다.

문서화가 안되어 있긴하지만,
뒤적거리시다보면, 뭔가 쓸모 있는게 있으실지도...

아래와 같은 것들이 포함되어 있습니다.

Service 
----
sb : 다국이 지원, Resources/StringBundles/StringBundle.txt 에서 텍스트 편집
bundle : 애셋 번들 다운로드 지원, 씬 / 리소스 다운로드 지원, 멀티 다운로드 지원
goPooler : 게임 오브젝트 풀러, 로컬/리모트 리소스 풀링 가능 (단 GoItem을 상속받은 클래스를 포함한 게임오브젝트만 풀링 가능)
encryption : 3Des 암호화 모듈
sound : 사운드 플레이 관련 서비스
setting : 설정 관련 서비스
time : 시간 관련 서비스

Editor 
----
Builder : 애셋번들 리소스 패커, 씬과 리소스를 아이퐁 / 안드로이드 타켓으로 패킹

Utils
----
AverageFilter : 평균값 필터, 스무스 카메라 개발등에 사용
CameraFade : 페이드인 / 아웃 / 블링크 지원
CsvParser : Csv 파일 파서
DescendantMap : 하위 컴포넌트를 쉽게 찾기 위한 맵, UI작업시 사용 
Ease : easing function 모음
GameObjectExtensions : 게임 오브젝트와 MonoBehaviour 확장
Logger : 로깅 클래스
PersistenceUtil : 파일 저장 / 로드 기능 제공, 암호화기능을 같이 제공
Phantom : 메모리 암호화 관련
Singleton : 싱글턴 클래스
Vector3Extensions : Vector3 기능 확장
WeakReference : 제네릭 WeakReference 클래스
Wheel : 간단한 회전 함수
UIArranger : NGUI 다양한 해상도 대응을 위한 클래스, BOOT_NGUI_SUPPORT 를 Define해줘야 돌아감..

ThirdParty
----
LitJson: Json파싱 모듈
CreatePlane : 간단한 플랜 생성 가능


아래 씬을 열면 테스트 가능합니다. 건저갈만한게 있으시길~
Assets/Demo/Scene/BootDemo/


반응형
Posted by blueasa
, |

유니티로 개발을 하다보면 스크립트에서 public으로 빼놓은 것을 

EditMode(작업모드)에서 바로 적용된 것을 보고 싶을때가 있습니다.

처음에 유니티를 하게되면 항상 ▶버튼을 눌러 실행을 한 후 확인하게 됩니다.


물론 ▶버튼을 눌러서 확인을 해야 하는 경우도 있지만 가볍게 변경된 것을 스크립트를 통해 바로 확인하고 싶을 때 사용하면 좋을 것 입니다.


[ExecuteInEditMode] 를 사용하여 바로 변경되도록 할 수 있는데요.

이 키워드를 스크립트에서 class위에 배치를 하면 Update부분에 적용한 것을 EditMode에서 바로 확인 하실 수 있습니다.


/* 예제 */

/*******************************************************************/

[ExecuteInEditMode]

public class UIRoot : MonoBehaviour

{

void Update()

{

//EditMode이든 실행되는 화면이든 변경되었으면 하는 부분 처리

}

}

/*******************************************************************/ 






위에 두 그림 처럼 Manual Height 의 값을 조정하면 Transform 부분에 Scale 부분에 값이 변하는 것을 확인 하실 수 있을껍니다.

위에 두 그림은 실행모드가 아닌 EditMode임을 말씀 드립니다.




출처 : http://jhc777.tistory.com/65




The functions are not called constantly like they are in play mode.
Update is only called when something in the scene changed.
OnGUI is called when the Game View recieves an Event.

OnRenderObject and the other rendering callback functions are called on every repaint of the Scene View or Game View.


참조 : http://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html

반응형
Posted by blueasa
, |
[파일]

2d 모바일 게임 만들때 유용합니다.
카메라에 붙여서 사용하시면 됩니다.
 
총 6 모드 지원합니다.
전부 보여주기, 좌우비율 맞추기, 가로 고정, 세로 고정, 여백 없음, 늘리기
 
https://gist.github.com/howlryu/6bf4305c96f7dda4a3f4




출처 : http://unitystudy.net/bbs/board.php?bo_table=tip&wr_id=124

반응형
Posted by blueasa
, |

timeScale Lerp – Custom Time Manager

By now I need a timeScale Lerp and this need to be Time.deltaTime independent so, for anyone who needs this too, here’s what I’ve got (C#). You can also use it to create slow motion effects and control the play /pause of your game (assuming that you are using a Time.timeScale dependent approach):

You cal also get this code at GitHub

/// <summary>
/// Time Manager.
/// Time.deltaTime independent Time.timeScale Lerp.
/// Author: Fabio Paes Pedro
/// </summary>
///
/// 

using UnityEngine;
using System.Collections;

public class CustomTimeManager : MonoBehaviour
{
    /// <summary>
    /// CustomTimeManager is Paused or not
    /// </summary>
    [SerializeField]
    private bool _isPaused = false;
    /// <summary>
    /// CustomTimeManager is Fading (from _minScale to _scale or vice-versa)
    /// </summary>
    [SerializeField]
    private bool _isFading = false;
    /// <summary>
    /// CustomTimeManager will pause after fading (is going from _scale to _minScale)
    /// </summary>
    [SerializeField]
    private bool _willPause = false;

    [SerializeField]
    private float _scale = 1f;
    private float _fadeToScaleDifference = 0f;
    private float _scaleToFade = 0f;
    [SerializeField]
    private float _deltaTime = 0f;
    private float _lastTime = 0f;
    private float _maxScale = 3f;
    private float _minScale = 0f;
    private bool _fadeToScaleIsGreater = false;



    #region PseudoSingleton
    private static CustomTimeManager _instance;
    public static CustomTimeManager Instance
    {
        get
        {
            return _instance;
        }
    }
    void Awake()
    {
        if (_instance != null) Debug.LogError("There's another instance of " + this + " already");
        _instance = this;
    }
    void OnDestroy()
    {
        _instance = null;
    }
    #endregion


    void Start()
    {
        Scale = Time.timeScale;
        StartCoroutine("UpdateDeltaTime");
    }

    public bool WillPause
    {
        get
        {
            return _willPause;
        }
    }

    public bool IsFading
    {
        get
        {
            return _isFading;
        }
    }

    public bool IsPaused
    {
        get
        {
            return _isPaused;
        }
    }


    /// <summary>
    /// Time.timeScale independent deltaTime
    /// </summary>
    /// <value>
    /// time.timeScale independent Delta Time
    /// </value>
    public float DeltaTime
    {
        get
        {
            return _deltaTime;
        }
    }

    /// <summary>
    /// Getter and Setter for the CustomTimeManager Scale (time.timeScale). This will set IsPaused to true automatically if the scale == 0f
    /// </summary>
    /// <value>
    /// Scale (Time.timeScale)
    /// </value>
    public float Scale
    {
        get
        {
            return _scale;
        }
        set
        {
            _scale = value;
            _scale = _scale < _minScale ? _minScale : _scale > _maxScale ? _maxScale : _scale;
            Time.timeScale = _scale;
            _isPaused = _scale <= 0.001f;
            if (_isPaused)
            {
                _scale = 0f;
                _willPause = false;
            }
        }
    }

    /// <summary>
    /// Pause toggle (Changes the "IsPaused" flag from true to false and vice-versa)
    /// </summary>
    /// </param>
    /// <param name='time'>
    /// Time until Pause or Play
    /// </param>
    /// <param name='playScale'>
    /// Play scale.
    /// </param>
    public void TogglePause(float time = 0f, float playScale = -1f)
    {
        StopStepper();
        // WillPause == true means that a "Pause" was already called: this will make "WillPause" change to false and call "Play" function. 
        // Else just toggle.
        _willPause = _willPause == true ? false : !_isPaused;
        if (_willPause)
        {
            Pause(time);
        }
        else
        {
            Play(time, playScale);
        }
    }

    void StopStepper()
    {
        _instance.StopCoroutine("FadeStepper");
    }

    /// <summary>
    /// CustomTimeManager Pause
    /// </summary>
    /// <param name='time'>Fading time until Time.timeScale == 0f</param>
    public void Pause(float time = 0f)
    {
        if (Mathf.Approximately(time, 0f))
        {
            _willPause = false;
            _instance.StopCoroutine("FadeStepper");
            Scale = 0f;
        }
        else
        {
            _willPause = true;
            FadeTo(0f, time);
        }
    }

    /// <summary>
    /// CustomTimeManager Play 
    /// </summary>
    /// <param name='time'>
    /// Fading time until Time.timeScale == scale param
    /// </param>
    /// <param name='scale'>
    /// Final scale for Time.timeScale
    /// </param>
    public void Play(float time = 0f, float scale = 1f)
    {
        if (Mathf.Approximately(time, 0f))
        {
            _instance.StopCoroutine("FadeStepper");
            Scale = scale;
        }
        else
        {
            FadeTo(scale, time);
        }
    }

    /// <summary>
    /// CustomTimeManager Scale Fading.
    /// </summary>
    /// <param name='scale'>
    /// The final Time.timeScale
    /// </param>
    /// <param name='time'>
    /// The transition time to reach the desired scale
    /// </param>
    public void FadeTo(float scale, float time)
    {
        _instance.StopCoroutine("FadeStepper");
        _scaleToFade = scale;
        _fadeToScaleDifference = scale - _scale;
        _fadeToScaleIsGreater = _fadeToScaleDifference > 0f;
        float scalePerFrame = _fadeToScaleDifference / time;
        _instance.StartCoroutine("FadeStepper", scalePerFrame);
    }

    /// <summary>
    /// Coroutine to fade the Unity's timeScale
    /// </summary>
    IEnumerator FadeStepper(float scalePerFrame)
    {
        bool achieved = false;
        _isFading = true;
        while (achieved == false)
        {
            Scale += scalePerFrame * _deltaTime;
            if (_fadeToScaleIsGreater)
            {
                achieved = _scale >= _scaleToFade;
            }
            else
            {
                achieved = _scale <= _scaleToFade;
            }
            yield return null;
        }
        Scale = _scaleToFade;
        _isFading = false;
        _willPause = false;
    }

    /// <summary>
    /// Updating our internal _deltaTime
    /// </summary>
    IEnumerator UpdateDeltaTime()
    {
        while (true)
        {
            float timeSinceStartup = Time.realtimeSinceStartup;
            _deltaTime = timeSinceStartup - _lastTime;
            _lastTime = timeSinceStartup;
            yield return null;
        }
    }

}



출처 : http://fliperamma.com/timescale-lerp-custom-time-manager/

반응형
Posted by blueasa
, |