Unity 3d 我如何在不知道我要使用哪个预制件的情况下在运行时启动预制件?

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

下面提供的代码似乎没有从文件中获取的预制名称生成对象

    void load()
        {
        string[] files = Directory.GetFiles(Application.persistentDataPath, "*.furniture");

                foreach (string file in files)
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    FileStream stream = new FileStream(file, FileMode.Open);
                    Classforsavingdatafurnitures data = formatter.Deserialize(stream) as Classforsavingdatafurnitures;
                    stream.Close();

                    // Create game object from prefab
                    GameObject prefab = Resources.Load<GameObject>(data.nameofprefab);
                    if (prefab != null)
                    {
                        GameObject obj = Instantiate(prefab, new Vector3(data.position[0], data.position[1], data.position[2]), Quaternion.Euler(data.rotation[0], data.rotation[1], data.rotation[2]));
                        obj.name = data.nameofobject;
                       
                    }
                }

        }

我希望它从文件名 ofprefab 中获取并创建游戏中对象的精确副本。这段代码应该从每个文件加载每个保存的对象 .furniture 。如果你需要进一步挖掘,这里是 github 的 repo https://github.com/piso101/Architect_N0

c# file unity3d binary gameobject
© www.soinside.com 2019 - 2024. All rights reserved.