IntSlider回调函数?编辑器脚本 [英] IntSlider callback function? Editor Scripting

查看:128
本文介绍了IntSlider回调函数?编辑器脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近刚接触编辑器脚本,但仍然掌握了所有基础知识.我想知道IntSlider(我们用来创建整数值的滑块)是否具有可用于获取更多功能的方法或函数?

I just got into Editor Scripting recently and I'm still getting a hold of all the basics. I wanted to know if the IntSlider (that we use to create a slider of integer values) has any methods or functions that can be used to get more functionality?

类似于Slider类的内容:
您具有-maxValue,-minValue,-value,-onValueChanged(在更改滑块的值时执行回调)

Something like the Slider class:
You have -maxValue, -minValue, -value, -onValueChanged (Callback executed when the value of the slider is changed)

这些工作在播放"模式下进行.

These work during the "Play" mode.

我需要在编辑器中访问此信息. 具体来说:我需要访问IntSlider的onValueChanged(如果存在),以便为它分配一个函数以执行.

I need to access this information while in the editor. Specifically: I need to access the onValueChanged (if it exists) of the IntSlider so I can assign a function for it to execute.

IntSlider代码:

IntSlider Code:

totalRooms = EditorGUILayout.IntSlider(new GUIContent ("Total Rooms"), totalRooms, 0, 10);

有没有办法为检查员实现这一目标?或者可以创建解决方案吗?如果您能指出正确的方向,我将不胜感激.

Is there a way to achieve this for the inspector? Or something that can be created to solve this? If you can point me in the right direction, I'd be grateful.

我正在使用Unity 5.3.1f

I'm using Unity 5.3.1f

推荐答案

Unity目前没有此类事件.但是,可以像这样轻松实现:

There is no such event in Unity at the moment. However this can be implemented easily like this:

在实际脚本中添加UnityEvent.

public UnityEvent OnVariablesValueChanged;

然后在编辑器脚本中检查值是否已更改. (注意:我试图为此编写易于理解的代码示例.您可以相应地对其进行更改)

Then in your editor script check if value is changed. (Note: I tried to write easily understandable code sample for this. you can change it accordingly)

[CustomEditor(typeof(MyScript))]
public class MyScriptEditor : Editor 
{
    public override void OnInspectorGUI()
    {
        MyScript myTarget = (MyScript)target;
        int prevValue = myTarget.variable;
        myTarget.variable = EditorGUI.IntSlider(new Rect(0,0,100, 20), prevValue, 1, 10);
        int newValue = myTarget.variable;
        if (prevValue != newValue)
        {
            Debug.Log("New value of variable is  :" + myTarget.variable);
            myTarget.OnVariablesValueChanged.Invoke();
        }
        base.OnInspectorGUI();
    }
}

然后从检查器将侦听器添加到OnVariablesValueChanged事件.

Then add listener to OnVariablesValueChanged event from inspector.

希望这会有所帮助.

这篇关于IntSlider回调函数?编辑器脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆