Unity EditorWindow 笔记

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:00   1964   0

一:功能

1.实例化

 //设置插件在菜单栏的位置   和快捷键  
    [MenuItem("YCC's Tools/模型更改/更改父物体和测量长度 %W")]
    //实例化窗体  
    static void Init()
    {
        myTools window = (myTools)EditorWindow.GetWindow(typeof(myTools));
        window.titleContent.text = "更改父物体/测长";
        window.Show();
    }

2.选项卡制作

 //用GUI画出窗体的空间布局  
    void OnGUI()
    {
        toolbarOption = GUILayout.Toolbar(toolbarOption, toolbarTexts);
        switch (toolbarOption)
        {
            case 0:
                fnChangeParent();
                break;
            case 1:
                fnLength();
                break;
        }
    }

3.多个物体更改模型父物体

void fnChangeParent()
    {
        GUILayout.BeginHorizontal("box");
        GUILayout.Label("父物体:", EditorStyles.boldLabel);
        ObjParent = EditorGUILayout.ObjectField(ObjParent, typeof(Transform)) as Transform;
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal("box");
        iChildCount = Selection.transforms.Length;//获取当前鼠标选中的物体个数
        if (Selection.transforms.Length > 0)
        {
            GUILayout.Label("当前选中子物体个数:"+iChildCount, EditorStyles.boldLabel);
            if (GUILayout.Button("应用"))
            {
                if (ObjParent != null)
                {
                    for (int i = 0; i < iChildCount; i++)
                    {
                        if (Selection.transforms[i].parent != ObjParent)
                            Selection.transforms[i].parent = ObjParent;
                    }
                    EditorUtility.DisplayDialog("提示", "已更换父物体", "确定");//显示对话框 DisplayDialog (title : string, message : string, ok : string, cancel : string = "") : bool
                }
                else
                    this.ShowNotification(new GUIContent("当前没有父物体!"));//显示通知
            }
        }
        else
            GUILayout.Label("当前没有选中子物体" , EditorStyles.boldLabel);
        GUILayout.EndHorizontal();
    }

4.测量两个物体在场景中的距离

 void fnLength()
    {
        GUILayout.BeginHorizontal("box");
        GUILayout.Label("测量基准物体1:", EditorStyles.boldLabel);
        T1 = EditorGUILayout.ObjectField(T1, typeof(Transform)) as Transform;
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal("box");
        GUILayout.Label("测量参考物体2:", EditorStyles.boldLabel);
        T2 = EditorGUILayout.ObjectField(T2, typeof(Transform)) as Transform;
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        if (T1 != null && T2 != null)
        {
            Debug.DrawLine(T1.position, T2.position, Color.red);
            //if (GUILayout.Button("查    寻"))
            //{  
            fDistance = Vector3.Distance(T1.position, T2.position);
            //}
            GUILayout.Label("距离:", EditorStyles.boldLabel);
            EditorGUILayout.FloatField(fDistance, EditorStyles.boldLabel);
        }
        GUILayout.EndHorizontal();
    }

二:注意

1.重绘

 //在OnInspectorUpdate上调用重绘,因为它在窗口上较少重绘,就好象是OnGUI/Update
    void OnInspectorUpdate()
    {
        Repaint();//重绘
    }

2.错误

出现错误: Invalid editor window UnityEditor.FallbackEditorWindow 解决方法:Layout-> Revert Factory Settings

转载于:https://www.cnblogs.com/YDoubleC/p/6201736.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP