基本上是否有一种节省时间的方法来创建一个函数,让你创建一个由函数参数指定的GameObject?
像:
public void thing_maker(string gameobject_name, string sprite_name, string rg_body_name)
答案 0 :(得分:1)
这个例子应该做你需要的。 它要求您有一个名为资源的文件夹,并且您要加载的精灵将位于该文件夹中。
另一种选择是对GameObject go = Instantite(SomePrefabName) as GameObject
进行new GameObject()
内嵌,以防您准备好预制件并且您只想更改其某些组件的值。
public GameObject thing_maker(string gameobject_name, string sprite_name, string rg_body_name)
{
GameObject go = new GameObject(gameobject_name);
SpriteRenderer sr = go.AddComponent<SpriteRenderer>();
sr.sprite = Resources.Load<Sprite>(sprite_name);
Rigidbody rb = go.AddComponent<Rigidbody>();
/* now if you want to change values of the components you can just do it
by accesing them directly. for instance: rb.isKinematic = true; will
change the isKinematic value of this rigidbody to true. */
return go;
}
答案 1 :(得分:1)
在我看来,你可以尝试这样的事情:
public void ThingMaker(string gameobject_name, Sprite spriteToDraw) {
GameObject newObj;
newObj= new GameObject(gameobject_name);
newObj.AddComponent<Rigidbody2D>();
newObj.AddComponent<SpriteRenderer>();
newObj.GetComponent<SpriteRenderer>().sprite = spriteToDraw;
}