C# 将游戏对象动态添加到 Unity3d 中的场景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15500137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add gameobject dynamically to scene in Unity3d
提问by Ashwani K
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:
我正在创建一个场景,我想在其中显示优惠列表。为了显示报价,我创建了一个预制件,其中包含我将在运行时获得的报价详细信息的占位符。我在场景中创建了一个占位符以将预制件添加到场景中,但它没有显示在 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.
我是 Unity 的新手,不确定我在这里缺少什么。
采纳答案by UnityFan2013
//Drag object prefab to variable in inspector
public GameObject spawnObject;
//----------------------------------------
Below will create GameObject using the objects OwnTransform settings.
下面将使用对象自己的变换设置创建游戏对象。
GameObject clone;
clone = Instantiate(spawnObject.transform,
spawnObject.transform.position,
spawnObject.transform.rotation) as GameObject;
Below will create GameObject using the objects ParentsTransform settings.
下面将使用对象的ParentsTransform设置创建GameObject 。
GameObject clone;
clone = Instantiate(spawnObject.transform,
transform.position,
transform.rotation) as GameObject;
Not sure if this helps, but good luck on your game :)
不确定这是否有帮助,但祝你游戏好运:)
回答by zhuchun
In Unity, you can do it like this.
在 Unity 中,您可以这样做。
GameObject.Instantiate(prefab,new Vector3(1,1,0),Quaternion.identity);
See also : http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
另见:http: //docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
Especially for Position, it must in front of your Camera, or you may not see it.
特别是Position,它必须在你的 Camera 前面,否则你可能看不到它。
What's more, I suggest you take a look to NGUI. It's a powerful GUI system with some useful API for develop. BTW I can't imagine how hard it is to develop games without such thing, so you may need it sooner or later ;\
更重要的是,我建议你看看NGUI。它是一个强大的 GUI 系统,带有一些用于开发的有用 API。顺便说一句,我无法想象没有这些东西开发游戏有多难,所以你迟早可能需要它;\
With it, you can do so easily.
有了它,您就可以轻松做到这一点。
Gameobject go = NGUITools.AddChild(Gameobject Parent, Gameobject Prefab)
UPDATE:
更新:
When I answered this, NGUI is the ONLYusable gui system, so I recommended it. However, there's an official Unity UI system(AKA uGUI) out there, you don't really have to use NGUI, leave alone the gui war is still continuing.
当我回答这个问题时,NGUI 是唯一可用的 gui 系统,所以我推荐了它。然而,那里有一个官方的 Unity UI 系统(AKA uGUI),你真的不必使用 NGUI,更不用说 gui 战争仍在继续。
What's more, you may want to take a took into pool system. It's used to handle massive gameobjects like bullets, cubes etc. If you have hundreds of specific gameobject in the same scene and suffering from instantiate, then you probably need a pool. Personally I tried FastPool and it works good, actually all assets of its kind work exactly the same.
更重要的是,您可能想要一个带入池系统。它用于处理大量游戏对象,如子弹、立方体等。如果您在同一场景中有数百个特定的游戏对象并遭受实例化,那么您可能需要一个池。我个人尝试过 FastPool 并且效果很好,实际上所有同类资产的工作方式完全相同。