指定一个纹理来为粒子的起始颜色着色。
要为粒子的起始颜色着色,形状模块会在 CPU 上读取此纹理中的像素。这意味着您必须在分配的纹理的导入设置中启用读/写选项。
要为粒子着色,形状模块首先会将纹理拉伸到您指定的形状上。然后,当系统从形状上的某个点发射粒子时,形状模块会使用该位置处纹理的颜色作为粒子颜色。
要查看纹理如何在形状上拉伸,请在层次结构视图中选择粒子系统并展开形状模块。形状的场景视图可视化包含纹理预览。
using UnityEditor; using UnityEngine;
[RequireComponent(typeof(ParticleSystem))] public class ExampleClass : MonoBehaviour { private ParticleSystem ps; public float alphaThreshold = 0.0f; public bool colorAffectsParticles = true; public bool alphaAffectsParticles = true; public bool bilinearFiltering = false;
void Start() { ps = GetComponent<ParticleSystem>();
var main = ps.main; main.startSpeed = 0.0f; main.startSize = 0.5f; main.startLifetime = 1.0f;
var emission = ps.emission; emission.rateOverTime = 500.0f;
var shape = ps.shape; shape.shapeType = ParticleSystemShapeType.Circle; shape.radius = 6.0f; shape.texture = AssetDatabase.GetBuiltinExtraResource<Texture2D>("Default-Particle.psd"); }
void Update() { var shape = ps.shape; shape.textureClipThreshold = alphaThreshold; shape.textureColorAffectsParticles = colorAffectsParticles; shape.textureAlphaAffectsParticles = alphaAffectsParticles; shape.textureBilinearFiltering = bilinearFiltering; }
void OnGUI() { float y = 120.0f; float spacing = 40.0f;
GUI.Label(new Rect(25, y, 140, 30), "Alpha Threshold"); alphaThreshold = GUI.HorizontalSlider(new Rect(165, y + 5, 100, 30), alphaThreshold, 0.0f, 1.0f); y += spacing;
colorAffectsParticles = GUI.Toggle(new Rect(25, y + 5, 200, 30), colorAffectsParticles, "Color Affects Particles"); y += spacing;
alphaAffectsParticles = GUI.Toggle(new Rect(25, y + 5, 200, 30), alphaAffectsParticles, "Alpha Affects Particles"); y += spacing;
bilinearFiltering = GUI.Toggle(new Rect(25, y + 5, 200, 30), bilinearFiltering, "Bilinear Filtering"); y += spacing; } }