【发布时间】:2015-02-12 09:37:00
【问题描述】:
这似乎是一个愚蠢的问题,但我坚持下去。我在列表中有 GameObjects (List<GameObject>),我想将它们添加到场景运行时,最好是在预定义的位置(如占位符或其他东西)。什么是这样做的好方法?我一直在网上搜索,但找不到任何可以解决这个问题的方法。到目前为止,这是我的代码:
public static List<GameObject> imglist = new List<GameObject>();
private Vector3 newposition;
public static GameObject firstGO;
public GameObject frame1;//added line
void Start (){
newposition = transform.position;
firstGO = GameObject.Find ("pic1");
frame1 = GameObject.Find ("Placeholder1");//added line
//this happens when a button is pressed
imglist.Add(firstGO);
foreach(GameObject gos in imglist ){
if(gos != null){
print("List: " + gos.name);
try{
//Vector3 temp = new Vector3 (0f, 0f, -5f);
Vector3 temp = new Vector3( frame1.transform.position.x, frame1.transform.position.y, -1f);//added line
newposition = temp;
gos.transform.position += newposition;
print ("position: " + gos.transform.position);
}catch(System.NullReferenceException e){}
}
}
}
如何将图片 (5) 放置在预定义的位置上?
//----------------
编辑:现在我可以将 1 个图像放置到占位符(透明 png)。由于某种原因,z 值无处不在,因此需要强制为 -1f,但这没关系。我将其他场景的图像添加到列表中,其中可以有 1-5 个。我需要将占位符放在另一个列表或数组中吗?我有点迷路了。
【问题讨论】:
-
我知道它适用于 Unity3D,但也许这可以帮助您解决问题:stackoverflow.com/questions/24899481/…
标签: c# unity3d gameobject unity3d-2dtools