粒子统一创建星场
Particles to create star field in unity
我的脚本有问题。我正在尝试为我的统一场景在一个球体中随机创建一个星域。但我是 unity 和 c# 的新手,所以我有点困惑。
星星有一个固定的位置,所以它们不应该移动,所以是在 Start() 中创建的;然后在 Update();
中绘制
问题是我得到这个错误:
MissingComponentException: There is no 'ParticleSystem' attached to the "StarField" game object, but a script is trying to access it.
You probably need to add a ParticleSystem to the game object "StarField". Or your script needs to check if the component is attached before using it.
Stars.Update () (at Assets/Stars.cs:31)
如果我手动添加一个粒子系统组件,它会导致加载大量闪烁的橙色斑点,这是我不想要的,所以我想以某种方式在脚本中添加该组件。
这是我附加到空游戏对象的脚本:
using UnityEngine;
using System.Collections;
public class Stars : MonoBehaviour {
public int maxStars = 1000;
public int universeSize = 10;
private ParticleSystem.Particle[] points;
private void Create(){
points = new ParticleSystem.Particle[maxStars];
for (int i = 0; i < maxStars; i++) {
points[i].position = Random.insideUnitSphere * universeSize;
points[i].startSize = Random.Range (0.05f, 0.05f);
points[i].startColor = new Color (1, 1, 1, 1);
}
}
void Start() {
Create ();
}
// Update is called once per frame
void Update () {
if (points != null) {
GetComponent<ParticleSystem>().SetParticles (points, points.Length);
}
}
}
我如何设置它以获得静态星域,因为手动添加粒子系统组件会给我这些恼人的橙色粒子,我想纯粹通过脚本来完成。
如果您手动添加粒子系统并更改设置,这样您就不会在运行时或在编辑器中看到任何有趣的形状,这对您来说会更容易。
附带说明一下,您不需要在更新中的每一帧都设置粒子。即使您这样做了,调用 GetComponent 的开销也很大,因此您应该将 ParticleSystem
保存为 Start()
方法中 class 的一个字段。
这是一些对我有用的修改代码:
using UnityEngine;
public class Starfield : MonoBehaviour
{
public int maxStars = 1000;
public int universeSize = 10;
private ParticleSystem.Particle[] points;
private ParticleSystem particleSystem;
private void Create()
{
points = new ParticleSystem.Particle[maxStars];
for (int i = 0; i < maxStars; i++)
{
points[i].position = Random.insideUnitSphere * universeSize;
points[i].startSize = Random.Range(0.05f, 0.05f);
points[i].startColor = new Color(1, 1, 1, 1);
}
particleSystem = gameObject.GetComponent<ParticleSystem>();
particleSystem.SetParticles(points, points.Length);
}
void Start()
{
Create();
}
void Update()
{
//You can access the particleSystem here if you wish
}
}
这是星域的屏幕截图,其中包含粒子系统中使用的设置。请注意,我关闭了 looping
和 play on awake
。
我的脚本有问题。我正在尝试为我的统一场景在一个球体中随机创建一个星域。但我是 unity 和 c# 的新手,所以我有点困惑。
星星有一个固定的位置,所以它们不应该移动,所以是在 Start() 中创建的;然后在 Update();
中绘制问题是我得到这个错误:
MissingComponentException: There is no 'ParticleSystem' attached to the "StarField" game object, but a script is trying to access it.
You probably need to add a ParticleSystem to the game object "StarField". Or your script needs to check if the component is attached before using it.
Stars.Update () (at Assets/Stars.cs:31)
如果我手动添加一个粒子系统组件,它会导致加载大量闪烁的橙色斑点,这是我不想要的,所以我想以某种方式在脚本中添加该组件。
这是我附加到空游戏对象的脚本:
using UnityEngine;
using System.Collections;
public class Stars : MonoBehaviour {
public int maxStars = 1000;
public int universeSize = 10;
private ParticleSystem.Particle[] points;
private void Create(){
points = new ParticleSystem.Particle[maxStars];
for (int i = 0; i < maxStars; i++) {
points[i].position = Random.insideUnitSphere * universeSize;
points[i].startSize = Random.Range (0.05f, 0.05f);
points[i].startColor = new Color (1, 1, 1, 1);
}
}
void Start() {
Create ();
}
// Update is called once per frame
void Update () {
if (points != null) {
GetComponent<ParticleSystem>().SetParticles (points, points.Length);
}
}
}
我如何设置它以获得静态星域,因为手动添加粒子系统组件会给我这些恼人的橙色粒子,我想纯粹通过脚本来完成。
如果您手动添加粒子系统并更改设置,这样您就不会在运行时或在编辑器中看到任何有趣的形状,这对您来说会更容易。
附带说明一下,您不需要在更新中的每一帧都设置粒子。即使您这样做了,调用 GetComponent 的开销也很大,因此您应该将 ParticleSystem
保存为 Start()
方法中 class 的一个字段。
这是一些对我有用的修改代码:
using UnityEngine;
public class Starfield : MonoBehaviour
{
public int maxStars = 1000;
public int universeSize = 10;
private ParticleSystem.Particle[] points;
private ParticleSystem particleSystem;
private void Create()
{
points = new ParticleSystem.Particle[maxStars];
for (int i = 0; i < maxStars; i++)
{
points[i].position = Random.insideUnitSphere * universeSize;
points[i].startSize = Random.Range(0.05f, 0.05f);
points[i].startColor = new Color(1, 1, 1, 1);
}
particleSystem = gameObject.GetComponent<ParticleSystem>();
particleSystem.SetParticles(points, points.Length);
}
void Start()
{
Create();
}
void Update()
{
//You can access the particleSystem here if you wish
}
}
这是星域的屏幕截图,其中包含粒子系统中使用的设置。请注意,我关闭了 looping
和 play on awake
。