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
1
Question by Itinerant · Oct 22, 2013 at 10:13 PM · colorcolors

Using predefined colors vs defining new colors

I have a script that recolors a number of objects at runtime, using someObject.renderer.material.color = color. I'm pulling these colors from a predefined list. For some reason, if I fill that list with the predefined colors in unity, everything works fine. If I try to define my own colors, they show up as unlit colors.

Here's my code, and an example of the difference between the two:

         List<Color> colorList = new List<Color>(){
             Color.red,
             Color.green,
             Color.yellow,
             Color.magenta,
             new Color(255F, 0F, 255F),    
             new Color(0F, 255F, 255F),    
             new Color(255F, 255F, 0F),
             new Color(128F, 0F, 128F),    
             new Color(128F, 0F, 0F)
             };

 someModel.renderer.material.color = colorList[i];



alt text

colors.jpg (63.9 kB)
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
3
Best Answer

Answer by rutter · Oct 22, 2013 at 10:43 PM

Unity's Color expects values between 0 and 1. You can swap 255 for 1.0, 128 for 0.5.

Comment
Add comment · Show 5 · 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 tanoshimi · Oct 22, 2013 at 10:52 PM 1
Share

Yeah, that's confusing for C# folk who are used to the standard .NET color constructor that uses 0-255 for RGBA values.

avatar image Bunny83 · Oct 22, 2013 at 10:56 PM 2
Share

Just for completeness, you can also use the Color32 struct to define your colors. The Color32 constructor takes 4 byte values between 0 and 255. There's only one constructor so you have to pass the alpha value as well.

As you can see at the bottom of the scripting reference it has two implicit casting operators which allows you to assign a Color32 value directly to a Color value or vice versa.

So you can simply do this:

 List<Color> colorList = new List<Color>(){
     Color.red,
     Color.green,
     Color.yellow,
     Color.magenta,
     new Color32(255,   0, 255, 255),
     new Color32(  0, 255, 255, 255),
     new Color32(255, 255,   0, 255),
     new Color32(128,   0, 128, 255),
     new Color32(128,   0,   0, 255)
 };

avatar image Owen-Reynolds · Oct 22, 2013 at 11:00 PM 1
Share

0-255 isn't a dot-NET thing. RGBA32 has been stored as 1-byte per channel = 0-255 for a long time.

But shaders have "always" used 0-1 for colors, and that range is also a little nicer for blending.

If you like, write: Color makeCol(int r...) { return new Color(r/255.0f...)}

avatar image tanoshimi · Oct 23, 2013 at 02:03 PM 1
Share

Owen - I think you missed my point - I'm not suggesting that 0-255 is a dot-NET thing ;) I'm suggesting that most C# programmers co$$anonymous$$g to Unity will see a Color() structure and assume it's the one from the System.Drawing namespace (which represents RGBA components as a value from 0-255), not Unity's own Color() structure from the UnityEngine namespace (which represents compononent values in the range 0.0 - 1.0). And both of which are unrelated to the representation of a colour in Cg/GLSL as used in a shader.

avatar image Itinerant · Oct 23, 2013 at 03:35 PM 0
Share

Thank you all for your help and explanations - it makes a bit more sense why Color wants float values. I've switched over to Color32, and everything is now working exactly as desired. Thanks again!

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

18 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

Related Questions

Vertex colors as diffuse in lightmapping. 0 Answers

My foreach or the Resources.LoadAll Is not getting the information I need 2 Answers

how to change color in unity scripting overtime?,how to change color gradually with C# 2 Answers

Color of some instantiated objects differs from the normal ones. 1 Answer

[Resolved] Game view displays the wrong colors? 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