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

问题描述

我创建中,我想展示的报价列表中的场景。为了显示报价,我创建了一个预制与占位符优惠详情,我会在运行时。我在现场预制添加到场景中创造了一个占位符,但它没有显示在UI上。
OfferHolderClass:

 使用UnityEngine; System.Collections中使用
;

公共类OfferHolder:MonoBehaviour {

公共游戏物体localOffer;
//使用这个初始化
无效的start(){
游戏对象的报价=实例化(localOffer)作为游戏对象;
offer.GetComponent<提供>()文本=测试。
offer.transform.parent = this.transform;
}

//更新是每帧
无效更新调用一次(){

}
}

我是新来的团结,我不知道我是缺少在这里。


解决方案

  //拖动对象预制在检查
公共游戏物体spawnObject变量;
// ----------------------------------------

下面将创建一个使用对象的变换设置。游戏物体

 游戏物体的克隆; 
克隆=实例化(spawnObject.transform,
spawnObject.transform.position,
spawnObject.transform.rotation)作为游戏对象;



下面将使用对象的家长变换设置创建游戏物体。

 游戏物体的克隆; 
克隆=实例化(spawnObject.transform,
transform.position,
transform.rotation)作为游戏对象;



不知道这会有所帮助,但你的游戏的好运气:)


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 () {

    }
}

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;

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 05:28