0

我是unity 3d的新手,我必须在退出项目中做一些增强。如果用户选择正确的选项,那么我必须在运行时在按钮周围显示一些粒子。我添加粒子的代码如下..不工作:

ParticleSystem ps = GetComponent<ParticleSystem>();
ps.Play ();

我还从统一编辑器中添加了粒子组件..

提前致谢

编辑 :

正如@kardux 建议的那样:

宣言 :

[SerializeField] private ParticleSystem ps;

关于方法:

ps.Play()

检查员截图:

在此处输入图像描述

错误:

I/Unity   (23313): NullReferenceException
I/Unity   (23313):   at UnityEngine.ParticleSystem.<Play>m__0 (UnityEngine.ParticleSystem ps) [0x00001] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:3666 
I/Unity   (23313):   at UnityEngine.ParticleSystem.IterateParticleSystems (Boolean recurse, UnityEngine.IteratorDelegate func) [0x00003] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:3780 
I/Unity   (23313):   at UnityEngine.ParticleSystem.Play (Boolean withChildren) [0x00020] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:3666 
I/Unity   (23313):   at UnityEngine.ParticleSystem.Play () [0x00005] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:3661 
4

3 回答 3

2

首先,如果您在 Unity UI 中使用粒子,我强烈建议您查看Unity UI Extension存储库中的UIParticleSystem.cs脚本:这是一个包含许多有用 UI 工具的社区 :) (只是不要忘记添加UI /Particles/隐藏着色器,你可以在这里找到)

您可以在此处更改要使用的精灵: UIParticleSystem 组件设置

还请记住,在使用此脚本时,您必须根据屏幕缩放粒子(粒子的大小初始化为 1,因为在 Unity 3D 世界中这是 1 米:但现在您可能处于画布空间中,这将是像 1920x1080px 这样 1px 会非常小)。您可以在下面找到一些基本设置: ParticleSystem 组件设置

现在来到你的脚本,我怀疑你只需要像这样Stop()之前调用Play()(注意我在我的粒子系统设置中使用了爆发发射类型):

ParticleSystem ps = GetComponent<ParticleSystem>();
ps.Stop ();
ps.Play ();

P.-S。请注意,如果您使用UIParticleSystem脚本,您将不得不将您的粒子系统视为 UI 项(将根据层次顺序呈现在其他项之上)

希望这可以帮助,

编辑:
您有两种设置GameObjects 的方法:

  • 您在同一个 GameObject 上拥有所有组件 ParticleSystem UIParticleSystemYOUR_SCRIPT :这样您可以通过在脚本中调用来获取ParticleSystem引用GetComponent<ParticleSystem>()

  • 您有一个粒子GameObject(带有ParticleSystemUIParticleSystem)并且YOUR_SCRIPT在另一个GameObject上:您不能GetComponent<ParticleSystem>()在脚本中调用,因为它会搜索此GameObject的组件,因此您声明一个ParticleSystem ps;变量(或public[SerializeField] private),您通过通过将粒子GameObject拖到检查器上。

请注意,GetComponent<ParticleSystem>()等于隐含地this.gameObject.GetComponent<ParticleSystem>():这就是它会从当前GameObject中搜索组件的原因。

编辑 2:
不知道为什么你的脚本会抛出这个NullReference异常:我只是尝试了一个非常短的脚本,它工作得很好......

public class TestScript: MonoBehaviour
{
    [SerializeField]
    private ParticleSystem ps;

    void Start()
    {
        // This one is not even needed
        ps.Stop();
    }

    public void PlayParticles()
    {
        ps.Stop();
        ps.Play();
    }
}
于 2017-06-08T13:29:31.167 回答
0

如果您在调用它的脚本的同一个游戏对象上拥有一个粒子系统,那么应该没问题。

你在使用 UI 按钮吗?如果是这样,看看这里.. http://answers.unity3d.com/questions/852397/particle-system-in-46-ui.html

它很旧,但仍然相关。

于 2017-06-08T10:16:00.113 回答
0

您使用的是新的 Unity UI 系统还是 GUI,是 UI 世界空间吗?

  1. 创建一个空的游戏对象 - 将粒子附加到该对象上。
  2. 每当你想发射粒子调用Gameobject.SetActive(true);
  3. 确保在粒子系统中选中 Play on Awake 选项。

根据您的 UI 设置粒子的位置。

于 2017-06-08T10:25:34.957 回答