UnityEditorUnity编辑器开发分享Unity教程合集

Unity之Editor-helpBox-Toggle、Butt

2017-09-11  本文已影响27人  循环渐进123456

转载:http://blog.csdn.net/liqiangeastsun/article/details/42173557

使用Unity编辑器类编辑 helpBox、Toggle、Button、Label




在Editor文件夹下创建脚本

using UnityEngine;  
using System.Collections;  
using UnityEditor;  
  
[CanEditMultipleObjects]  
[CustomEditor(typeof(TestScript))]  
public class EditorScript : Editor {  
  
    private TestScript testScript;  
    private bool isTrue = true;  
  
    private void OnEnable()  
    {  
        testScript = (TestScript)target;  
    }  
  
    public override void OnInspectorGUI()  
    {  
        EditorGUILayout.BeginVertical("box");  //开始水平布局, “box” 为一个 方框区域  
        EditorGUILayout.LabelField("ALDJLG0"); //不可点的Label  
        EditorGUILayout.SelectableLabel("adfdfdfdfdsf");  //可点的Label  
        if (GUILayout.Button("Click"))  //创建Button ,当点击按钮时,调用相应方法  
        {  
            testScript.MyTestA();  
        }               
        EditorGUILayout.EndVertical();  
        EditorGUILayout.Space();  //空格行  
  
        isTrue = EditorGUILayout.Toggle("isTrigger",isTrue);  
        EditorGUILayout.BeginVertical();  
        EditorGUI.BeginDisabledGroup(isTrue);  // 如果 isTrue为真,则下方显示在面板上(灰色)不可操作, 为假则可操作  
        if (GUILayout.Button("Button"))  
        {  
            testScript.MyTestA();  
        }  
  
        EditorGUI.EndDisabledGroup();  
        EditorGUILayout.EndVertical();  
  
        EditorGUILayout.HelpBox("help help help", MessageType.Warning);  //显示帮助框,类型为 警告  
        EditorGUILayout.Space();  
        EditorGUILayout.HelpBox("aaa aaa aaa", MessageType.Error);   //显示帮助框,类型为 错误  
    }  
}  

TestScript脚本如下,将其绑定在对象上即可

using UnityEngine;  
using System.Collections;  
  
public class TestScript : MonoBehaviour {  
  
    public void MyTestA()  
    {  
        Debug.Log("Click");  
    }  
}
上一篇下一篇

猜你喜欢

热点阅读