记录
学习过程中记录一下
第一个脚本
- using UnityEditor;这是必须要引入的
- [MenuItem(“我的工具/HelloWorld工具/创建HelloWorld”)] 这里就会在菜单栏目创建对应的菜单
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class HelloWorld
{
[MenuItem("我的工具/HelloWorld工具/创建HelloWorld")]
private static void CreateHelloWorldGameObject()
{
bool res = EditorUtility.DisplayDialog("Hellow World", "确定创建吗", "创建", "取消");
if (res)
{
new GameObject("HelloWorld");
}
}
}
效果
Api
1、 EditorUtility.DisplayDialog
bool res = EditorUtility.DisplayDialog("Hellow World", "确定创建吗", "创建", "取消");
Editor
关于Editor文件夹,只要把代码放到Editor文件夹下,那么是打包打不进去的,默认是编辑器的专用脚本,这个Editor可以是…/Editor/… 并不看Editor放在哪,可以有n个Editor文件夹。
演示一下Editor和非Editor文件夹下面的区别
没有Editor文件夹
有Editor文件夹,并将代码放进去
会发现,工程会自动将Editor文件下的代码都放入自动创建的Editor项目中
即使创建了多个Editor,都会讲Editor下面的代码都放入新项目中
Editor注意
Editor下面的东西是不会被打入到包里面的,只会把Assembly-CSharp打成dll
如果不把编辑器代码放入到Editor下,打包时候会报错
这是因为,using UnityEditor;并不会被打入到包体中,如果想要成功,有两种方法
- 1、编辑器代码放入到Editor下面
- 2、条件编译
/*
#if UNITY_EDITOR
#endif
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class HelloWorld
{
#if UNITY_EDITOR
[MenuItem("我的工具/HelloWorld工具/创建HelloWorld")]
private static void CreateHelloWorldGameObject()
{
bool res = EditorUtility.DisplayDialog("Hellow World", "确定创建吗", "创建", "取消");
if (res)
{
new GameObject("HelloWorld");
}
}
#endif
}