3DMenu

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</javascript>" to "</syntaxhighlight>")
(Updated example code and converted it to csharp)
 
Line 1: Line 1:
 
 
== Description ==
 
== Description ==
This is a very basic example implementation of a menu-like mouse input detector. This script detects mouse overs and mouse clicks, the object this script is applied to will change to a red color when the mouse is over it. The game will quit or load level 1 depending on the value of isQuitBtn.
+
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.
  
== Script ==
+
<syntaxhighlight lang="csharp">using UnityEngine;
 +
using UnityEngine.SceneManagement;
 +
using UnityEngine.Events;
  
<syntaxhighlight lang="javascript">
+
public class MouseButton3D : MonoBehaviour
var isQuitBtn : boolean = false;
+
{
 +
    public enum ButtonMode { Normal, Restart, Quit }
  
private var startColor : Color = renderer.material.color;
+
    public ButtonMode Mode = ButtonMode.Normal;
 +
    public Color HighlightColor = Color.red;
 +
    public UnityEvent OnMouseClick;
  
function OnMouseEnter ()
+
    private Color m_StartColor;
{
+
    private Renderer m_Renderer;
renderer.material.color = Color.red;
+
}
+
  
function OnMouseExit ()
 
{
 
renderer.material.color = startColor;
 
}
 
  
function OnMouseUp()
+
    private void Awake ()
{
+
    {
if(isQuitBtn)
+
        m_Renderer = GetComponent<Renderer> ();
{
+
        m_StartColor = m_Renderer.material.color;
Application.Quit();
+
    }
}
+
 
else
+
 
{
+
    private void OnMouseEnter ()
Application.LoadLevel(1);
+
    {
}
+
        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;
 +
        }
 +
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 15:26, 6 September 2018

[edit] 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