2017-02-09 1 views
1

나는 여러 가지 방법으로 잘 작동하지만 지연 후에 만 ​​호출하고 싶습니다. 에 대해 다른 방법을 쓰는 것을 피하려면 나는 어쨌든 Invoke에 더 유익하다고 생각했습니다. 나는 의 timeScale을 제외하도록 메소드를 만들었으므로 사용자 정의 단축 함수를 사용하여 항상 Real seconds을 기다립니다.C# 사용자 정의 Invoke 메서드를 사용하여 다양한 다른 메서드 호출

WaitForRealSeconds :

public class WaitForRealSecondsClass 
{ 
    #region Wait for real seconds 

    public Coroutine WaitForRealSeconds(float aTime, MonoBehaviour mono) 
    { 
     return mono.StartCoroutine(_WaitForRealSeconds(aTime)); 
    } 
    private IEnumerator _WaitForRealSeconds(float aTime) 
    { 
     while (aTime > 0.0f) 
     { 
      aTime -= Mathf.Clamp(Time.unscaledDeltaTime, 0, 0.2f); 
      yield return null; 
     } 
    } 
    #endregion 
} 

내가 Invoke 광산의 Move function하고자하는 방법 :

public void InvokeAnim(float timeBeforeStart, Action<MonoBehaviour> MoveFunction, MonoBehaviour mono) 
{ 
    if (moveRoutine != null) 
    { 
     mono.StopCoroutine(moveRoutine); 
    } 
    moveRoutine = _InvokeAnim(timeBeforeStart, MoveFunction, mono); 
} 
IEnumerator _InvokeAnim(float timeBeforeStart, Action<MonoBehaviour> MoveFunction, MonoBehaviour mono) 
{ 
    yield return new WaitForRealSecondsClass().WaitForRealSeconds(timeBeforeStart, mono); 
    MoveFunction(mono); 
} 

그리고 Move(MonoBehaviour mono) 자체가 : 내가 시험하고 일을 무엇

public void Move(MonoBehaviour mono) 
{ 
    if (moveRoutine != null) 
    { 
     mono.StopCoroutine(moveRoutine); 
    } 
    moveRoutine = _Move(from, to, overTime, mono); 
    mono.StartCoroutine(moveRoutine); 
} 

입니다 Move 자체, WaitForRealSeconds 다른 프로젝트에서 게임이 중단되었을 때 UI 대기 중으로 사용되었습니다. 내가 호출 할 메소드가 많았으므로, 모두 void을 리턴하고 파라미터는 MonoBehaviour입니다. 현재는 아무 것도하지 않고 이유를 모르겠습니다.

+0

,'방법 ** 유니티 API 내에서 ** 그래서 당신은 단지 호출 할 수 있습니다 'Invoke ("MeFancyMethodName", 0.5);와''MeFancyMethodName'은'0.5' second delay 후에 호출됩니다. –

+0

예,하지만 ** 나는 그 방법의 자손이 아니기 때문에 ** 'Monobehaviour'를 통과시켜야합니다. AFAIK'Invoke ("Name", time)'메서드는 void Name()을 호출 할 수 있습니다. ** 편집 : ** 그리고 나는 이러한 방법을 만들기 위해 상속권이 필요했기 때문에 'MonoBehaviours'가 될 수 없습니다. – agiro

+0

맞습니다. 그러나 여전히 당신은 _ "래퍼 (wrapper)"_를 만들어서 메소드를 호출 할 수 있습니다. 그런 다음 이전에 지정된 매개 변수로 메소드를 호출합니다. –

답변

3

축축하게 말하면, 나는 빗방울이되어 실제로 coroutine을 시작한다는 것을 잊어 버렸다. 에서

Invoke :

모든 위대한하지만 .. (시간을 떠, 문자열 methodName로) 이미이`공공 무효 호출있다
 public void InvokeAnim(float timeBeforeStart, Action<MonoBehaviour> MoveFunction, 
     MonoBehaviour mono) 
    { 
     if (moveRoutine != null) 
     { 
      mono.StopCoroutine(moveRoutine); 
     } 
     moveRoutine = _InvokeAnim(timeBeforeStart, MoveFunction, mono); 
     mono.StartCoroutine(moveRoutine); //one line was missing 
    } 
    IEnumerator _InvokeAnim(float timeBeforeStart, Action<MonoBehaviour> MoveFunction, 
     MonoBehaviour mono) 
    { 
     yield return new WaitForRealSecondsClass().WaitForRealSeconds(timeBeforeStart, mono); 
     MoveFunction(mono); 
    } 
관련 문제