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 /
  • Help Room /
avatar image
0
Question by Wicaodian · Dec 19, 2016 at 08:01 AM · 2d gamecolor change

how to change color of animated sprite prefab

I am making a 2D game in which there is a moving object just say like a cube. its more like a jelly cube moving ditto like jumping jelly.

I want to change the color of that "jelly cube" but not instantly but more like a filling like. it's need to start changing color from bottom to the top.

Please tell me any thing which could be helpful for this to me.

P.S. I am not really good at Unity right now maybe i am missing some basic things for this functionality please feel free for open comments anything related to this.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Dec 19, 2016 at 08:49 AM

There's a couple ways of doing this, but probably the simplest for a beginner is to just use two sprites with exactly the same transform values; each having the colors that you want (start/finish).

The initial image would be set to Fill Origin Top and the final image to Bottom. At this point all you have to do is lerp the initial sprite's Fill Amount value from 0 -> 1 while simultaneously adjusting the ending sprite's value from 1 -> 0. It would look something like this.

This is just something I knocked together to demonstrate the concept. Change the color in the inspector and toggle the enabled flag to see what I mean. This script will duplicate the initial sprite and create a second one for you, destroying it when done. This isn't ideal if you're going to be doing a lot of this, but you should consider your use case and experiment with different solutions based on what kind of performance you get.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class ColorFill : MonoBehaviour
 {
     public Color color;
     public Image initial;
 
     void OnEnable()
     {
         StartCoroutine(ChangeColor(color, 1));
     }
 
     public IEnumerator ChangeColor(Color color, float duration)
     {
         var final = Duplicate(initial);
         final.color = color;
         final.fillOrigin = 2;
         float elapsed = 0;
         while ((elapsed += Time.deltaTime) < duration)
         {
             var t = elapsed / duration;
             initial.fillAmount = Mathf.Lerp(1, 0, t);
             final.fillAmount = Mathf.Lerp(0, 1, t);
             yield return null;
         }
         initial.fillAmount = 1;
         initial.color = color;
         Destroy(final.gameObject);
     }
 
     Image Duplicate(Image image)
     {
         var result = Object.Instantiate(image) as Image;
         result.transform.SetParent(image.transform.parent, false);
         result.transform.localScale = image.transform.localScale;
         result.transform.position = image.transform.position;
         result.transform.rotation = image.transform.rotation;
         return result;
     }
 }
 
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 iwaldrop · Dec 19, 2016 at 08:57 AM 0
Share

Btw, I might have misunderstood the question and made this example using a UI Image. If this is not what you want then the solution will be a bit more complicated. Post back with your use case. The best solution may be a shader or vertex manipulation. The UI stuff unity built has a bit more functionality out of the box than the Sprite does.

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

88 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 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 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 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 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

i want random color in my five block but not same color how i do that 3 Answers

2D Top down player won't move down 2 Answers

How do you Isolate Physics2D.OverlapArea to a single script/gameObject/Instance? 0 Answers

Virtual controller and fire bullet shot in 2D 0 Answers

2D depth simulation in a sidescroller 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