当前位置:网站首页>【Unity】编辑器扩展-02-拓展Hierarchy视图
【Unity】编辑器扩展-02-拓展Hierarchy视图
2022-07-31 07:52:00 【韩天衣】
【Unity】编辑器扩展-02-拓展Hierarchy视图
拓展菜单
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_04 : MonoBehaviour
{
//Hierarchy视图菜单本质上是窗体上的GameObject下的子菜单,只需要注意路径即可,同理之前的也是一样Project视图菜单是Asset的子菜单,如果没有已知路径就会自建新根菜单
[MenuItem("GameObject/My Create/Cube",false,0)]
static void CreatePlayer()
{
GameObject.Instantiate(Resources.Load("Prefabs/Cube"));
}
}
拓展布局
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_05
{
[InitializeOnLoadMethod]
static void InitializeOnLoadMethod()
{
EditorApplication.hierarchyWindowItemOnGUI += delegate (int instanceID,Rect selectionRect)
{
if (Selection.activeObject && instanceID == Selection.activeObject.GetInstanceID())
{
float width = 50f;
float height = 15f;
selectionRect.x += (selectionRect.width - width);
selectionRect.width = width;
selectionRect.height = height;
if (GUI.Button(selectionRect, "click"))
{
Debug.LogFormat("click:{0}",Selection.activeObject.name);
}
}
};
}
}
重写菜单
我们已经知道了可以在原有菜单的基础上进行扩展,那么我们也可以完全抛弃原有菜单,完全自定义自己的菜单。
基本原理就是判断情况并陈列自己的菜单
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_06
{
[MenuItem("Window/Test/Player")]
static void Test()
{
}
[MenuItem("Window/Test/Enemy")]
static void Test1()
{
}
[MenuItem("Window/Test/UI")]
static void Test2()
{
}
[InitializeOnLoadMethod]
static void StartInitializeOnLoadMethod()
{
EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
}
static void OnHierarchyGUI(int instanceID,Rect selectionRect)
{
if (Event.current != null && selectionRect.Contains(Event.current.mousePosition) && Event.current.button == 1)
{
GameObject selectedGameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (selectedGameObject)
{
Vector2 mousePosition = Event.current.mousePosition;
EditorUtility.DisplayPopupMenu(new Rect(mousePosition.x,mousePosition.y,0,0),"Window/Test",null);
Event.current.Use();
}
}
}
}
覆盖菜单
我们还可以重写系统自带的菜单行为。例如,我觉得Unity创建的Image组件不好,那么可以复写他的行为。
创建Image组件时,会自动勾选RaycastTarget。如果图片不需要处理点击事件,则带来了额外开销。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
public class Script_07
{
[MenuItem("GameObject/UI/Image")]
static void CreateImage()
{
if (Selection.activeTransform)
{
if (Selection.activeTransform.GetComponentInParent<Canvas>())
{
Image image = new GameObject("Image").AddComponent<Image>();
image.raycastTarget = false;
image.transform.SetParent(Selection.activeTransform,false);
Selection.activeTransform = image.transform;
}
}
}
}
边栏推荐
猜你喜欢
【MySQL功法】第5话 · SQL单表查询
如何使用mysql binlog 恢复数据
使用PageHelper实现分页查询(详细)
分布式缓存系统必须要解决的四大问题
@Transactional注解的失效场景
[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Part 1)--Component UI
First acquaintance with NK-RTU980 development board
[Interview: Concurrency 38: Multithreading: Thread Pool] Basic concepts of the ThreadPoolExecutor class
【MySQL功法】第3话 · MySQL中常见的数据类型
sqli-labs(less-11)
随机推荐
《c语言》青蛙跳台阶递归问题
[PSQL] SQL Basic Course Reading Notes (Chapter1-4)
shell/bash脚本命令教程
【云原生与5G】微服务加持5G核心网
Navicat new database
开源|商品识别推荐系统
安装部署KubeSphere管理kubernetes
[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Middle)--Search Suggestions
Calculation example of matlab program iEEE9 node system for power flow calculation of AC-DC hybrid system based on alternate iteration method
NK-RTU980烧写裸机程序
Failure scenarios of @Transactional annotations
0730~Mysql优化
[Cloud native and 5G] Microservices support 5G core network
【C#】判断字符串中是否包含指定字符或字符串(Contains/IndexOf)
35-Jenkins-Shared library application
MySQL安装教程
SQL语句知识大全
Vscode: Project-tree plugin
奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史