2016-06-27 4 views
-1

私は最初のプロジェクトをUnity3Dにしています。これは2Dのゲームです。これは何らかの種類のrunnerですが、プレイヤーがある程度遠ざかる可能性があります。瞬間のためにこの機能を実現するために、私はこのような何かやってる:Unity3D非同期ゲームオブジェクトのインスタンス化

  • がコンテンツを持つ2つの画面を作成する(最初のプレーヤーexceedes最初の画面の後に、それを示すために二、ゲーム開始時に参照するため)
  • プレイヤーが次の画面に移動したとき、現在の画面の位置とサイズを計算して新しいものを作成しています(開始から2秒、プレーヤーが2回目に行くと3回目が作成されます)

私は自分のPCでテストしていましたが、すべてうまくいきましたが、何らかの理由で次の画面が作成されているときに電話が遅れる第二のように、今どのようにコードが何を行く:

スクリプトのStart()方法では、私は2つのシーンの初期化しています:

Scene scene = new Scene(); 
    scene.setSceneBounds (screenBounds); 
    scene.createBackground (cameraOffsetOnStart, sceneSize); 
    scene.createContent(); 

    sceneNumber++; 
    currentScenePosition = sceneSize * sceneNumber; 
    Vector2 nextScenePosition = new Vector2 (cameraOffsetOnStart.x + currentScenePosition.x, cameraOffsetOnStart.y); 

    Scene scene2 = new Scene(); 
    screenBounds.min = new Vector2(min.x + currentScenePosition.x, min.y); 
    screenBounds.max = new Vector2(max.x + currentScenePosition.x, max.y); 
    scene2.setSceneBounds (screenBounds); 
    scene2.createBackground (nextScenePosition, sceneSize); 
    scene2.createContent(); 

をそしてUpdate()に、私はプレイヤーexceedes現在のシーンかどうかをチェックし、作成しています新しい1:

void Update() { 
    if (player.transform.position.x - playerOffset > sceneNumber * (max.x - min.x)) { 
     Debug.Log("current scene is : " + (++sceneNumber)); 
     currentScenePosition = sceneSize * sceneNumber; 
     Vector2 nextScenePosition = new Vector2 (cameraOffsetOnStart.x + currentScenePosition.x, cameraOffsetOnStart.y); 
     Scene scene = new Scene(); 
     screenBounds.min = new Vector2(min.x + currentScenePosition.x, min.y); 
     screenBounds.max = new Vector2(max.x + currentScenePosition.x, max.y); 
     scene.setSceneBounds (screenBounds); 
     scene.createBackground(nextScenePosition, sceneSize); 
     scene.createWebs(); 
     sceneManager.Scenes.Add(scene); 
    } 
} 

やコンテンツを作成するためのコード:

public void createBackground(Vector2 position, Vector2 size) { 
    background = new Background (position, size); 
} 

public void createContent() { 
    Vector2[] positions = Utilities.generateRandomPositions(5, sceneBounds, 4f); 

    for (int i = 0; i < positions.Length; i++) { 
     Web web = ScriptableObject.CreateInstance<Web>(); 
     web.init(positions[i]); 
    } 
} 

遅れの問題は、createContentメソッドから発生します。 initのためのコード:

public void init(Vector2 position) { 
     if (position != Vector2.zero) { 
      obj = Instantiate (Resources.Load ("Textures/web", typeof(GameObject)), position, Quaternion.identity) as GameObject; 
     } 
    } 

それはInstantiate方法、5オブジェクトの行で5回を呼び出すには、このbehviourを引き起こしていることは明らかです。必要に応じて、「テクスチャ/ウェブ」上

詳細:それが唯一の5項目に遅れている理由:これは

質問動に設定され、円のコライダーと剛体とプレハブのですか? Instantiateを間違った方法で使用していますか?どうすれば速くすることができますか?それを非同期と呼ぶ方法はありますか?

+0

私は 'Resources.Load'操作を覚えているので、' Instantiate'には時間がかかりました。可能であれば、例えば 'Stat'に' GameObject'をロードして、必要なときにこのオブジェクトをインスタンス化してみてください。 –

+0

@PawełMarecki、基本的に、あなたが言っていることは、同じリソースを何度も何度も読み込んでいるということですか?ああ、それは悪いです、私はavareiable GameObjectとして一度だけ、初期化されたようにしようとします。 –

+0

@PawełMarecki、うわー、本当にありがとう、あなたは実際に私の問題を解決した、 times =( –

答えて

2

コメントで説明しました。ラインで

obj = Instantiate (Resources.Load ("Textures/web", typeof(GameObject)), position, Quaternion.identity) as GameObject; 

あなたはデバイスメモリからあなたはこのコードを呼び出すたびにリソースをロードします。 GameObjectをいくつかの変数、例えばStart()メソッドに格納してください。

関連する問題