- Home /
Get mesh name with mouse click, then change color (and other properties) of mesh
Sorry about the lengthy title.. but that's about the gist of what I'm looking for.
Judging by the 'related questions' thingy that popped up, I may be able to find the mesh color change code from another post.. but I don't see anything about getting a mesh using the mouse pointer.
Any ideas? rifles through more unity docs
Still looking for assistance with this. Trying to select a single mesh of a model with the mouse pointer, then change the color of said mesh.
Answer by cjmarsh · Jan 16, 2011 at 02:37 AM
You could use Monobehaviour.OnMouseEnter and OnMouseExit. The script reference page is here. Also, a good resource for searching through unity-related docs is the Custom Google Search.
I tried attaching both of those to a mesh, and it doesn't seem to do anything. Don't I need to cast a ray at a collider or something to get it to recognize that the mouse is over it?
Answer by lodendsg · Sep 11, 2012 at 08:13 PM
Based on cjmarsh pointer I came up with the following thought I would post it here in case others came looking for similar.
Note I capture the start color but you could in most cases assume that a mats base color is white I suppose I have a few points where I am using the mat color to tent my meshes so I needed to know what it was before the mouse entered. As to the boolean testing; unnecessary to stack them like that its done for clarity in this case.
using UnityEngine;
using System.Collections;
public class MouseEventHandler : MonoBehaviour {
private Color StartColor;
public Color MouseOverColor = Color.yellow;
// Use this for initialization
void Start ()
{
if(renderer != null)
if(renderer.material != null)
StartColor = renderer.material.color;
}
// Update is called once per frame
void Update () {
}
void OnMouseEnter()
{
if(renderer != null)
if(renderer.material != null)
renderer.material.color = MouseOverColor;
}
void OnMouseOver ()
{
}
void OnMouseExit()
{
if(renderer != null)
if(renderer.material != null)
renderer.material.color = StartColor;
}
}