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
1
Question by nyonge · May 30, 2017 at 10:20 PM · coloralphafloatbytecolor32

Float to Byte for Color32

How do I take a float (0f to 1f) and convert that to a byte (not a byte array) for a Color32's alpha channel (0-255)?

I have an function I wrote awhile back, SetAlpha, that takes a float from 0f to 1f and sets it to the alpha channel of a Color. With some new changes I've made, I now need to use Color32 instead of Color to set alpha. Accessing .a doesn't work as it takes a byte, not a float (and I can't Mathf.RoundToInt either).

I suspect this is probably pretty simple but I've never worked with Color32 before, and don't know the syntax. Thank you!

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
5
Best Answer

Answer by IgorAherne · May 30, 2017 at 10:32 PM

 (byte) (your01float *  255)

lol'ed

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 superme2012 · Nov 14, 2017 at 10:12 PM 0
Share

Thanks for the info.

.toByte() would be nice.

avatar image zenixue · Apr 15, 2020 at 02:53 PM 0
Share

i love u!!!

avatar image
2

Answer by FM-Productions · May 30, 2017 at 10:37 PM

You could try this:

float randomAlphaValue = 0.43f;

color.r = (byte) ((int) (randomAlphaValue * 255) % 256)

The modulo % 256 is not strictly necessary, since only the right-most byte of the int value you pass is used. You could also omit the int casting.

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 IgorAherne · May 30, 2017 at 10:38 PM 0
Share

meh to complex

avatar image FM-Productions IgorAherne · May 30, 2017 at 10:41 PM 1
Share

You are right, I saw your answer only after I have written $$anonymous$$e. But I will leave $$anonymous$$e also here :)

avatar image RayAndGames FM-Productions · Oct 19, 2019 at 03:55 AM 0
Share

such humble... such kind

avatar image
1

Answer by bison-- · Sep 14, 2020 at 06:02 AM

I casted it to Color32 like this:

 UnityEngine.Color sourceColor = new UnityEngine.Color(0.5f, 0.5f, 0.5f, 0.5f);
 UnityEngine.Color32 convertedColor;
 convertedColor = sourceColor;

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

70 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

Related Questions

How to make UI Label text loop through colors ? (example attached) 1 Answer

Default color issue 2 Answers

Trying to change the alpha on a particle system. 1 Answer

Acces the secondary colors in the shaders. 1 Answer

guiTexture color half alpha White? 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