简体   繁体   English

Unity3D异步游戏对象实例化

[英]Unity3D async game object instantiation

I'm making my first project in Unity3D right now. 我现在正在用Unity3D创建我的第一个项目。 This is a 2D game. 这是一个2D游戏。 This is some kind of a runner , but with possibility for player to go back on some distance. 这是一种runner ,但玩家有可能回到某个距离。 To achieve this functionality for the moment, i'm doing something like this: 为了暂时实现此功能,我正在执行以下操作:

  • Creating two screens with content(first to see at the start of the game, second to show it after player exceedes first screen) 创建两个带有内容的屏幕(第一个在游戏开始时看到,第二个在玩家超过第一个屏幕后显示)
  • When player goes on the next screen i'm calculating current screen position and size to create new one after it(so it's 2 from the start, and when player goes to the second one, third one is creating and so on) 当玩家进入下一个屏幕时,我正在计算当前屏幕的位置和尺寸以在其后创建一个新屏幕(因此从一开始就是2,而当玩家进入第二个屏幕时,第三个正在创建,依此类推)

When i was testing it on my PC, everything was fine, but for some reason, when next screen is creating it's causing my phone to lag for somewhat like a second, now how the code goes: 当我在PC上对其进行测试时,一切都很好,但是由于某种原因,当创建下一个屏幕时,这会使我的手机延迟大约一秒钟,现在代码是如何运行的:

In Start() method of the script i'm initializing two scenes: 在脚本的Start()方法中,我将初始化两个场景:

    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 ();

And then in Update() i'm checking if player exceedes current scene and creating new one: 然后在Update()我检查播放器是否超出当前场景并创建新场景:

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);
    }
}

And the code for creating content: 以及用于创建内容的代码:

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]);
    }
}

The problem of lagging comes from createContent method. 滞后的问题来自createContent方法。 Code for init : init代码:

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

It's obvious that Instantiate method, calling 5 times in a row for 5 object is causing this behviour. 很明显, Instantiate方法连续对5个对象调用5次会导致这种现象。

More details on "Textures/web" if needed: This is a prefab with circle collider and rigidbody, which set to be kinematic 如果需要,可以在“纹理/纤维网”上获得更多详细信息:这是一个带有圆形碰撞器和刚体的预制件,将设置为运动学的

Questions: Why it's lagging on only 5 items? 问题:为什么只剩下5个项目? Am i using Instantiate in a wrong way? 我使用错误的方式Instantiate了吗? How can i make it faster? 我怎样才能使其更快? Is there a way to call it async? 有没有一种方法可以称之为异步?

As discussed in comments. 如评论中所讨论。

In line: 排队:

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

You loading resources from devices memory each time you call this code. 每次调用此代码时,都从设备内存中加载资源。 Just store GameObject in some variable eg in Start() method. 只需将GameObject存储在某个变量中,例如在Start()方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM