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
0
Question by Daniel G · Jul 26, 2013 at 10:20 PM · javascriptmaterial colorrgb

Changing material Color using RBG?

Hello, Just to Be clear i am using JavaScript I am wondering if it is possible to change the color of a material using RBG values? I would like to make one GUI slider that accesses the numbers from 0-255 on R, G, B and alpha value. (That GUI slider would access all the possible colors from most right to most left.

I found this snippet of code to change the color of the material in RBG:

 material.color = newColor(255f, 255f, 255f, 1);


Am I on the right track?

Thank You

Daniel

Comment
Add comment · Show 1
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 Daniel G · Jul 26, 2013 at 10:24 PM 0
Share

I would also like to be a will to change the shininess of the objects material

3 Replies

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

Answer by Eric5h5 · Jul 26, 2013 at 10:51 PM

If you want to use 0-255 integer values, use Color32.

 material.color = Color32(255, 255, 255, 1);
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 LemonLake · Jul 26, 2013 at 10:58 PM 0
Share

This is good to know! Thanks for showing this. I'm sure it'll be useful.

avatar image Daniel G · Jul 26, 2013 at 11:09 PM 0
Share

Thank you! This is the answer I'm looking for, simple, straightforward!

For any one that wants to read more about Color32 http://docs.unity3d.com/Documentation/ScriptReference/Color32.html

avatar image
1

Answer by LemonLake · Jul 26, 2013 at 10:36 PM

newColor, as far as I know, does not exist. Here's a function to create a color for what you want:

    function getColor(red:int, green:int, blue:int, alpha:int):Color{
        return Color((1/255)*red, (1/255)*green, (1/255)*blue, (1/255)*alpha);
    }

(note: not tested, but should work :))

To use it, simply do:

 material.color = getColor(255,255,255,255);

Red, green, blue and alpha are numbers between 0 and 255.

UPDATE: As for shininess, that is up to your shader. See http://docs.unity3d.com/Documentation/ScriptReference/Material.SetFloat.html

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 AlucardJay · Jul 26, 2013 at 10:40 PM 0
Share

Looks like a typo, no space between the keyword new and Color. Just as a note, the keyword new and denotation f doesn't have to be included in uJS (though I like to use them!). uJS has a lot of implicit typecasting.

 material.color = new Color( 0.1f, 0.1f, 0.1f, 1 );
 // is the same as below in uJS
 material.color = Color( 0.1, 0.1, 0.1, 1 );

Nice function =]

avatar image LemonLake · Jul 26, 2013 at 10:41 PM 0
Share

@alucardj Thanks for telling me, more into C# now, I last used javascript properly a few years ago. Fixed.

avatar image Eric5h5 · Jul 26, 2013 at 10:54 PM 0
Share

"f" doesn't have to be included in Unityscript because numbers with decimals are floats by default; if you want doubles then you need to specify "d". C# is the opposite, where numbers with decimals are doubles by default. You can specify "d" but it's not necessary, whereas you need to specify "f" if you want floats.

avatar image Eric5h5 · Jul 26, 2013 at 10:55 PM 0
Share

That function isn't necessary, since Color32 exists. Also it wouldn't work since you're doing integer division.

avatar image Daniel G · Jul 26, 2013 at 11:07 PM 0
Share

Excellent function however I'm leaning toward color32, because i can replace the numbers easily with floats and don't have to have integer division! BUT thank you for the shininess! How do i accept more than one answer it wont let me? I have seen it done before though!

avatar image
1

Answer by AlucardJay · Jul 26, 2013 at 10:29 PM

http://docs.unity3d.com/Documentation/ScriptReference/Color.html

Each color component is a floating point value with a range from 0 to 1.

Try :

 var r : float = value / 255.0f;

Shininess would be relative to the Shader.

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 Daniel G · Jul 26, 2013 at 11:08 PM 0
Share

But looks like color 32 is already 255? SO this would be only necessary if i was using the function lemon lake submitted correct?

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

17 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

Related Questions

Multiple Cars not working 1 Answer

OnTriggerStay Not Working, JavaScript Help Needed 1 Answer

How to Implement System.StringBuilder into JavaScript? 2 Answers

Speed Pill... Variable referencing and adjusting temporarily 3 Answers

Detect OnTriggerEnter with tags 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