unity Editor EditorWindow Grid 使用 笔记


创建一个Grid脚本
创建一个C#脚本Grid.cs,我们用它来为一个物体绘制我们自己的gizmo;我们会在编辑器中绘制一个简单的网格(Grid)来作为一个例子。
为了在编辑器中绘图,我们需要使用OnDrawGizmo回调函数,因此让我们创建它。


public class Grid : MonoBehaviour
{
    public float width = 32.0f;
    public float height = 32.0f;


    public Color color = Color.white;
//我们需要一系列水平线和垂直线,并且还要知道编辑器摄像机
    void OnDrawGizmos()//为了在编辑器中绘图,我们需要使用OnDrawGizmo回调函数
    {
Vector3 pos = Camera.current.transform.position;
Gizmos.color = color;
for (float y = pos.y - 800.0f; y < pos.y + 800.0f; y += height)
{
Gizmos.DrawLine(new Vector3(-10000.0f, Mathf.Floor(y/height) * height, 0.0f),
new Vector3(10000.0f, Mathf.Floor(y/height) * height, 0.0f));
}
 
for (float x = pos.x - 1200.0f; x < pos.x + 1200.0f; x += width)
{
Gizmos.DrawLine(new Vector3(Mathf.Floor(x/width) * width, -10000.0f, 0.0f),
new Vector3(Mathf.Floor(x/width) * width, 10000.0f, 0.0f));
}
    }
}


要观察网格,你需要创建一个空物体,然后把我们的脚本Grid.cs绑定到它上




创建一个自定义的Inspector
接下来的工作就是自定义Inspector。我们需要创建一个编辑器脚本来做这件事。创建一个新的C#文件,
命名为GridEditor。这个脚本需要放置在Editor文件夹中;如果你还没有一个Editor文件夹,那么现在就创建它吧。


using UnityEngine;
using UnityEditor;
using System.Collections;
 
[CustomEditor (typeof(Grid))]//[CustomEditor(typeof(Grid))]会告诉Unity我们想要自定义Grid的inspector
public class GridEditor : Editor //使用Editor
   Grid grid;
   public void OnEnable()
   
    {
        grid = (Grid)target;//应该在OnEnable()函数中为它赋值该函数会在inspector可用时调用。
SceneView.onSceneGUIDelegate += GridUpdate;//设置委托 使用 + 来实现
    }
  public override void OnInspectorGUI()//重载OnInspectorGUI
    {
// base.OnInspectorGUI();
GUILayout.BeginHorizontal();//表示接下来元素要从左向右放置
GUILayout.Label("Grid Width");
grid.width = EditorGUILayout.FloatField(grid.width, GUILayout.Width(50));
GUILayout.EndHorizontal();//结束这种放置方式
//以上为一行
GUILayout.BeginHorizontal();
GUILayout.Label("Grid Height");
grid.height = EditorGUILayout.FloatField(grid.height, GUILayout.Width(50));
GUILayout.EndHorizontal();

if (GUILayout.Button("Open Grid Window", GUILayout.Width(255)))
{
GridWindow window = (GridWindow) EditorWindow.GetWindow(typeof(GridWindow));
window.Init();
}
SceneView.RepaintAll(); //Inspector改变后立即刷新属性
    }
public void OnDisable()
    {
SceneView.onSceneGUIDelegate -= GridUpdate;//删除委托 使用 - 来实现
    }
void GridUpdate(SceneView sceneview)
{
Event e = Event.current;
if (e.isKey && e.character == 'a')
{
Ray r = Camera.current.ScreenPointToRay(new vector3(e.mousePosition.x,-e.mousePosition.y + Camera.current.pixelHeight));
GameObject obj;
Object prefab = PrefabUtility.GetPrefabParent(Selection.activeObject);
Undo.IncrementCurrentGroup();//这个保证单个进行创建的撤销
if (prefab)
{
obj = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
Vector3 aligned = new Vector3(Mathf.Floor(mousePos.x/grid.width)*grid.width + grid.width/2.0f,
Mathf.Floor(mousePos.y/grid.height)*grid.height + grid.height/2.0f, 0.0f);
obj.transform.position = aligned;
Undo.RegisterCreatedObjectUndo(obj,"Create"+obj.name);//可以进行撤销的操作
}
}
else if (e.isKey && e.character == 'd')
{
Undo.IncrementCurrentGroup();
foreach (GameObject obj in Selection.gameObjects)
Undo.DestroyImmediate(obj);//可以撤销的删除
}
}
}


 创建一个编辑器窗口脚本
创建一个新的脚本,并且让它继承EditorWindow而不是Editor。我们把它命名为GridWindow.cs


现在,让我们回到GridWindow脚本中来,在窗口中创建一个颜色字段以便我们可以在窗口中拾取颜色。我们可以在OnGUI()回调函数中做这件事。


public class GridWindow : EditorWindow
{
    Grid grid;
 
    public void Init()
    {
        grid = (Grid)FindObjectOfType(typeof(Grid));
    }
 
    void OnGUI()
    {
        grid.color = EditorGUILayout.ColorField(grid.color, GUILayout.Width(200));
    }
}
 
好了,现在你可以在编辑器中检查是否一切工作正常。 


 
结论:
这就是编辑器脚本的介绍。如果你想扩充你的知识,在Unity脚本手册中有很多可阅读的话题——根据你的需要你可能想要查看Resources, AssetDatabase或者FileUtil类来了解更多内容。
不幸的是,一些类还没有被写进文档。由于这个原因,很容易导致不能工作。例如,SceneView类和它的函数或者是Undo类中的Undo.IncrementCurrentEventIndex()函数。如果文档中没有提供你要找的答案,你可能需要在UnityAnswers或者Unity Forum中搜索一下了。
感谢您花时间阅读这篇文章!
写在最后的话
点击这里,查看英文原版。 
点击这里,查看中文翻译版。 

该文章来源于Envato网站上的Tuts+,版权属原作者所有!如果转载,请保留到原文的链接!

传送门; http://www.w2bc.com/Article/12021

传送门; https://code.tutsplus.com/tutorials/how-to-add-your-own-tools-to-unitys-editor--active-10047

传送门; http://www.cnblogs.com/champ/p/4148875.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值