x


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

more ▼

asked Jan 16 '11 at 01:48 AM

Silx gravatar image

Silx
1 6 6 7

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.

Jan 23 '11 at 07:52 PM Silx
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jan 16 '11 at 02:37 AM

cjmarsh gravatar image

cjmarsh
373 2 9

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?

Jan 23 '11 at 07:50 PM Silx
(comments are locked)
10|3000 characters needed characters left

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;
    }
}
more ▼

answered Sep 11 '12 at 08:13 PM

lodendsg gravatar image

lodendsg
31 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1356
x982
x506
x76
x32

asked: Jan 16 '11 at 01:48 AM

Seen: 1731 times

Last Updated: Sep 11 '12 at 08:13 PM