在行动中产生精灵
Spawn sprites on action
我正在尝试制作一个 Pinata GameObject,当点击它时会爆发并提供可变数量的 Gift GameObjects,其中包含各种图像和行为。
我也不确定它的统一词汇是什么,以便在统一文档中查找。
有人可以帮我一下吗?谢谢!
有几种方法可以处理这个问题。
简单的方法就是使用Object.Instantiate,Object Instantiation就是你要找的词汇。
这将创建预定义 Unity 对象的副本,这可以是游戏对象或派生自 UnityEngine.Object 的任何其他对象,请查看文档以获取更多信息 https://docs.unity3d.com/ScriptReference/Object.Instantiate.html。
在您的情况下,您的 Pinata 将有一个预制件数组或列表。这些预制件是由您创建的,每个预制件都具有特定的行为和精灵。当 Pinata 爆裂时,您会在 Pinata 周围的随机位置实例化随机预制件,由您决定如何放置这些对象。
按照这些思路应该可以解决问题:
class Pinata : Monobehaviour
{
public GameObject[] pickupPrefabs;
public int numberOfItemsToSpawn; //This can be random
//any other variables that influence spawning
//Other methods
public void Burst()
{
for(int i = 0; i < numberOfItemsToSpawn; i++)
{
//Length - 1 because the range is inclusive, may return
//the length of the array otherwise, and throw exceptions
int randomItem = Random.Range(0, pickupPrefabs.Length - 1);
GameObject pickup = (GameObject)Instantiate(pickupPrefabs[randomItem]);
pickup.transform.position = transform.position;
//the position can be randomised, you can also do other cool effects like apply an explosive force or something
}
}
}
切记,如果您希望游戏保持一致,那么每个行为预制件都会有自己的预定义精灵,这不会是随机的。唯一随机化的是产卵和定位。
如果你确实想随机化精灵的行为,那么你必须将其添加到 Pinata class:
public class Pinata : Monobehaviour
{
//An array of all possible sprites
public Sprite[] objectSprites;
public void Burst()
{
//the stuff I mentioned earlier
int randomSprite = Random.Range(0, objectSprites.Length - 1);
SpriteRenderer renderer = pickup.GetComponent<SpriteRenderer>();
//Set the sprite of the renderer to a random one
renderer.sprite = objectSprites[randomSprite];
float flip = Random.value;
//not essential, but can make it more random
if(flip > 0.5)
{
renderer.flipX = true;
}
}
}
您可以使用 Unity random 来满足您所有的随机需求,https://docs.unity3d.com/ScriptReference/Random.html
希望这会引导您朝着正确的方向前进。
我正在尝试制作一个 Pinata GameObject,当点击它时会爆发并提供可变数量的 Gift GameObjects,其中包含各种图像和行为。
我也不确定它的统一词汇是什么,以便在统一文档中查找。
有人可以帮我一下吗?谢谢!
有几种方法可以处理这个问题。 简单的方法就是使用Object.Instantiate,Object Instantiation就是你要找的词汇。 这将创建预定义 Unity 对象的副本,这可以是游戏对象或派生自 UnityEngine.Object 的任何其他对象,请查看文档以获取更多信息 https://docs.unity3d.com/ScriptReference/Object.Instantiate.html。
在您的情况下,您的 Pinata 将有一个预制件数组或列表。这些预制件是由您创建的,每个预制件都具有特定的行为和精灵。当 Pinata 爆裂时,您会在 Pinata 周围的随机位置实例化随机预制件,由您决定如何放置这些对象。
按照这些思路应该可以解决问题:
class Pinata : Monobehaviour
{
public GameObject[] pickupPrefabs;
public int numberOfItemsToSpawn; //This can be random
//any other variables that influence spawning
//Other methods
public void Burst()
{
for(int i = 0; i < numberOfItemsToSpawn; i++)
{
//Length - 1 because the range is inclusive, may return
//the length of the array otherwise, and throw exceptions
int randomItem = Random.Range(0, pickupPrefabs.Length - 1);
GameObject pickup = (GameObject)Instantiate(pickupPrefabs[randomItem]);
pickup.transform.position = transform.position;
//the position can be randomised, you can also do other cool effects like apply an explosive force or something
}
}
}
切记,如果您希望游戏保持一致,那么每个行为预制件都会有自己的预定义精灵,这不会是随机的。唯一随机化的是产卵和定位。
如果你确实想随机化精灵的行为,那么你必须将其添加到 Pinata class:
public class Pinata : Monobehaviour
{
//An array of all possible sprites
public Sprite[] objectSprites;
public void Burst()
{
//the stuff I mentioned earlier
int randomSprite = Random.Range(0, objectSprites.Length - 1);
SpriteRenderer renderer = pickup.GetComponent<SpriteRenderer>();
//Set the sprite of the renderer to a random one
renderer.sprite = objectSprites[randomSprite];
float flip = Random.value;
//not essential, but can make it more random
if(flip > 0.5)
{
renderer.flipX = true;
}
}
}
您可以使用 Unity random 来满足您所有的随机需求,https://docs.unity3d.com/ScriptReference/Random.html
希望这会引导您朝着正确的方向前进。