- Home /
Add Button Script to 3D gameObject
Hi all,
I'm trying to add the Unity Button Script to a 3d gameObject. When I tried this by adding it onto a cube, I can't get the desired behaviours. How can I get the 3d object to work like a button? In particular the highlighting colors, the click events. I was speculating that perhaps it uses the wrong raycaster, and if the raycaster specificaiton is changed some how it would work? Does anyone have a good enough understanding of the source code to explain if this is possible, and if so what I would need to do to achieve this? Thanks.
Rik
Answer by Pinkuboxu · May 07, 2018 at 03:31 AM
Look into https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html then have it Invoke() a public or SerializedField UnityEvent that you can setup in the inspector, if you want to have something similar to the OnClick event that a UI button has. https://docs.unity3d.com/Manual/UnityEvents.html to understand UnityEvents. I can elaborate if needed.
I'm going to elaborate anyway and not be lazy. Your Cube code can look like this, but this is just an example so it's simple enough:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events; // this namespace makes the magic, tho
public class OnClickCube : MonoBehaviour {
[SerializeField] UnityEvent anEvent; // puts an easy to setup event in the inspector and anEvent references it so you can .Invoke() it
// This captures a click as long as you have a collider, even if it's set to just be a trigger, and nothing blocking it.
// However, it will still capture even if a Gui button is on top of it, so make sure you aren't checking this while also checking Gui
// Only other colliders block and it's not as manageable as Canvas Groups.
private void OnMouseDown()
{
print("You clicked the cube!");
anEvent.Invoke(); // Triggers the events you have setup in the inspector
}
// This is the first method the event is setup to do, the second audio part needed no script to just do a one shot effect, thanks to the event system.
// You just set up the Event in the inspector for easy peasy, but the UnityEvent could also be coded the same way if needed.
public void EventClick() // methods have to be public void to be used by UnityEvents, they can't really return anything either, as far as I know... At least I don't know how an event will capture the return...
{
print("Which also triggered this method as a UnityEvent!");
}
}
Put it on a cube and set up the cube in the inspector thusly so:
And that should make an object be more like a UI Button. But you can't actually reuse the same UI button script, but this is technically the same idea. (You gotta come up with your own Click sound asset though.) As far as highlighting, you'll want to change the material values at the same time... I'll have to help with that later though if needed. I have to pass out...
Can you please explain why this script is not resuable? I managed to make one of my 3d object work ok , and so did the others
Your answer
Follow this Question
Related Questions
Unusual trouble with OnPointerClick 1 Answer
How to detect which UI Button was triggered via Script (and not actually clicked)? 0 Answers
Click/touch object to enable/disable buttons inside Canvas 1 Answer
Selecting a selectable using script doesn't seem to work anymore? 1 Answer
NullReferenceException after passing InputField gameObject into EventSystem.SetSelectedGameObject() 1 Answer