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