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

查看:24
本文介绍了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.

Add a UnityEvent in your actual script.

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天全站免登陆