最小值-最大值渐变的脚本接口。
包含两个 Gradient,并基于 ParticleSystem.MinMaxGradient.mode 返回一个 Color。根据模式,可能返回随机值。 选择了需要渐变的 ParticleSystemGradientMode 的之后,可通过 ParticleSystem Inspector 编辑渐变。某些模式不需要渐变,只需要颜色。 另请参阅:ParticleSystem。
using UnityEngine;
// This example shows setting a constant color value. public class ConstantColorExample : MonoBehaviour { ParticleSystem myParticleSystem; ParticleSystem.ColorOverLifetimeModule colorModule;
void Start() { // Get the system and the color module. myParticleSystem = GetComponent<ParticleSystem>(); colorModule = myParticleSystem.colorOverLifetime;
GetValue(); SetValue(); }
void GetValue() { print("The constant color is " + colorModule.color.color); }
void SetValue() { colorModule.color = Color.red; } }
using UnityEngine;
// This example shows using 2 colors to drive the color over lifetime. public class TwoConstantColorsExample : MonoBehaviour { ParticleSystem myParticleSystem; ParticleSystem.ColorOverLifetimeModule colorModule;
void Start() { // Get the system and the color module. myParticleSystem = GetComponent<ParticleSystem>(); colorModule = myParticleSystem.colorOverLifetime;
GetValue(); SetValue(); }
void GetValue() { print(string.Format("The constant values are: min {0} max {1}.", colorModule.color.colorMin, colorModule.color.colorMax)); }
void SetValue() { colorModule.color = new ParticleSystem.MinMaxGradient(Color.green, Color.red); } }
using UnityEngine;
// This example shows using a gradient to drive the color over lifetime. public class GradientColorExample : MonoBehaviour { ParticleSystem myParticleSystem; ParticleSystem.ColorOverLifetimeModule colorModule; Gradient ourGradient;
void Start() { // Get the system and the color module. myParticleSystem = GetComponent<ParticleSystem>(); colorModule = myParticleSystem.colorOverLifetime;
// A simple 2 color gradient with a fixed alpha of 1.0f. float alpha = 1.0f; ourGradient = new Gradient(); ourGradient.SetKeys( new GradientColorKey[] { new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) } );
// Apply the gradient. colorModule.color = ourGradient;
// In 5 seconds we will modify the gradient. Invoke("ModifyGradient", 5.0f); }
void ModifyGradient() { // Reduce the alpha float alpha = 0.5f; ourGradient.SetKeys( new GradientColorKey[] { new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) } );
// Apply the changed gradient. colorModule.color = ourGradient; } }
using UnityEngine;
// This example shows using 2 gradients to drive the color over lifetime. public class TwoGradientColorExample : MonoBehaviour { ParticleSystem myParticleSystem; ParticleSystem.ColorOverLifetimeModule colorModule;
Gradient ourGradientMin; Gradient ourGradientMax;
void Start() { // Get the system and the emission module. myParticleSystem = GetComponent<ParticleSystem>(); colorModule = myParticleSystem.colorOverLifetime;
// A simple 2 color gradient with a fixed alpha of 1.0f. float alpha1 = 1.0f; ourGradientMin = new Gradient(); ourGradientMin.SetKeys( new GradientColorKey[] { new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(alpha1, 0.0f), new GradientAlphaKey(alpha1, 1.0f) } );
// A simple 2 color gradient with a fixed alpha of 0.0f. float alpha2 = 0.0f; ourGradientMax = new Gradient(); ourGradientMax.SetKeys( new GradientColorKey[] { new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(alpha2, 0.0f), new GradientAlphaKey(alpha2, 1.0f) } );
// Apply the gradients. colorModule.color = new ParticleSystem.MinMaxGradient(ourGradientMin, ourGradientMax);
// In 5 seconds we will modify the gradient. Invoke("ModifyGradient", 5.0f); }
void ModifyGradient() { // Reduce the alpha float alpha = 0.5f; ourGradientMin.SetKeys( new GradientColorKey[] { new GradientColorKey(Color.green, 0.0f), new GradientColorKey(Color.red, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) } );
// Apply the changed gradients. colorModule.color = new ParticleSystem.MinMaxGradient(ourGradientMin, ourGradientMax); } }
color | 设置常量颜色。 |
colorMax | 为上限设置常量颜色。 |
colorMin | 为下限设置常量颜色。 |
gradient | 设置渐变。 |
gradientMax | 为上限设置渐变。 |
gradientMin | 为下限设置渐变。 |
mode | 设置模式,最小值-最大值渐变使用该模式计算颜色。 |
ParticleSystem.MinMaxGradient | 用于整个渐变的单个常量颜色。 |
Evaluate | 手动查询渐变以基于所处模式计算颜色。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.