I’ve created a material with a gradient texture. The gradient texture looks like the following:
This are my settings for the texture.
Now I create a material with this textura and “Bumbed diffuse” shader. Unfortunately if I assign this material to a “UI → Image” element it will not be shown on it. Instead, the defined color will be shown.
How can I get the gradient to be shown instead of the color? Do I need a specific shader? If so, why since it is a normal texture assigned to the material!?
It’s easy, my friend.
Assign your sprite directly to sprite property of Image component, if you don’t want to modify it at run time.
Else, for more flexibility, you can use gradient shader in your material.
using UnityEngine;
using System.Collections;
public class GradientTest : MonoBehaviour {
public Material yourGradientMaterial;
void Start () {
yourGradientMaterial.SetColor("_Color", Color.black); // the left color
yourGradientMaterial.SetColor("_Color2", Color.white); // the right color
}
}
Hey, this is too late to be a helpful answer for the original author. But for everyone else looking for a way to use gradients on their UI, it might be useful.
Unity’s UI components use vertex colors to color the graphics. This saves draw calls because all elements can use the same material. One can easily use vertex colors to create a gradient. It is best implemented using the BaseMeshEffect class where you can simply set the vertex colors of the image. (Unity - Scripting API: BaseMeshEffect)
I have implemented a gradient component based on this. You can animate the colors or change them via a script, which is a bonus compared to using a texture. Here is the store link: UI Gradient | GUI Tools | Unity Asset Store
Instead of creating a material with the texture assigned, assign the gradient texture to “Source image” of the element itself. This has solved the problem for me.