Unity编辑器的开发(1)

记录

学习过程中记录一下

第一个脚本

  • 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
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值