以单位设置实例化GameObject Sprite的z变换位置

问题描述 投票:0回答:1

是否有一种简单(较短)的方法来统一设置实例化游戏对象(子画面)的z变换位置?我现在想将每个实例设置为2。这是我正在使用的一行代码,我只是想设置z位置,以这种方式执行似乎很麻烦-忽略第一行,因为我仅包括它来演示如何实例化GameObject:

GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
laser.transform.position = new Vector3(laser.transform.position.x, laser.transform.position.y, 2);

提前感谢!

c# unity3d z-index instantiation
1个回答
0
投票

Vector3上有一些扩展方法,可能会使它更易于阅读。但是我认为它不会比您写的要短得多。

像Vector3.MoveTowards

https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

但是简短的答案可能是:No。

Vector(2/3)是一个结构。在这种情况下,它对用户是安全的,并成为完全只读的,因为您不能真正将值分配给结构,因为这将导致结构的新实例。因此,您无法进行laser.transform.position.z = 2这样的分配。您将始终必须创建一个新的Vector。

© www.soinside.com 2019 - 2024. All rights reserved.