3DMenu

From Unify Community Wiki
Jump to: navigation, search

Description

This script detects mouse interactions with the 3D object it is attached to, making it function like a 3D button. The object's material color will also change when the mouse cursor is over it.

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Events;
 
public class MouseButton3D : MonoBehaviour
{
    public enum ButtonMode { Normal, Restart, Quit }
 
    public ButtonMode Mode = ButtonMode.Normal;
    public Color HighlightColor = Color.red;
    public UnityEvent OnMouseClick;
 
    private Color m_StartColor;
    private Renderer m_Renderer;
 
 
    private void Awake ()
    {
        m_Renderer = GetComponent<Renderer> ();
        m_StartColor = m_Renderer.material.color;
    }
 
 
    private void OnMouseEnter ()
    {
        m_Renderer.material.color = HighlightColor;
    }
 
 
    private void OnMouseExit ()
    {
        m_Renderer.material.color = m_StartColor;
    }
 
 
    private void OnMouseUp ()
    {
        if (OnMouseClick != null)
            OnMouseClick.Invoke ();
 
        switch (Mode)
        {
            case ButtonMode.Quit:
            Application.Quit ();
            break;
 
            case ButtonMode.Restart:
            // Reload current scene.
            SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
            break;
        }
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox