Hi All
I have a UI Color Palette. Where i can touch Ui Color Palette the Color should apply in Cube.
I have used the below Code. But the Code is in GUI. Could any one can help me in converting into an UI.
using UnityEngine;
using System.Collections;
public class TextureColorP : MonoBehaviour
{
//Before we start, we need to store some variables that can be accessed in the editor, and can be used throughout the script.
//Our first variable will be the texture displayed on the screen, so we can choose the colour from it
public Texture2D colourTexture;
//Next, we want to store the Renderer of the cube in the scene, this way we can change the colour of the material on it
public Renderer colouredCube;
//lastly we store a Rect that describes where our texture is going to be displayed on the screen.
private Rect textureRect = new Rect (10, 10, 200, 200);
//Now everything else is done within the OnGUI function.
void OnGUI ()
{
//Simply just draw our texture in the position we gave it at the beginning
GUI.DrawTexture (textureRect, colourTexture);
//We want to check what event is happening during OnGUI. In this case we want to check if the mouse button has been released, so that we know if the user clicked on the texture or not
if (Event.current.type == EventType.mouseDrag) {
//if (Event.current.type == EventType.MouseUp) {
//get the mouse position
Vector2 mousePosition = Event.current.mousePosition;
//if the mouse position is outside the texture being displayed on the screen, just exit out because we dont want to do anything.
if (mousePosition.x > textureRect.xMax || mousePosition.x < textureRect.x || mousePosition.y > textureRect.yMax || mousePosition.y < textureRect.y) {
return;
}
//if we made it here, we know that the mouse is somewhere on the texture. Since we know this, we need to figure out a way to get the colour of the texture, wherever the mouse currently is. In order to do this, we need to calculate the UV coordinates of the mouse on the texture
float textureUPosition = (mousePosition.x - textureRect.x) / textureRect.width;
float textureVPosition = 1.0f - ((mousePosition.y - textureRect.y) / textureRect.height);
//Once we have the UV coordinates, we use a function called GetPixelBilinear on the texture. This will return the colour of the texture at the given UV coordinates.
Color textureColour = colourTexture.GetPixelBilinear (textureUPosition, textureVPosition);
// and now that we have our colour, we just apply it to the cube's material
colouredCube.material.color = textureColour;
}
}
void Update()
{
}
When the user touch the color paletee. The color should changed in the cube.
Thanks in Advance
}