简体   繁体   English

如何在屏幕中间实例化一个预制件?

[英]How do I instantiate a prefab in the middle of the screen?

My game requires a prefab to be instantiated in the middle of the screen.我的游戏需要在屏幕中间实例化一个预制件。 Currently I have the code set up like this:目前我的代码设置如下:

 Instantiate(_Confirm,new Vector3(Screen.width/2,Screen.height/2,0),Quaternion.identity);

However, when I run the code, I find that it object has been instantiated far away from my viewport.但是,当我运行代码时,我发现它 object 已在远离我的视口的地方被实例化。

EDIT: It's a 2D game编辑:这是一个 2D 游戏

This should work:这应该有效:

Vector2 spawnPos = Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 0.5f));
Instantiate(_Confirm, spawnPos, Quaternion.identity);

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

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