- Home /
Use a function as Unity 4.6 UI event
Hey.
Is it possible to use a function as Unity 4.6 UI event, instead of creating a new public function in an object?
What I mean by this, can I, as example, do this: QualitySettings.SetQualityLevel (0, true);
With the event system? If not, what's the suggested way of doing this?
(PS: With the event system I mean the built in event system where you don't have to script anything but just select and drag anything you want. If you still don't know what I mean, add a button and scroll down until you see OnClick())
(to anybody: if you want, edit any wrong naming I gave to anything. I'm pretty new to unity)
I don't think so. But I almost everytime create a "XYZClicked()" and execute there what ever is necessary or delegate the work to another script for seperation concerns.
I assume that there's no answer to my question, except script it myself. If that's untrue, please write that down below. If not, I'll mark every correct answer below.
Answer by BMayne · Dec 25, 2014 at 09:29 PM
Hey there,
You can do this without having to write a new function. It's called a Lambda Expression, they are pretty neat. Here is the syntax.
myButton.onClick.AddListener(()=>
{
QualitySettings.SetQualityLevel (0, true);
Debug.Log("Quality set to Zero");
});
There is a lot more to learn about these types of expressions so I would suggest reading about them on MSDN.
Regards,
I can't read. You wanted to do it with no scripting. In that case you are out of luck. You can expect Unity to use Reflection to find every method in your assembly and let you pick it from a list. You have to give it a reference to what it is calling.
Answer by Mmmpies · Dec 25, 2014 at 08:34 PM
I know you said without any scripting but you've pretty much scripted what you want right there.
using UnityEngine;
using System.Collections;
public class SetQuality : MonoBehaviour {
public void SetQualityZero()
{
QualitySettings.SetQualityLevel (0, true);
}
}
Create a UI button and drag the script onto it. Then add an eventTrigger to the button, PointerClick then click on the + sign. Drag the button into the empty slot and from the drop down select SetQuality -> SetQualityZero()
That's it.
Answer by Kiwasi · Dec 25, 2014 at 08:24 PM
The best way to do this is a wrapper method.
public void MyButtonClick () {
// call some other function
}
Answer by wowipop · Dec 26, 2014 at 04:35 AM
on Unity Editor, inspector>add component>event trigger
I know what an event trigger is, but could you please explain what you mean? I don't know how to add these functions on there without any scripting. If you could show me that would be great.
sry though it's under the camera. I guess you can't get setting manager as gameobject
Your answer
![](https://koobas.hobune.stream/wayback/20220613174900im_/https://answers.unity.com/themes/thub/images/avi.jpg)