- Home /
How to check if a Gradient in Editor has been changed.
I have a gradient that the user can change in editor but also some pre-made gradients in the script. I want to be able to check the gradient for differences, ie if they have set some colours on the inspector gradient then I don't want to replace it with my premade ones.
Is there a quick way to check? Without going through 10 different times looking for colour changes.
the only way i know how to check if things change is to make a variable like "oldGradient" and then something like
if(gradient != oldGradient)
{
//it changed
//do stuff
oldGradient = gradient;
}
Unfortunately that won't work much similar to how transform != oldTransform would work.
It's more of a type than a variable.
Answer by NathanFlurry · Oct 26, 2015 at 12:02 PM
Here's some code I just wrote up to do this: *Be sure to read the comments defining the difference between Compare()
and QuickCompare()
. Compare()
is moderately costly, so I suggest you use that only if you plan on using it as an editor extension or for in-editor testing purposes.
GradientMagic.cs
using UnityEngine;
public static class GradientMagic {
public static bool Compare(this Gradient gradient, Gradient otherGradient) {
// Compare the lengths before checking actual colors and alpha components
if (gradient.colorKeys.Length != otherGradient.colorKeys.Length || gradient.alphaKeys.Length != otherGradient.alphaKeys.Length) {
return false;
}
// Compare all the colors
for (int i = 0; i < gradient.colorKeys.Length; i++) {
// Test if the color and alpha is the same
GradientColorKey key = gradient.colorKeys[i];
GradientColorKey otherKey = otherGradient.colorKeys[i];
if (key.color != otherKey.color || key.time != otherKey.time) {
return false;
}
}
// Compare all the alphas
for (int i = 0; i < gradient.alphaKeys.Length; i++) {
// Test if the color and alpha is the same
GradientAlphaKey key = gradient.alphaKeys[i];
GradientAlphaKey otherKey = otherGradient.alphaKeys[i];
if (key.alpha != otherKey.alpha || key.time != otherKey.time) {
return false;
}
}
// They're the same
return true;
}
/// Compares the two gradients by testing points at a given interval, note this does not detect when new nodes are added
public static bool QuickCompare(this Gradient gradient, Gradient otherGradient, int testInterval = 3) {
// Tests the gradient at a couple points to see if they are the same, may fail is various cases
for (int i = 0; i < testInterval; i++) {
float time = (float) i / (float) (testInterval - 1);
if (gradient.Evaluate(time) != otherGradient.Evaluate(time)) {
return false;
}
}
// All the test points match
return true;
}
}
GradientChangeTester.cs
using UnityEngine;
[ExecuteInEditMode]
public class GradientChangeTester : MonoBehaviour {
/// The first gradient
public Gradient gradient = new Gradient();
/// The second gradient
private Gradient oldGradient = new Gradient();
/// Determines wether or not to use the QuickCompare method
public bool useQuickCompare;
/// The number of testing points for a quick compare
public int quickCompareTestInterval = 5;
public void Update() {
if (useQuickCompare) {
if (oldGradient == null || !gradient.QuickCompare(oldGradient, quickCompareTestInterval)) {
PrintChanged();
oldGradient.SetKeys(gradient.colorKeys, gradient.alphaKeys);
}
} else {
if (oldGradient == null || !gradient.Compare(oldGradient)) {
PrintChanged();
oldGradient.SetKeys(gradient.colorKeys, gradient.alphaKeys);
}
}
}
/// Used for debugging purposes to print when a gradient is changed
public void PrintChanged() {
print("Gradient changed.");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613130941im_/https://answers.unity.com/themes/thub/images/avi.jpg)