我正在制作砖破的游戏,我想显示与被球击中的砖相同的颜色。这是我的代码:
GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
ParticleSystem ps = smokePuff.GetComponent<ParticleSystem>();
ParticleSystem.MainModule psmain = ps.main;
psmain.startColor = gameObject.GetComponent<SpriteRenderer> ().color;
这是不起作用的,颗粒颜色显示为粉红色。如何修复它?
我正在使用Unity 5.6。
这是某些统一版本的错误。它应在Unity 2017.2中固定。发生的事情是,当您更改ParticleSystem
颜色时,它会失去其材料参考。
您可以在设置颜色后将统一更新为最新版本,也可以手动将其材料参考或新材料附加回ParticleSystem
。
public GameObject smoke;
void Start()
{
GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
ParticleSystem ps = smokePuff.GetComponent<ParticleSystem>();
ParticleSystem.MainModule psmain = ps.main;
psmain.startColor = gameObject.GetComponent<SpriteRenderer>().color;
//Assign that 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;
}
编辑:
关于创建粒子系统和粉红色粒子问题还有两件事:
1 。如果您从 component ---> 效果 ---> 粒子系统菜单中创建粒子系统,则Unity将不<</strong>将材料附加到粒子系统上,因此它将是粉红色的。您将必须使用上面的代码来创建新材料或从编辑器手动进行。如果您不这样做,您将获得粉红色的特定系统。
您的问题是我所描述的的参考错误。
2 。如果您从 gameObject创建粒子系统 ----> 效果 ---> ---> 粒子系统菜单,Unity将创建新的GameObject,请附加粒子系统及其材料。您不应该有粉红色粒子问题,除非我谈论过的粒子在修改颜色时失去材料参考的错误。