【问题标题】:adding GameObject at runtime在运行时添加 GameObject
【发布时间】: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 个。我需要将占位符放在另一个列表或数组中吗?我有点迷路了。

【问题讨论】:

标签: c# unity3d gameobject unity3d-2dtools


【解决方案1】:

如果你已经创建了 5 个新对象,你可以像他们在这里做的那样做: http://unity3d.com/learn/tutorials/modules/beginner/scripting/invoke 在 InvokeScript 下

foreach(GameObject gos in imglist)
{
    Instantiate(gos, new Vector3(0, 2, 0), Quaternion.identity);
}

【讨论】:

    【解决方案2】:

    我真的不明白你想要做什么,但如果我是正确的并且你有一个对象列表,并且你知道在运行时要将它们移动到哪里,只需制作两个列表, 一个包含对象和 一种包含场景中放置在这些预定义位置的空游戏对象的变换,并在运行时匹配它们。

    从检查器填充两个列表。

    public List<GameObject> imglist = new List<GameObject>();
    public List<Transform> imgPositions = new List<Transform>();
    
    
    void Start()
    {
        for(var i = 0 i < imglist.Count; ++i)
        {
            imglist[i].transform.position = imgPositions[i].position
        }
    }
    

    【讨论】:

    • 我自己找到了一个几乎相同的解决方案,我制作了一个占位符的 Vector3 列表(public static List coordinates = new List(); )并将 foreach 更改为 for所以我可以使用相同的迭代器读取两个列表。
    【解决方案3】:

    一般最好的方法是为您的对象创建预制件,将它们作为参数传递并在需要时实例化(在您的情况下开始)。这是常见的情况,但也许你的情况略有不同。

    这是一个传递预制数组并为数组中的每个对象实例化一个对象的示例:

    public GameObject prefabs[];
    List<GameObject> objects = new List<GameObject>();
    
    void Start() {
        for(GameObject prefab in prefabs) {
            GameObject go = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject; // Replace Vector3.zero by actual position
    
            objects.Add(go); // Store objects to access them later: total enemies count, restart game, etc.
        }
    }
    

    如果您需要同一预制件的多个实例(例如多个敌人或物品),只需修改上面的代码。

    【讨论】:

    • 以这种方式使用预制件对我来说还是很陌生的事情。感谢您的代码,我将对其进行测试和练习:)
    • 这很基本。你应该试试教程。它会让你的生活更轻松。祝你好运!
    猜你喜欢
    • 2014-09-13
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2019-11-02
    • 2019-06-07
    相关资源
    最近更新 更多