将游戏对象动态添加到 Unity3d 中的场景 [英] Add gameobject dynamically to scene in Unity3d

查看:84
本文介绍了将游戏对象动态添加到 Unity3d 中的场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a scene in which I want to show list of offers. In order to show the offer, I created a prefab with placeholders for the offer details which I will get at runtime. I created a place holder in the scene to add the prefab to the scene, but it is not showing on the 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 的新手,不确定我在这里缺少什么.

I am new to Unity and am not sure what I am missing here.

推荐答案

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

下面将使用对象自己的变换设置创建游戏对象.

Below will create GameObject using the objects Own Transform settings.

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

下面将使用对象 Parents 变换设置创建游戏对象.

Below will create GameObject using the objects Parents Transform settings.

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

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

Not sure if this helps, but good luck on your game :)

这篇关于将游戏对象动态添加到 Unity3d 中的场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆