c# - 将游戏对象动态添加到 Unity3d 中的场景

标签 c# unity3d gameobject ngui

我正在创建一个场景,我想在其中显示优惠列表。为了显示报价,我创建了一个带有占位符的预制件,用于我将在运行时获得的报价详细信息。我在场景中创建了一个占位符以将预制件添加到场景中,但它没有显示在 UI 上。 OfferHolderClass:

using UnityEngine;
using System.Collections;

public class OfferHolder : MonoBehaviour {

    public GameObject localOffer;
    // Use this for initialization
    void Start () {
        GameObject offer = Instantiate(localOffer) as GameObject;
        offer.GetComponent<Offer>().Text = "Testing";
        offer.transform.parent = this.transform;
    }

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

    }
}

我是 Unity 的新手,不确定我在这里遗漏了什么。

最佳答案

//Drag object prefab to variable in inspector
public GameObject spawnObject;
//----------------------------------------

下面将使用对象 Own 转换设置创建 GameObject。

 GameObject clone;
    clone = Instantiate(spawnObject.transform, 
                        spawnObject.transform.position, 
                        spawnObject.transform.rotation) as GameObject;

下面将使用对象 Parents 转换设置创建 GameObject。

 GameObject clone;
    clone = Instantiate(spawnObject.transform, 
                        transform.position, 
                        transform.rotation) as GameObject;

不确定这是否有帮助,但祝你游戏顺利:)

关于c# - 将游戏对象动态添加到 Unity3d 中的场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15500137/

相关文章:

c# - 为什么 Unity 在某些函数(例如 GetChild())中使用变换而不是游戏对象?

c# - 仅当文件存在时才加载资源字典

c# - CAPICOM TripleDES 和 System.Security.Cryptography TripleDES 之间的区别

c# - DbContext 丢弃更改而不处理

audio - 立方体撞墙时发出统一声音

swift - 室内导航系统利用计算机视觉进行大面积测绘的解决方案是什么?

c# - 如何强制重绘我的场景?

c# - 在拖放游戏中匹配对象

c# - 产品生命周期的 SQL Server View 创建

unity-game-engine - GameObject.FindGameObjectWithTag 返回(克隆)?