Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 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
16
Question by christianstrang · Jul 29, 2015 at 12:12 PM · colorcolorshex

Hex colors in unity

This is bothering me for quite some time: How can you use hex color values in your script, without the need to convert it first. Is there a simple script or asset everyone is using? I can't be the only one that finds this representation for a color value strange (at least coming from a web background).

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

3 Replies

· Add your reply
  • Sort: 
avatar image
21
Best Answer

Answer by Jessespike · Jul 29, 2015 at 04:43 PM

An extension could be written to fit your needs. I haven't found it strange to use only numeric RGB values, though I would prefer to have the option available, even if I don't use them frequently. What we can do, is use the TryParseHexString function:

     Color myColor = new Color();
     Color.TryParseHexString("#F00", out myColor);
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 ZDFDigital · Sep 22, 2015 at 10:44 AM 15
Share

@Jessespike Since I updated to 5.2.0 the TryParseHexString vanished.

The new way is:

 ColorUtility.TryParseHtmlString (hexString, out color);

avatar image
39

Answer by FSpark · Sep 14, 2016 at 12:36 PM

@simpleplaystudio In case anyone else stumbles across this - you can also use hex notation directly e.g. Color myColor = new Color32( 0x2B, 0xB1, 0xD2, 0xFF ); // RGBA

Comment
Add comment · Show 2 · 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 Deepscorn · Jul 21, 2018 at 04:51 PM -1
Share

The following simple test wount work with your code:

 Color myColor = new Color(0xc6,0xe7, 0x45, 0xFF);
 string colorHex = ColorUtility.ToHtmlStringRGB(myColor);
 Assert.AreEqual("c6e745", colorHex);

colorHex will be FFFFFF. This is due to ColorUtility.ToHtmlStringRGB expects r,g,b in 0..1 range. But you provide them in 0..255

avatar image Hellium Deepscorn · Jul 21, 2018 at 04:54 PM 3
Share

Simply because he has used new Color32, not new Color

avatar image
0

Answer by Fragmental · May 21, 2018 at 08:56 PM

You can also create a public color variable and then assign the hex to that variable through the color picker in the inspector. You can do the same with color blocks, if your using UI objects. It's not ideal, but for the sake of completeness, I thought I should mention it.

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

9 People are following this question.

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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Using predefined colors vs defining new colors 1 Answer

Legacy animation system cannot animate colors? 3 Answers

Convert RGB to RYB color scheme? 0 Answers


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