Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jazzer008 · Jan 20, 2014 at 05:25 PM · editorinspectorcheckgradientdifference

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TimBorquez · Jan 20, 2014 at 07:46 PM 0
Share

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;
 }
avatar image Jazzer008 · Jan 22, 2014 at 02:19 PM 0
Share

Unfortunately that won't work much similar to how transform != oldTransform would work.

It's more of a type than a variable.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.");
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gradient Changes in Editor Reset In Play Mode. Why? 1 Answer

Draw inspector for SerializedObject within EditorWindow? 1 Answer

Inspector turns black on refresh 4 Answers

How to record hideFlags for Undo/Redo 0 Answers

Exposing Enums in Custom Inspector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges