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 JasonSpine · Jun 20, 2018 at 12:51 AM · componentmonobehaviourpropertygroup

How do I change component properties without overriding them with parent component?

Hello, I'd like to write a script that works similar to setting alpha in CanvasGroup: https://docs.unity3d.com/Manual/class-CanvasGroup.html

When I set alpha in CanvasGroup, child objects opacity changes but doesn't get overwritten. I'd like to achieve similar effect for setting child SpriteRenderers tint color. Is it possible not to change their color property permanently? So the tint color would just depend on enabled parent component's value?

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 LeeroyLin · Jun 20, 2018 at 08:13 AM

You can change the material color, but notice that if you only change the color to your target color, the final color will be your target color * SpriteRenderer color. What you should do is use the target color RGB divided by the SpriteRenderer color RGB. I hope this will help you:

 public bool isUseColor = true;
 public Color col;
 
 void Update () {
         foreach (Transform item in transform)
         {
             Color c = item.GetComponent<SpriteRenderer>().color;
             Color newCol = new Color();
             newCol.r = col.r / c.r;
             newCol.g = col.g / c.g;
             newCol.b = col.b / c.b;
             newCol.a = col.a / c.a;
             item.GetComponent<Renderer>().material.color = isUseColor ? newCol : Color.white;
         }
 }
Comment
Add comment · Show 3 · 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 JasonSpine · Jun 20, 2018 at 01:56 PM 0
Share

Hmmm, sprites in Unity share default sprite material. Won't your code affect other sprites? - the ones that aren't object's children.

Besides I need it to work in editor, so OnValidate() would be better than Update() :)

avatar image LeeroyLin JasonSpine · Jun 21, 2018 at 11:02 AM 0
Share

Well, it's great to use OnValidate(). And I find a way to change the material which will not affect other sprites by using "$$anonymous$$aterialPropertyBlock".

And there is another problem is that when original color's alpha value set to 0, the new color will be black. To solve this, I change the 0 alpha value to a very small value.

 foreach (Transform item in root)
             {
                 Color c = item.GetComponent<SpriteRenderer>().color;
                 if (c.a == 0) {
                     c.a = .004f;
                     item.GetComponent<SpriteRenderer>().color = c;
                 }
 
                 Color newCol = new Color();
                 newCol.r = color.r / c.r;
                 newCol.g = color.g / c.g;
                 newCol.b = color.b / c.b;
                 newCol.a = color.a / c.a;
 
                 $$anonymous$$aterialPropertyBlock b = new $$anonymous$$aterialPropertyBlock();
                 b.SetColor("_Color", newCol);
                 item.GetComponent<SpriteRenderer>().SetPropertyBlock(b);
             }

Hope this can help you.

avatar image JasonSpine LeeroyLin · Jun 21, 2018 at 05:22 PM 0
Share

Thanks, it works! But it works strangely... When I change color in editor, sprite loses it's colors. The color is applied, when Unity window loses and regains focus ($$anonymous$$imize - maximize).

alt text

$$anonymous$$y previous code (changing sprite color property) worked on the fly:

      SpriteRenderer[] sprites = GetComponentsInChildren<SpriteRenderer> (true);
      for (int i = 0; i < sprites.Length;) {
          sprites [i].color = tintColor;
      }

But I don't like it because it makes colors of prefab instances "dirty".

It would be nice to know, how CanvasGroup actually works...

terrain.jpg (294.2 kB)

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

85 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

Related Questions

Best way to get variable from another script 1 Answer

Cloth property useTether missing? 0 Answers

Is this a correct (efficient) way of selecting and handling units? 0 Answers

How do I find the name and value for each variable of a script component, using Unity script? 2 Answers

Get OnMouseUp event in other object's script 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