c# - 粒子系统附加到游戏对象时为粉红色

标签 c# unity3d 3d game-development

我正在研究统一粒子系统。

我创建了一个新项目和一个空对象,然后向其中添加了粒子系统。由于某种原因,它无法正常工作,您可以查看附加的图像以了解发生了什么......

enter image description here

最佳答案

由于您创建粒子的方式,问题是缺少 Material

创建粒子系统有两种方法:

1.创建空的游戏对象,选择它然后转到组件 --> 效果并添加粒子系统 组件到那个空的游戏对象。这就是您创建当前粒子系统的方式。

如果您使用方法#1 创建您的粒子系统,Unity 将不会将 Material 附加到粒子系统,因此使其变成粉红色。您必须创建一个新 Material ,将着色器更改为“Particles/Alpha Blended Premultiply”,并使用“Default-Particle”作为纹理以使粒子看起来像默认 Material 。

您也可以只为粒子系统使用“默认 Material ”,但您不能修改它。

enter image description here

2。通过转到 GameObject ---> Effects ---> Particle System 创建粒子.

如果您使用方法#2 创建粒子系统,Unity 创建新的游戏对象,附加一个粒子系统和还有一个 Material

总是通过转到 GameObject ---> Effects ---> Particle System 来创建你的 Material 。这将为您节省一些时间。

简单的解决方案是删除当前的粒子游戏对象,通过转到游戏对象 ---> 效果 ---> 粒子系统 而不是 #1 中描述的方法。

如果您需要从代码创建粒子系统,那么按照我在方法#1 中所说的操作,但是通过脚本。以下是如何做到这一点:

void Start()
{
    createParticleSys();
}

void createParticleSys()
{
    //Create GameObject to hold the Particle System
    GameObject psObj = new GameObject("Particle System");
    //Add Particle System to it
    ParticleSystem ps = psObj.AddComponent<ParticleSystem>();

    //Assign material to the particle renderer
    ps.GetComponent<Renderer>().material = createParticleMaterial();
}

Material createParticleMaterial()
{
    //Create Particle Shader
    Shader particleShder = Shader.Find("Particles/Alpha Blended Premultiply");

    //Create new Particle Material
    Material particleMat = new Material(particleShder);

    Texture particleTexture = null;

    //Find the default "Default-Particle" Texture
    foreach (Texture pText in Resources.FindObjectsOfTypeAll<Texture>())
        if (pText.name == "Default-Particle")
            particleTexture = pText;

    //Add the particle "Default-Particle" Texture to the material
    particleMat.mainTexture = particleTexture;

    return particleMat;
}

关于c# - 粒子系统附加到游戏对象时为粉红色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50236252/

相关文章:

c# - 选择 List<T> 中带有元音的单词

C# List<MyObj> 删除方法

c# - 如何从 UI.InputField 获取文本?

ios - 将 SDL 的 View 嵌入到 UIViewController 中

c# - 使用 C# 创建 3D 矩形

c# - 在 Unity3d 中翻转 3D 游戏对象

c# - 使用 AvalonEdit 自定义超链接

c# - 如何重构顺序ifs?

3d - 将 .ply 文件导入到 unity 3D

c# - 有什么方法可以用大量可编写脚本的对象填充列表