简体   繁体   English

创建Unity对象列表

[英]Creating a List of Unity Objects

I'm trying to create a strip of sprites. 我正在尝试创建一条精灵。 I created a list of game objects and it seems to work ok. 我创建了一个游戏对象列表,看来工作正常。 Except I have a duplicate object in the game view, so it seemed like I need to destroy my "template object". 除了在游戏视图中有一个重复的对象外,因此似乎需要销毁“模板对象”。 When I do that it looks fine, but then when I try to move them it says I'm trying to access an object that has been destroyed. 当我这样做时,它看起来还不错,但是当我尝试移动它们时,它说我正在尝试访问已被破坏的对象。 Can someone help explain my issue? 有人可以帮忙解释我的问题吗? It seems that I'm confused on Instantiate and how it works exactly. 似乎我对实例化及其确切工作方式感到困惑。 I was under the impression that the object isn't created in the game view until the instantiate. 我的印象是,直到实例化该对象才在游戏视图中创建。 But then why do I get a duplicate object? 但是,为什么我要得到一个重复的对象? Thanks for your input. 感谢您的输入。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ReelStrip : MonoBehaviour {
    public List <Sprite> image = new List<Sprite>();
    public List <int> index = new List<int>();
    public List <GameObject> symbol = new List<GameObject> ();
    public int reelNumber;
    public float symbolHeight = 1.74f;
    public float symbolWidth = 2.00f;
    public float speed = 0.01f;


    // Use this for initialization
    void Start () {
        reelNumber = 0;
        loadStrip (new List<int>{0,0,1,2,4,1,4,5});
    }

    void addSymbol(int indexToAdd)
    { 
        string name = "reelNum: " + reelNumber + " index " + indexToAdd;
        SpriteRenderer renderer = new SpriteRenderer ();
        GameObject symbolObject = new GameObject (name);
        symbolObject.AddComponent<SpriteRenderer> ();
        renderer = symbolObject.GetComponent<SpriteRenderer> ();
        symbolObject.AddComponent<symbol> ();
        Sprite loadedSprite = Resources.Load <Sprite>(symbolNameAtIndex(indexToAdd)) as Sprite;
        renderer.sprite = loadedSprite;
        symbol.Add (symbolObject);

        Vector3 newPos;
        if(symbol.Count<2)
        {
            newPos = new Vector3(0.0f,0.0f,0.0f);
                }
                else{
            newPos = new Vector3(symbol[symbol.Count-1].transform.position.x,symbol[symbol.Count-1].transform.position.y + symbolHeight, 0.0f);
            }
        Instantiate (symbol[symbol.Count-1], newPos, Quaternion.identity);
        // destroy template object


///// confused on the destroy //////
        //Destroy (symbolObject);



    }

    public void moveStripDown(float delta)
    {
        for (int i=0;i<symbol.Count;i++)
        {
            symbol[i].transform.position = new Vector3(symbol[i].transform.position.x,symbol[i].transform.position.y - delta, symbol[i].transform.position.z);

        }

    }

    public void loadStrip(List <int> Indexes)
    {
        for (int i=0;i<Indexes.Count;i++)
        {
            addSymbol (Indexes[i]);
        }

    }

    public string symbolNameAtIndex(int index)
    {
        string returnString;
    switch (index) {
        case 0:
            returnString = "img1";
            break;
        case 1:
            returnString = "img2";
            break;
        case 2:
            returnString = "img3";
            break;
        case 3:
            returnString = "img4";
            break;
        case 4:
            returnString = "img5";
            break;
        default:
            returnString = "symbolnotfound";
            break;



        }

        return returnString;
    }

    // Update is called once per frame
    void Update () {

        moveStripDown (speed*Time.deltaTime);
    }
}

Since you have added the symbolObject to a list and later destroy the same symbolObject, the added object no longer exists when you try to move (although the reference stays in the list). 由于您已将symbolObject添加到列表中,并在以后销毁了相同的symbolObject,因此当您尝试移动时,添加的对象将不再存在(尽管引用保留在列表中)。

GameObject():GameObject creates a new GameObject in the hierarchy and Instantiate(original:Object):Object clones an object. GameObject():GameObject在层次结构中创建一个新的GameObject,Instantiate(original:Object):Object克隆一个对象。 When a GameObject is created or cloned, it will also appear in the hierarchy. 创建或克隆GameObject时,它也会出现在层次结构中。

To get your code working, add the instantiated object to the list and then destroy the game object. 为了使代码正常工作,请将实例化的对象添加到列表中,然后销毁游戏对象。

symbol.Add (Instantiate (symbol[symbol.Count-1], newPos, Quaternion.identity));
Destroy (symbolObject);

an alternative would be to skip the Instantiate step completely since you create a new game object every time. 另一种选择是完全跳过实例化步骤,因为每次您都创建一个新的游戏对象。

However, I would recommend that you keep your game object as a prefab and load it from the resource folder and use Instantiate on said object. 但是,我建议您将游戏对象保留为预制对象,并从资源文件夹中加载它,并在该对象上使用实例化。 Read the folloiwng link for how to use the resources load folder (2nd example also shows how to use Instantiate for this) http://docs.unity3d.com/ScriptReference/Resources.Load.html 阅读folloiwng链接,了解如何使用资源加载文件夹(第二个示例还显示了如何使用实例化) http://docs.unity3d.com/ScriptReference/Resources.Load.html

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

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