[转]脚本的属性显示在自定义窗口下

一般我们需要把脚本绑定在GameObject上,然后在Inspector视图下对其进行序列化编辑。假如我想把多个GameObject显示在一个自定义windows中怎么办呢?

这里我做了一个例子,把GameObject绑定在场景上,绑定在文件夹上。 呵呵~

如下图所示,我把Hierarcy视图中的gameObject拖到了场景上。然后就可以直接对其进行编辑了。

上代码

using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditorInternal;
using System.Collections.Generic;
 
 
[CustomEditor (typeof(UnityEditor.DefaultAsset))]
 
public class CustomInspector : Editor
{
 
    List<Data> datas = new List<Data> ();
 
    public override void OnInspectorGUI ()
    {
        Event e = Event.current;
        string path = AssetDatabase.GetAssetPath (target);
 
        GUI.enabled = true;
        //if (path.EndsWith (".unity")) 
        {
            Draw();
            if (e.type == EventType.DragUpdated) {
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                e.Use ();
            }
            if (e.type == EventType.DragPerform) {
 
                Object o1 = DragAndDrop.objectReferences [0];
                if (o1 is GameObject) {
                    datas.Add(new Data(){go = o1 as GameObject});
                }
            }
        }
    }
 
    Vector2 scrollPos = Vector2.zero;
    void Draw()
    {
 
        scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
        foreach(Data data in datas)
        {
            var editor = Editor.CreateEditor (data.go);
            data.fold =EditorGUILayout.InspectorTitlebar (data.fold, data.go);
 
 
            if(data.fold)
            {
                editor.OnInspectorGUI ();
                GUILayout.BeginHorizontal();
                GUILayout.Space(20);
                GUILayout.BeginVertical();
                foreach (Component c in data.go.GetComponents<Component> ()) {
                    if (!data.editors.ContainsKey (c))
                        data.editors.Add (c, Editor.CreateEditor (c));
                } 
                foreach (Component c in data.go.GetComponents<Component> ()) {
                    if (data.editors.ContainsKey (c)) {
                        data.foldouts [c] = EditorGUILayout.InspectorTitlebar (data.foldouts.ContainsKey (c) ? data.foldouts [c] : true, c);
                        if (data.foldouts [c])
                            data.editors [c].OnInspectorGUI ();
                    }
                }
                GUILayout.EndVertical();
                GUILayout.EndHorizontal();
            }
        
        }
        EditorGUILayout.EndScrollView();
    }
 
    void OnInspectorUpdate ()
    {
        Repaint ();
    }
 
    class Data
    {
        public GameObject go;
        public bool fold = true;
        public Dictionary<Object , Editor> editors = new Dictionary<Object, Editor> ();
        public Dictionary<Object, bool> foldouts = new Dictionary<Object, bool> ();
    }
 
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,618评论 19 139
  • 任何人若有闲情逸致,那么一山一丘常隐无限趣味!我的家乡在祖国边陲,在河谷深处。这里既有“大漠孤烟直”的雄浑,...
    陌上花开陌上阅读 5,054评论 2 8
  • 从小到大,从生到死,我们做了很多违心的事,不管自己是否懊悔还是坦然处之,至少在那一刻,我们是违拗心意。拿说谎来说吧...
    逐日钩沉阅读 4,132评论 2 3
  • 今天的内容是:为一件大事服务 翻译:Serving for a great event. 解读:找到一件想完成的事...
    长期主义者庆福阅读 1,157评论 0 1