- Home /
Using a 3D model as a button, in Unity's UI system?
I have a 3D model, that is a child of a world space Canvas. How can I make it interactable, using the UI system? So far I've tried adding the button component, in addition to a box collider. I've also tried making it a child of a button prefab. No luck, am I missing something, or is it simply not possible, without using an event in a custom script?
2019 edit: Here's what I ended up creating and using back in 2016/2017:
https://www.dropbox.com/s/quf95uiwoijg50f/ColliderEventTrigger.unitypackage?dl=0
It has been tested in, and update for, 2019.1
Why not just check if it is clicked via raycasting and if yes, call the button function directly?
That is one way to do it, I would just prefer to use the UI system, if possible. I find it's a lot faster and easier to use, than scripting my entire UI. Also it interacts more smoothly with the animation system.
Answer by KillerFirefly · May 09, 2019 at 03:18 PM
You can use a UnityEvent to get the nice pretty editor event system just like a Button component.
using UnityEngine.Events;
[RequireComponent(typeof(Collider))] //A collider is needed to receive clicks
public class Interact : MonoBehaviour {
public UnityEvent interactEvent;
private void OnMouseDown() {
interactEvent.Invoke();
}
}
Although OP did want to avoid a "custom script", this is a pretty clean way of doing it.
I'm not following their aversion to writing something, as extending functionality of built-in Unity systems (like using a 3D object as a UI element) is the textbook use case for writing your own components.
I guess since you resurrected a 4 year old post anyway, I'll share what I ended up creating and using back then. It' might be missing some features added in 2018, but it works:
https://www.dropbox.com/s/quf95uiwoijg50f/ColliderEventTrigger.unitypackage?dl=0
Answer by Nirvana33 · Aug 20, 2015 at 06:11 PM
You can use the OnMouseDown() function. Add it to your 3D model, and set the object child of the canvas.
But I would not be able to use the in editor event system then. I would have to do it all in a script, right?