当前位置:网站首页>【Unity】编辑器扩展-04-拓展Scene视图
【Unity】编辑器扩展-04-拓展Scene视图
2022-07-31 07:52:00 【韩天衣】
【Unity】编辑器扩展-04-拓展Scene视图
辅助元素
借助Gizmo绘制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Script_12 : MonoBehaviour
{
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position,Vector3.one);
Gizmos.DrawCube(Vector3.one,Vector3.one);
}
}
辅助UI
我们使用EditorGUI,可以方便的在视图中处理一些操作事件。
我们可以在Scene视图中绘制辅助UI,EditorGUI代码需在Handles.BeginGUI()
和Handles.EndGUI()
之间绘制完成。
注意:编辑器绘制脚本如果存在多个只有第一个能够生效
[CustomEditor(typeof(Camera))]
public class Script_13 : Editor
{
private void OnSceneGUI()
{
Camera camera = target as Camera;
if (camera != null)
{
Handles.color = Color.red;
Handles.Label(camera.transform.position,camera.transform.position.ToString());
Handles.BeginGUI();
GUI.backgroundColor = Color.red;
if (GUILayout.Button("click", GUILayout.Width(200f)))
{
Debug.LogFormat("click = {0}",camera.name);
}
GUILayout.Label("Label");
Handles.EndGUI();
}
}
}
常驻辅助UI
辅助UI需在选中对应的游戏对象才会显现,我们可以使得EditorGUI常驻显示在Scene视图中
将绘制函数绑定至委托duringSceneGui
using System.Collections;
using System.Collections.Generic;
using UnityEngine;ß
using UnityEditor;
public class Script_14
{
[InitializeOnLoadMethod]
static void InitializeOnLoadMethod()
{
SceneView.duringSceneGui += delegate
{
Handles.BeginGUI();
if (GUI.Button(new Rect(0, 0, 50, 20), "click"))
{
Debug.Log("常驻UI");
}
GUI.Label(new Rect(0, 20, 50, 25), "click-label");
Handles.EndGUI();
};
}
}
禁用选中目标
FocusType.Passive
表示禁止接收控制焦点,获取他的ID,将禁止点击事件穿透下去
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_15
{
[InitializeOnLoadMethod]
static void InitializeOnLoadMethod()
{
SceneView.duringSceneGui += delegate
{
Event e = Event.current;
if (e != null)
{
int controlID = GUIUtility.GetControlID(FocusType.Passive);
if(e.type == EventType.Layout)
{
HandleUtility.AddDefaultControl(controlID);
}
}
};
}
}
另外一种方法:锁层
直接在Scene视图中选中目标很容易选中子节点,我们可以给他绑定一个[SelectionBase]标记,这样依赖该脚本下的所有节点都会定位到绑定这个标记的对象上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[SelectionBase]
public class RootScript : MonoBehaviour
{
}
边栏推荐
猜你喜欢
哆啦a梦教你页面的转发与重定向
How on one machine (Windows) to install two MYSQL database
SSM整合案例分析(详解)
如何升级nodejs版本
MySQL installation to the last step in the write the configuration file failed?And after the installation steps
【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)-- 组件UI
New in Visual Studio: Low Priority Builds
Calculation example of matlab program iEEE9 node system for power flow calculation of AC-DC hybrid system based on alternate iteration method
MySQL detailed explanation
【云原生&微服务五】Ribbon负载均衡策略之随机ThreadLocalRandom
随机推荐
全国中职网络安全B模块之国赛题远程代码执行渗透测试 PHPstudy的后门漏洞分析
【MySQL功法】第4话 · 和kiko一起探索MySQL中的运算符
《opencv学习笔记》-- 仿射变换
Regarding "computing power", this article is worth reading
哪些字符串会被FastJson解析为null呢
[MySQL exercises] Chapter 3 Common data types in MySQL
[Yellow ah code] Introduction to MySQL - 3. I use select, the boss directly drives me to take the train home, and I still buy a station ticket
PHP中 比较 0、false、null,‘‘ “
mysql插入新字段方法
【黄啊码】MySQL入门—3、我用select ,老板直接赶我坐火车回家去,买的还是站票
【Objective-C语言中的@property】
CY7C68013A之LED闪烁
NK-RTU980烧写裸机程序
[What is the role of auto_increment in MySQL?】
【MySQL功法】第2话 · 数据库与数据表的基本操作
Vue项目通过node连接MySQL数据库并实现增删改查操作
Open Source | Commodity Recognition Recommender System
"C language" frog jumping steps recursion problem
Matlab学习第一天(持续更新中)
MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql