unity

(一)Unity编辑器扩展之Project视图面板

こ雲淡風輕ζ 提交于 2019-12-03 15:49:53
1、前言 研究一下Unity编辑器的扩展,并记录一下,防止以后以往,也是一种交流吧。Unity编辑器有五大面板,Project、Game、Scene、Hierarchy、Inspector,先来说一下Project面板。 2、拓展右键菜单 主要用到[MenuIteam(“Assets/名字”,bool(为真则禁用),int(排序,值小在最上面)],直接上代码 using UnityEngine; using UnityEditor; public class ControllerEditor { //------------------扩展Project面板-------------------- //-----------------拓展右键菜单----------------- //---------[MenuIteam("Assets/名字",bool(为真则禁用),int(排序,值小在最上面)] [MenuItem("Assets/MyTools/Tools1",false,2)] static void MyTools1() { Debug.Log(Selection.activeObject.name); } [MenuItem("Assets/MyTools/Tools2",false,9)] static void MyTools2() { Debug.Log

1.1设计模式在Unity中应用—Behavioral Pattern— Chain of Responsibility Pattern 责任链模式

落花浮王杯 提交于 2019-12-03 15:47:37
Definition Participants Handler Approver ConcreteHandler Director VicePresident President Client ChainApp 责任链模式代码结构 案例1公司账单报销 案例2加减乘除 Definition Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. 使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止。 Participants The classes and objects participating in this pattern are: Handler (Approver) defines an interface for handling the requests (optional) implements the

1.2设计模式在Unity中应用—Behavioral Pattern— Command Pattern 命令模式

廉价感情. 提交于 2019-12-03 15:47:24
Command Pattern 命令模式 Definition Participants Command ConcreteCommand Client Invoker Receiver 责任链模式代码结构 案例1加减乘除计算器可以执行撤销 重复 命令 Command Pattern 命令模式 Definition Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. 命令模式将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象,同时支持可撤消的操作。 Participants The classes and objects participating in this pattern are: Command declares an interface for executing an operation ConcreteCommand defines a binding between a Receiver object and an action implements Execute by

Unity Editor 编辑器扩展 十五 提取NGUI 的Transform ResetPosition 功能

流过昼夜 提交于 2019-12-03 15:47:14
NGUI虽然不怎么在项目中应用了,但是里面这个ResetPosition功能还是非常好用的,可以拿出来在新项目中,实测可用 代码放进Editor文件夹里面就可以用了 using UnityEngine; using UnityEditor; [CustomEditor( typeof (Transform))] public class TransformInspector : Editor { /// <summary> /// Draw the inspector widget. /// </summary> public override void OnInspectorGUI () { Transform trans = target as Transform; EditorGUIUtility.LookLikeControls( 15 f); Vector3 pos; Vector3 rot; Vector3 scale; // Position EditorGUILayout.BeginHorizontal(); { if (DrawButton( "P" , "Reset Position" , IsResetPositionValid(trans), 20 f)) { //NGUIEditorTools.RegisterUndo("Reset Position",

Unity3D利用代码生成脚本模板

点点圈 提交于 2019-12-03 15:46:10
Unity3D利用代码生成脚本模板 目录 1、博客介绍 2、内容 (1)生成脚本展示 (2)核心 3、推送 4、结语 1、博客介绍 最近一直在学习框架,看到了一点关于在编辑器下去自动生成UI相关代码的逻辑,感觉很好用,就拆解的去学习,这边博客先介绍一下利用代码去生成脚本,我看其他的博客生成脚本的时候基本都是使用StringBuilder去一行一行的拼,感觉很麻烦,看了凉鞋大大地QFramework中生成的部分,中间是通过一个模本脚本来生成自己需要的脚本,感觉很方便,这里博主写的很简单,凉鞋老师的QFramework中有更加丰富的写法和用法,有兴趣的同学可以跳转一下,结语有推送。 2、内容 (1)生成脚本展示 (2)核心 第一项:创建我们的模板脚本,我们需要在这个模板脚本内写下我们需要生成的一些通用方法。 using UnityEngine; namespace TemplateNameSpace { /// <summary> /// 代码模板 /// Author:Sun /// Time:2019/5/29 23:58 /// </summary> public class ScriptTemplate : MonoBehaviour { public void OnInit() { } public void OnEvent() { } public void OnShow(

unity Editor自动生成材质及动态加载资源

寵の児 提交于 2019-12-03 15:45:42
最近这两天在做一个项目,然后里面有六十多个素材,还要全部生成材质球,差点人都废了,然后去手册上着了一下可以自动生成材质球的代码。然后自动生成材质球的过程中我还想要给材质球赋予一个默认的漫反射贴图,贴图是从网上下载来的,踩了数不清的坑之后终于搞定了。 直接贴代码 // Use this for initialization void Start() { StartCoroutine(DownLoadFiles()) ; } public IEnumerator DownLoadFiles() { //使用UnityWebRequest下载纹理 using (UnityWebRequest www = UnityWebRequest .GetTexture ( "http://pic1.16pic.com/00/04/57/16pic_457578_b.jpg" )) { yield return www .Send () ; if (www .isError ) { Debug .Log (www .error ) ; } else { Debug .Log (www .downloadHandler .data .Length ) ; try { FileInfo fi = new FileInfo( "Assets/Pictures/image.png" ) ; if (!fi

Unity Editor 编辑器扩展 六 EditorWindow

自闭症网瘾萝莉.ら 提交于 2019-12-03 15:45:25
目录 窗口弹出框 窗口创建查找游戏物体 为窗口添加图标和菜单 EditorWindow的简单用法之前都用过了,这里介绍一些特殊的用法 窗口弹出框 在Editor下创建脚本如下: using UnityEditor; using UnityEngine; public class WindowExample1 : EditorWindow { [MenuItem( "Window/WindowExample1" )] static void Open () { GetWindow<WindowExample1> (); } ExamplePupupContent popupContent = new ExamplePupupContent (); void OnGUI () { if (GUILayout.Button ( "PopupContent" ,GUILayout.Width( 128 ))) { var activatorRect = GUILayoutUtility.GetLastRect (); PopupWindow.Show (activatorRect, popupContent); } } } //弹窗 public class ExamplePupupContent : PopupWindowContent { public override void

代码更改Unity设置中的ICON

醉酒当歌 提交于 2019-12-03 15:44:48
下面的代码是自己研究的可以实现更改unity设置中ICON的功能。 void SetDefaultIcon(Texture2D tex) { var getIconFormPlatform = typeof(PlayerSettings).GetMethod("GetIconsForPlatform", BindingFlags.NonPublic | BindingFlags.Static); var getIconSizesForPlatform = typeof(PlayerSettings).GetMethod("GetIconSizesForPlatform", BindingFlags.NonPublic | BindingFlags.Static); var setIconsForPlatform = typeof(PlayerSettings).GetMethod("SetIconsForPlatform", BindingFlags.NonPublic | BindingFlags.Static); var array = (Texture2D[])getIconFormPlatform.Invoke(null, new object[] { string.Empty }); var iconSizesForPlatform = (int[]

Unity Editor 编辑器扩展 八 Undo

不想你离开。 提交于 2019-12-03 15:44:28
Unity的Undo撤销操作也是可以自定义的,当我们将自己的操作组册到Undo事件中,那么,我们就可以按照自己的方式撤销操作了。 目录 Undo操作的简单测试 Undo操作的分组折叠 对选中物体操作并注册Undo练习 将Undo操作分组撤销到指定位置 Undo操作的简单测试 下面是一些撤销测试,非常简单,直接看代码 using UnityEngine; using UnityEditor; public class UndoTest { //把选中物体位置归零 [MenuItem( "Undo/RecordObject" )] static void RecordObject () { Transform transform = Selection.activeTransform; Undo.RecordObject (transform, "Pos" ); transform.position = new Vector3 ( 0 , 0 , 0 ); } //选中物体添加刚体组件 [MenuItem( "Undo/AddComponent" )] static void AddComponent () { GameObject go = Selection.activeGameObject; // 添加组件,执行撤销后将被删除 Rigidbody rigidbody = Undo

unity编辑器扩展

一个人想着一个人 提交于 2019-12-03 15:44:08
一、在菜单栏添加按钮、在project和Herarity里点击鼠标右键出现按钮 MenuItem using UnityEditor; using UnityEngine; //Editor下文件不会被打包,只能在编辑器下起作用 public class Tools { //第一种 菜单栏添加按钮 //第三个参数为显示的优先级,优先级越小越在上,每一个菜单栏的priority的优先级默认为1000,如果相邻的优先级大于11,就相当于分类 [MenuItem("Tools/Test1",false,4)] static void Test1() { Debug.Log("添加成功!"); } //在GameObject菜单栏和Hierarchy视图里点击鼠标右键显示 [MenuItem("GameObject/my tool",false,10)] static void Test2() { Debug.Log("添加成功!"); } //在菜单栏和project里点击鼠标右键显示AssetButton [MenuItem("Assets/assetButton")] static void Test3() { Debug.Log("添加成功!"); } //资源商店下载的存放位置 C:\Users\huang\AppData\Roaming\Unity\Asset Store-5