2017-01-07 6 views
1

私のUnityゲームでは地形の表面にさまざまなオブジェクトを配置することができ、新しいオブジェクトが置かれたらファイルにタイムスタンプを保存する必要があります。私は節約の仕組みを働かせていました。私はサーバーからタイムスタンプを得ることができました。これは、サーバーからデータを取得するためにIEnumeratorを使用する必要がありますが、これは私のために新たな問題を作成したようです。 IEnumeratorは関数ではないので、単に何も返すことはできません。そして何も返すことができないので、IEnumeratorから要求された場所にタイムスタンプを戻すための回避策が必要です。どのように私はタイムスタンプを取得することができるだろう計画:IEnumeratorを何とか返すようにするための回避策

int _timestamp = ServerTime.Get(); 

だから私は簡単にここで

gameObject.GetComponent<_DrillStats>().timestamp = _timestamp; 
gameObject.GetComponent<_OtherStats>().timestamp = _timestamp; 
gameObject.GetComponent<_EvenMoreStats>().timestamp = _timestamp; 

のようにタイムスタンプを受信するための完全なコードがあり、それを保存することができます:

using UnityEngine; 
using System; 
using System.Collections; 

public class ServerTime : MonoBehaviour 
{ 
    private static ServerTime localInstance; 
    public static ServerTime time { get { return localInstance; } } 

    private void Awake() 
    { 
     if (localInstance != null && localInstance != this) 
     { 
      Destroy(this.gameObject); 
     } 
     else 
     { 
      localInstance = this; 
     } 
    } 

    public static void Get() 
    { 
     if (time == null) 
     { 
      Debug.Log("Script not attached to anything"); 
      GameObject obj = new GameObject("TimeHolder"); 
      localInstance = obj.AddComponent<ServerTime>(); 
      Debug.Log("Automatically Attached Script to a GameObject"); 
     } 

     time.StartCoroutine(time.ServerRequest()); 
    } 

    IEnumerator ServerRequest() 
    { 
     WWW www = new WWW("http://www.businesssecret.com/something/servertime.php"); 

     yield return www; 

     if (www.error == null) 
     { 
      int _timestamp = int.Parse (www.text); 

      // somehow return _timestamp 
      Debug.Log (_timestamp); 
     } 
     else 
     { 
      Debug.LogError ("Oops, something went wrong while trying to receive data from the server, exiting with the following ERROR: " + www.error); 
     } 

    } 

} 
+0

これは[本]の重複である(http://stackoverflow.com/a/30270515/3785314)。 'Action'パラメータの答えはあなたが探しているものです。 – Programmer

+0

に変更: 'time.StartCoroutine(time.ServerRequest(WWW、(ステータス)=> { プリント(status.ToString());} ));' と 'のIEnumerator ServerRequest(WWW WWW、アクションコールバック) \t { \t \t yield return www; \t \t場合(www.error == NULL) \t \t { \t \t \t INT _timestamp = int.Parse(www.text)。 \t \t \tコールバック(_timestamp); \t \t} \t他\t ( "次のエラーで終了する、サーバからデータを受信しようとしたときおっと、何かが間違っていた:" + www.error)\t \t { \t \t \t Debug.LogError。 \t \t} \t} ' しかし、私はそれを必要な他のスクリプトに戻って与えられたタイムスタンプを渡すことができますどのように私はまだ理解していません。 – Jonathan

+0

私は上記のフォーマット方法がわかりません – Jonathan

答えて

3

これは、私がコメント欄にどのような意味です。

USGAE

を有するServerTime.Get();置換:

ServerTime.Get((myTimeStamp) => 
{ 
    Debug.Log("Time Stamp is: " + myTimeStamp); 

    gameObject.GetComponent<_DrillStats>().timestamp = myTimeStamp; 
    gameObject.GetComponent<_OtherStats>().timestamp = myTimeStamp; 
    gameObject.GetComponent<_EvenMoreStats>().timestamp = myTimeStamp; 
}); 
1

タイムスタンプはフィールドに保存し、コルーチンが完了した後にアクセスすることができます。

boolフラグを使用してwhileループを使用すると、コルーチンが終了するのを待つことができます。私は、両方の関数がパラメータとしてAction<int>を取る作っ

public static void Get(Action<int> callback) 
{ 
    if (time == null) 
    { 
     Debug.Log("Script not attached to anything"); 
     GameObject obj = new GameObject("TimeHolder"); 
     localInstance = obj.AddComponent<ServerTime>(); 
     Debug.Log("Automatically Attached Script to a GameObject"); 
    } 

    time.StartCoroutine(time.ServerRequest(callback)); 
} 

IEnumerator ServerRequest(Action<int> callback) 
{ 
    WWW www = new WWW("http://www.businesssecret.com/something/servertime.php"); 

    yield return www; 

    if (www.error == null) 
    { 
     int _timestamp = int.Parse(www.text); 

     // somehow return _timestamp 
     callback(_timestamp); 

     Debug.Log(_timestamp); 
    } 
    else 
    { 
     Debug.LogError("Oops, something went wrong while trying to receive data from the server, exiting with the following ERROR: " + www.error); 
    } 
} 

+0

'time.StartCoroutine(time.ServerRequest());の後でwhileループを追加することを意味しますか? は、Coroutineの実行中にフリーズする原因になりませんか? – Jonathan

関連する問題