首页 >Unity3D频道 >【Unity3D拓展编辑器】 > Unity3D研究院编辑器之脚本的属性显示在自定义窗口下(十八)
2015
12-30

Unity3D研究院编辑器之脚本的属性显示在自定义窗口下(十八)

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

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

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

Snip20151230_5

 

上代码

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> ();
	}

}

放到你的工程里就可以直接看到效果。 另外,这里我没做窗口数据的序列化 ,祝大家学习愉快。。

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

Unity3D研究院编辑器之脚本的属性显示在自定义窗口下(十八)》有 8 条评论

  1. LiTaotao说:

    没明白怎么操作?????

    • 李哈哈说:

      我也第一次接触,花了点时间。
      首先,复制脚本内容创建脚本,并创建 Editor 文件夹,并将脚本放于其下,准备操作即可。
      如果你的版本不小于5 不大于 5.3 (测试版本5.6.0f3,有点小问题等会介绍)。
      在 Project 面板中 选择 文件夹 或者 Scene文件,然后将 Hierarchy 面板中的 GameObject 拖进 Inspector 中即可看到效果。
      如果你的版本大于5.3 出现 Scene 场景文件无法将 GameObject 拖进 Inspector 中的时候,则将第8行中的 DefaultAsset 更换为 SceneAsset 即可。
      但是如此操作,则这个脚本无法同时对 文件夹 和 Scene 场景文件生效,不知 雨松大大 有没有什么解决办法。

  2. 把Hierarcy视图中的gameObject拖到了场景上 ???我的怎么不能拖动??

  3. 李奇说:

    这个Logo放的位置挡住了…………

  4. pellen说:

    大大,UnityEditor.DefaultAsset,报错

留下一个回复

你的email不会被公开。 必填项已用 * 标注。