Unity - 在 android 构建中未正确设置粒子系统颜色

Unity - Particle system color not set properly in android build

我想在 运行 时间内设置我添加到游戏对象的 "Particle System" 的颜色。 游戏对象和粒子系统也是在 运行 时间内创建的。

当我 运行 它时,我有以下代码在 PC 上运行良好...... 但问题是在 "Android" 构建中,颜色设置不正确,粒子总是 "pink".

应该是: enter image description here

但在 Android 中它被视为: enter image description here

这是我使用的代码:

    GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);       
    sphere.transform.position = new Vector3(x, y, z);
    sphere.AddComponent<ParticleSystem>();
    var ps = sphere.GetComponent<ParticleSystem>();
    ps.startLifetime = 1;
    ps.startSpeed = 0.01f;
    ps.startSize = 0.03f;
    ps.maxParticles = 10000;
    ParticleSystemExtension.SetEmissionRate(ps, 10000);
    var sh = ps.shape;
    sh.shapeType = ParticleSystemShapeType.Circle;
    sh.radius = 0.69f;

    var cbl = ps.colorOverLifetime;
    cbl.enabled = true;
    Color mater = Color.green;
    cbl.color = new ParticleSystem.MinMaxGradient(mater);

天呐!我改变了我的解决方案,它工作正常!

首先,我创建了一个附加了 ParticleSystem 的空 GameObject,然后为其创建了一个预制件。之后,我将这个预制件添加到我的场景中并为其设置必要的属性,然后停用它。

在我的脚本中,添加一个 public 游戏对象并将其分配给我之前创建的游戏对象(称为 Ring)。

然后通过以下代码实例化:

GameObject ps = Instantiate(Ring);

然后使 ps 成为我在代码中创建的 sphere 的子代:

 Ring.GetComponent<ParticleSystem>().startColor = Color.green;
 GameObject ps = Instantiate(Ring);
 ps.transform.parent = sphere.transform;
 ps.transform.localPosition = Vector3.zero;
 ps.SetActive(true);

就是这样!效果不错!