Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CapnShanty · Oct 26, 2015 at 12:01 PM · c#color changeprocedural generation

Change color gradually?

Okay so I have the following script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ChangeColor : MonoBehaviour
 {
     Material target;
     public List<Color> colors;
     public bool useDefaultColorSet = false;
     ColorSet colorSet;
     void Awake()
     {
         if (useDefaultColorSet)
             colorSet = GameObject.FindObjectOfType<ColorSet>();
         target = gameObject.GetComponent<Renderer>().material;
         target.color = getRandomColor();
     }
 
 
     Color getRandomColor()
     {
         if (useDefaultColorSet)
             return colorSet.colors[Random.Range(0, colorSet.colors.Count)];
         return colors[Random.Range(0, colors.Count)];
     }
 
 }

And I would like for there to be a gradient area between the two colors, because right now it's fairly harsh. It's applying these different colors to a procedurally generated object in segments, so I was wondering if there's a way to use a gradient shift between them? In looking this up I saw something about color lerp but I don't really know if it could be used here, it seems like it shifts color over time mostly.

Comment
Add comment · Show 1
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 Bonfire-Boy · Oct 26, 2015 at 12:31 PM 0
Share

No, Color.Lerp is just a way of interpolating colours. It has nothing intrinsically to do with time.

So if you have 2 colours and want a colour halfway between them you do

Color halfway = Color.Lerp( colour1, colour2, 0.5f);

And you can use this to get colours any fraction of the way between them.

Sure, you can use this to change a single colour over time, from colour2 to colour2. But you can also use it just to generate a set of colours that vary between colour1 and colour2.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by The-Little-Guy · Oct 26, 2015 at 06:12 PM

You might be interested in using a Gradient. With this example, I create a gradient property and a duration property that you can edit it the inspector. You can use them to set the colors of the gradient with however many colors you would like, and the duration as well.

In this example I change the background color of the Main Camera.

 using UnityEngine;

 public class ColorChange : MonoBehaviour {

     [SerializeField]
     Gradient gradient;
     [SerializeField]
     float duration;
     float t = 0f;

     void Update() {
         float value = Mathf.Lerp(0f, 1f, t);
         t += Time.deltaTime / duration;

         Color color = gradient.Evaluate(value);
         Camera.main.backgroundColor = color;
     }
 }

If we add Start() we can generate a gradient on the fly like this:

 void Start(){
     var colorkeys = new GradientColorKey[2];
     var alphakeys = new GradientAlphaKey[2];
     
     colorkeys[0].color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
     colorkeys[0].time = 0f;
     colorkeys[1].color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
     colorkeys[1].time = 1f;
     
     alphakeys[0].alpha = 1f;
     alphakeys[0].time = 0f;
     alphakeys[1].alpha = 1f;
     alphakeys[1].time = 1f;
     
     gradient.SetKeys(colorkeys, alphakeys);
 }

And the last way that I can think of is to use 2 colors and lerp between them.

 using UnityEngine;
 
 public class ColorChange : MonoBehaviour {
 
     [SerializeField]
     float duration;
 
     float t = 0f;
     Color color1, color2;
 
     void Start() {
         color1 = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
         color2 = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
     }
 
     void Update() {
         Color color = Color.Lerp(color1, color2, t);
         t += Time.deltaTime / duration;
         Camera.main.backgroundColor = color;
     }
 }
Comment
Add comment · Show 1 · 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
avatar image Phantom01 · May 04, 2016 at 07:47 PM 0
Share

can you please tell me how to make it loop??

avatar image
1

Answer by sysmaya · Jan 30, 2017 at 10:48 AM

Fade IN - Fade OUT Change smoothed betwen 2 Colors.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class BlinkColor : MonoBehaviour {
     public Color colorIni = Color.white;
     public Color colorFin = Color.red;
     public float duration = 3f;
     Color lerpedColor = Color.white;
 
     private float t=0;
     private bool flag; 
 
     Renderer _renderer;
     // Use this for initialization
     void Start () {
         _renderer=GetComponent<Renderer>(); 
     }
     
     void Update() {
         lerpedColor = Color.Lerp(colorIni, colorFin,  t);
         _renderer.material.color = lerpedColor;
 
         if (flag == true) {
             t -= Time.deltaTime/duration;
             if (t < 0.01f)
                 flag = false;
         } else {
             t += Time.deltaTime/duration;
             if (t > 0.99f)
                 flag = true;
         }
     }
 }
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
avatar image
0

Answer by chillandcodegames · Aug 27, 2021 at 10:10 AM

Use this if you are using an Image

 using UnityEngine;
 using System.Collections;
 using System;
 using UnityEngine.UI;
 
 public class BlinkColor : MonoBehaviour
 {
     public Color colorIni = Color.white;
     public Color colorFin = Color.red;
     public float duration = 3f;
     Color lerpedColor = Color.white;
 
     private float t = 0;
     private bool flag;
 
     Image _renderer;
     // Use this for initialization
     void Start()
     {
         _renderer = GetComponent<Image>();
     }
 
     void Update()
     {
         lerpedColor = Color.Lerp(colorIni, colorFin, t);
         _renderer.color = lerpedColor;
 
         if (flag == true)
         {
             t -= Time.deltaTime / duration;
             if (t < 0.01f)
                 flag = false;
         }
         else
         {
             t += Time.deltaTime / duration;
             if (t > 0.99f)
                 flag = true;
         }
     }
 }
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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Implementation of directional buildings? 1 Answer

How do I change an objects color by clicking on it during runtime?,How do I change an objects color by clicking on it during run time? 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 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