Inspector variable for selecting a function to be called (Unity3D) - unity3d

The latest versions of NGUI have this great tool that lets you pick which function within any of the target's scripts will be called when you click on it.
Basically, it's a select box within the inspector that gets automatically filled with all the functions from all the scripts attached to the game object.
How can I generate a function list that fills up automatically like this?
I don't want to have to maintain an enum with all the possible functions (including some that the current object might not have)
I tried looking at the code NGUI used, but it was a bit too complicated for me to understand right now.

If you want to do it like in NGUI then use the tools that are available within NGUI itself and define a public variable like this:
public List<EventDelegate> DelegateList = new List<EventDelegate>();
With this, you can drop MonoBehaviour scripts in the Inspector field and can then select public methods/delegates contained in that script.
You can then invoke them like this:
void Start() {
EventDelegate.Execute(DelegateList);
}
Now every method in your delegate list will be invoked. You can see this for example in the UIButton script where this is used to handle the OnClick delegates.

Related

No function dropdown in AnimationEvent in Unity

I try to handle some events during animations, but everywhere I look, every tutorial have access to AnimatorEvent Inspector like this:
A nice simple field, where you can select a function, I want this!
But instead of this, I always getting this sick 5 fields view, and don't have any idea how to handle animation event in this case!
I tried to create function test() with debug log, but it didn't work anyway. Why I can't get access to this simple window where I can choose an function?
You will need to add this animation into a State in Animator Controller (via Animator Window).
In the object which contains Animator component, attach your script component to it.
Open the Animation window, select the object above, you will see a dropdown of animations (top left) which Animator Controller of this object contains. Choose and add event to the animation you want to.
Select the event in Animation Window, in the Inspector, you should see the dropdown of public functions of your script component attached above.
Answer by Aluminium18
Sometimes it doesn't work even if you do all things according to tutorials or advices in internet. I often leave Unity editor launched for a long time without any interactions with it. After several gibernations and several days it can get buggy - various errors appear, you can't see some functions from scripts and so on. So, just reloading the Unity editor solves many of such issues for me. And it continues to happen so for several years no matter what Unity version you have. I tried versions from 2019.x.x to 2022.x.x - all this time Unity behaves itself the same.

Add a dropdown inside OnGUI function

I build a menu inside the OnGUI function like
GUILayout.Label("Singleplayer");
if(GUILayout.Button("Zombie"))
ChangeScene("Zombie");
Is it possible to add a Dropdown programmatically like i create the button?
If you're doing this in the editor - i.e. a custom Inspector or EditorWindow - then yes:
https://docs.unity3d.com/ScriptReference/EditorGUILayout.DropdownButton.html
But if you're doing it in-game, you need to stop using the OnGUI method (it's not supported any more by Unity) and switch to using the new UnityUI (which isn't really "new" any more - it went live almost 5 years ago! :)). UnityUI has built-in drop-down functionality you create directly in the Editor:
https://docs.unity3d.com/2018.4/Documentation/ScriptReference/UI.Dropdown.html
and:
https://docs.unity3d.com/Packages/com.unity.ugui#1.0/manual/script-Dropdown.html

unity3d: how to use OpenFolderPanel

I'm new to unity. I want to try this tutorial but I failed.
I created a GameObject - UI - Button, and tried to add script to Canvas. But I got this error. Any help would be appreciated.
OpenFolderPanel is an Editor script which lets people to make some customized function for Unity Efitor.
If you want to click button to show file dialog in your game, you can try StandaloneFileBrowser
Alright, I know your mistake. When you create a c# script, do as follows (if you want to attach it to a gameobject).
1) Select the gameobject in the heirarchy.
2) Click add component in the inspector window.
3) Type in the name of your new script, and Unity will generate a basic script derived from MonoBehaviour automatically.
BTW, game objects have monobehaviour derived scripts on them (in singleplayer). The script you are using is derived from EditorWindow, not MonoBehaviour. Personally, I have not used a custom editor, but I think the script you are using should work automatically without having to attach it to a gameobject.

Filtering an object field in Unity Editor Extension

I'm looking to use Unity's ObjectField to have users search for objects of a particular type. I know the constructor allows for a typeOf(objectType), but that does not seem to allow for filtering on custom components.
Essentially, a previous editor script I have sets up objects, and places a unique custom script on each of the components, but I've been unsuccessful in utilizing the ObjectField to search for just those objects. If I change the typeOf in my object field to be my component, it is always empty, despite there being many prefabs with that script attached in my project.
Has anyone had any success with this? Using GameObject finds them, but this finds all game objects. Is there any way to restrict this? Or to keep it only looking at particular folders?
Adding a filter while the field itself stays as type Object is a bit tricky. Here's one way.
Use ShowObjectPicker
Try opening a custom ObjectPicker. To do so you'll have to hide the default object picker and call one with a set searchFilter. It is quite a bit of work.
Setting searchFilter
Use https://docs.unity3d.com/ScriptReference/EditorGUIUtility.ShowObjectPicker.html with a searchFilter that matches the component you add to every object you want to show up in an ObjectPicker. for example "t:objectType".
There's some good information on using ShowObjectPicker at https://answers.unity.com/questions/554012/how-do-i-use-editorguiutilityshowobjectpicker-c.html
Hide default object picker
Here's a post with a way to hide the default object picker Is there any way to hide the "Object picker" of an EditorGUILayout.ObjectField in Unity Isnpector?
Show picker
You'll need to create your own Editor GUI.Button to bring up the ShowObjectPicker with the custom searchFilter.
Assets searchFilter
As a sidenote, to do the same with file assets use a "l:labelName" fileFilter instead of "t:objectType". You can set the label with the Unity Editor UI as shown here:
or with "ref:relative/path/from/assets/to/material/material.mat"

how can i regist my own property to unity native animator?

I want to edit some custom proeprty of my own script, 'TopazObject' in Unity native system. But it doesn't appear to animation window.
If the property is public then it is going to show up in the animation tab. Make sure its public. If you have a variable. If you want a variable with a custom class type to be visible there I think there is no way to do it.