当前位置:网站首页>【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
{
}
边栏推荐
- Machine Learning - Notes and Implementation of Linear Regression, Logistic Regression Problems
- 【云原生与5G】微服务加持5G核心网
- 【MySQL功法】第5话 · SQL单表查询
- sqli-labs(less-11)
- Install the deployment kubernetes KubeSphere management
- 关于@Autowired
- 个人报错问题 持续总结
- 分布式缓存系统必须要解决的四大问题
- 35-Jenkins-Shared library application
- Cloud server deployment web project
猜你喜欢

TypeError The view function did not return a valid response. The function either returned None 的解决

力扣 593. 有效的正方形

安装部署KubeSphere管理kubernetes

0730~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](/img/7b/f50c5f4b16a376273ba8cd27543676.png)
[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

云服务器部署 Web 项目

《c语言小游戏》入门级三子棋游戏(机器人加强版)

Visual Studio新功能出炉:低优先级构建

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)

免安装版的Mysql安装与配置——详细教程
随机推荐
SQLAlchemy使用教程
XSS靶场prompt.ml过关详解
深度学习随机数设置,保证实验的可重复性
[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Part 1)--Component UI
Vscode:Project-tree插件
关于yum源的配置及更新
关于Error EPERM operation not permitted, mkdir...几种解决办法的比较
关于“算力”,这篇文章值得一看
The torch distributed training
Vscode: Project-tree plugin
【C#】判断字符串中是否包含指定字符或字符串(Contains/IndexOf)
实用生物信息学2:多组学数据整合和挖掘
使用MySQL如何查询一年中每月的记录数
WLAN部署(AC+AP)配置及常见问题记录
会话技术之Coookie && Session详解
《c语言》青蛙跳台阶递归问题
MySQL detailed explanation
Collation and sharing of related classic papers and datasets in the field of deep learning communication
奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!
全国中职网络安全B模块之国赛题远程代码执行渗透测试 PHPstudy的后门漏洞分析