[英]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: 为了暂时实现此功能,我正在执行以下操作:
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.