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 brebarth · Feb 10, 2015 at 07:05 PM · colorgradientrectangle

Fill rectangle with a gradient not using GUI

Hi,

I'm new to unity.. I'm working in 2D mode. I can't figure out how to place a rectangle with color fill in the scene. I don't want to use the GUI methof of doing this, because the rectangle needs to be part of the scene.

Also I'd like to fill it with a gradient going from one color to another. Say from Red to Blue. How can I achieve this, best practice. I need the colors to change dynamically, so one frame could be red to blue but the next frame could be pink to yellow or something, but would need to be the same game object. Thanks for your help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by giulio-pierucci · Feb 10, 2015 at 07:38 PM

You can use:

http://docs.unity3d.com/ScriptReference/Graphics.DrawMesh.html

or

GLDRAW http://docs.unity3d.com/ScriptReference/GL.html

You have to set with first color 2 vertices color, other 2 with second color. The shader MUST read vertex color (like Particle)

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 brebarth · Feb 10, 2015 at 08:30 PM 0
Share

Grazie mille! I'll see how I go and if I get stuck I'll come back.

avatar image giulio-pierucci · Feb 10, 2015 at 08:43 PM 0
Share

Di nulla! At your disposal :)

avatar image
0

Answer by brebarth · Feb 12, 2015 at 07:57 PM

Okay. I found a solution for this. My solution wasn't exactly how giulio.pierucci suggested, but it helped my get to the right way. Anyway there is more than one way. What I did was:

(1) Make a quad in the hierarchy

(2) import a perfect white image into the project view.

(3) create a material with the white swatch

(4) drag the material on the quad's inspector into the mesh renderer Materials property.

(5) create a script file and add code:

     MeshFilter viewedModelFilter = (MeshFilter)GetComponent("MeshFilter");
     Mesh mesh = viewedModelFilter.mesh;


     Color[] colors = new Color[mesh.vertices.Length];

     Color BottomColor = Color.red;
     Color TopColor = Color.blue;

     colors[0] = BottomColor;
     colors[1] = TopColor;
     colors[2] = BottomColor;
     colors[3] = TopColor;
     mesh.colors = colors;

into the Update or Start function, depending where you want it.

(6) in the quad's inspector pick the white material's shader as Particles/Alpha Blended Premultiply

I'm almost happy with this solution. I have a couple of question though:

(question a) Is this solution efficient or computationally wasteful?

(question b) I've added a screen shot. On the left is my Blue to Red gradient in photoshop, on the right is my quad / material gradient in Unity. Now, when I open the screen shot image in my image editor and use color picker to see the R,G,B values of the respective gradients, I get different colors for unity-sourced gradient vs the editing-sourced gradient, even though they should be exactly the same. The image editor's gradient is on the left, the unity gradient is on the right. In code I use Color.red/Color.blue, and in the editor I use perfect red (255,0,0) and perfect blue (0,0,255) Why is unity changing the color from perfect red to (248,15,0)-> color-picked, which is almost perfect but not quite. Btw, I also color-picked the screen shot of the original gradient from the editor and the color grab was also not perfect red but different values than the unity-sourced gradient. But shouldn't the changes the color picker has be exactly identical? Is there a modification in Unity, because of the shader? What can I do to have unity not modify the on-screen colors in that way?

alt text


screencap.png (63.2 kB)
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 giulio-pierucci · Feb 12, 2015 at 08:37 PM 0
Share

The difference of color maybe caused by texture importing parameters (like compression, RGB sampling, etc)

The solution isn't too expensive, but is better not compose mesh in Update function, but you should instantiate and modify the mesh in Start, if possible.

avatar image brebarth · Feb 12, 2015 at 09:13 PM 0
Share

Colors: I used lossless compression for my editor output images. Whatever screen capture does I don't know, so there might be lossy compression. But even in that case, shouldn't both gradients then have exactly the same loss error? But this is not the case, they slightly vary.

$$anonymous$$esh: Where is the mesh composed in that piece of code? Is it composed "hidden" inside one of the calls I make? Otherwise I don't see that it es composed.. Or were you speaking in general?

avatar image giulio-pierucci · Feb 12, 2015 at 10:31 PM 0
Share

$$anonymous$$esh: Oh, in general. In your code you get a quad mesh from asset, sorry for my mysunderstanding.

Colors: You probably use a loseless compression for exporting images by your favourite image editor, BUT ... Unity imports, corverts and optimizes them PER PLATFOR$$anonymous$$!

If you click image in Project window, you can see import parameters in inspector.

See the image above, for example:

alt text

immagine-20150212-232342.png (45.0 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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Gradient Text in Unity 5.2.2 "BaseVertexEffect is obsolete use BaseMeshEffect instead" 3 Answers

Particle System - Get 'Start Color' as Gradient 1 Answer

Apply gradient between two colors on x amount of objects 1 Answer

Smooth gradient between colours? 1 Answer

change Mesh.colors over time help? 2 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