unity编辑器拓展二——一键调整scene中物件坐标朝向
来源:互联网 发布:如何查看手机mac地址 编辑:程序博客网 时间:2024/05/21 21:41
1.描述
在项目中,我们遇到很多这种情况,美术制作的时候从来不管坐标朝向,真正使用起来就会有各种问题。
需要美术来调整坐标朝向的时候,物体数量很多,就会很麻烦,很枯燥的工作,还得耐主性子一个一个调整。
如图,我们今天要做的是不动任何物体的情况下,一键控制物体的坐标朝向
2.步骤分析
1.绘制窗口部分
2.lab绘制
3.button绘制
4.功能实现
代码如下:
using System.Collections.Generic;using UnityEngine;using UnityEditor;public class SceneTool : EditorWindow{ [MenuItem("Owen/Tool")] //绘制窗口 static void tranPoint() { SceneTool win = (SceneTool)EditorWindow.GetWindow(typeof(SceneTool), false, "Tool", false); win.Show(); } void OnGUI() { //设置字体类型 GUIStyle style1 = new GUIStyle(); //字体大小为15 style1.fontSize = 15; //字体颜色为灰白色 style1.normal.textColor = new Color(0.7f, 0.7f, 0.7f); GUIStyle style2 = new GUIStyle(); style2.fontSize = 13; style2.normal.textColor = new Color(0.7f, 0.7f, 0.7f); //垂直绘制 GUILayout.BeginHorizontal(); { //lab的绘制 EditorGUILayout.LabelField("▼TransformDirection", style1); //按钮的绘制 if (GUI.Button(new Rect(100, 40, 25, 25), "↑")) { if (Selection.activeGameObject) { forward(); } } //坐标朝向-z方向 按钮触发 if (GUI.Button(new Rect(100, 100, 25, 25), "↓")) { if (Selection.activeGameObject) back(); } //坐标朝向-x方向 按钮触发 if (GUI.Button(new Rect(70, 70, 25, 25), "←")) { if (Selection.activeGameObject) left(); } //坐标朝向x方向 按钮触发 if (GUI.Button(new Rect(130, 70, 25, 25), "→")) { if (Selection.activeGameObject) right(); } } GUILayout.EndHorizontal(); } //坐标朝向Z轴方向 void forward() { Transform parent = Selection.activeGameObject.transform; if (parent != null) { if (parent.GetComponent<Renderer>()) { GameObject parent02 = new GameObject(parent.name+"-Parent"); parent02.transform.position = parent.position; parent02.transform.SetParent(parent.parent); parent.SetParent(parent02.transform); Selection.activeGameObject = parent02; parent = parent02.transform; } Vector3 Position = parent.position; Vector3 rotate = parent.localEulerAngles; Vector3 Scale = parent.localScale; parent.position = Vector3.zero; parent.localScale = Vector3.one; foreach (Transform trans in parent) { trans.transform.RotateAround(Vector3.zero, Vector3.up, rotate.y); } parent.localEulerAngles = new Vector3(0, 0, 0); parent.transform.position = Position; parent.transform.localScale = Scale; } } //坐标朝向-x方向 void left() { Transform parent = Selection.activeGameObject.transform; if (parent.GetComponent<Renderer>() != null) { GameObject parent02 = new GameObject(parent.name + "-Parent"); parent02.transform.position = parent.position; parent02.transform.SetParent(parent.parent); parent.SetParent(parent02.transform); Selection.activeGameObject = parent02; parent = parent02.transform; } Vector3 Position = parent.position; Vector3 rotate = parent.localEulerAngles; Vector3 Scale = parent.localScale; parent.position = Vector3.zero; parent.localScale = Vector3.one; foreach (Transform trans in parent) { trans.transform.RotateAround(Vector3.zero, Vector3.up, rotate.y + 90); } parent.localEulerAngles = new Vector3(0, -90, 0); parent.transform.position = Position; parent.transform.localScale = Scale; } //坐标朝向-z方向 void back() { Transform parent = Selection.activeGameObject.transform; if (parent.GetComponent<Renderer>() != null) { GameObject parent02 = new GameObject(parent.name + "-Parent"); parent02.transform.position = parent.position; parent.SetParent(parent02.transform); Selection.activeGameObject = parent02; parent = parent02.transform; } Vector3 Position = parent.position; Vector3 rotate = parent.localEulerAngles; Vector3 Scale = parent.localScale; parent.position = Vector3.zero; parent.localScale = Vector3.one; foreach (Transform trans in parent) { trans.transform.RotateAround(Vector3.zero, Vector3.up, rotate.y + 180); } parent.localEulerAngles = new Vector3(0, -180, 0); parent.transform.position = Position; parent.transform.localScale = Scale; } //坐标朝向X方向 void right() { Transform parent = Selection.activeGameObject.transform; if (parent.GetComponent<Renderer>() != null) { GameObject parent02 = new GameObject(parent.name + "-Parent"); parent02.transform.position = parent.position; parent02.transform.SetParent(parent.parent); parent.SetParent(parent02.transform); Selection.activeGameObject = parent02; parent = parent02.transform; } Vector3 Position = parent.position; Vector3 rotate = parent.localEulerAngles; Vector3 Scale = parent.localScale; parent.position = Vector3.zero; parent.localScale = Vector3.one; foreach (Transform trans in parent) { trans.transform.RotateAround(Vector3.zero, Vector3.up, rotate.y + 270); } parent.localEulerAngles = new Vector3(0, -270, 0); parent.transform.position = Position; parent.transform.localScale = Scale; }}
操作视图如下:
阅读全文
0 0
- unity编辑器拓展二——一键调整scene中物件坐标朝向
- unity编辑器拓展三——一键调整scene中物体坐标中心点
- Unity编辑器拓展<二>
- Unity编辑器拓展<一>
- Unity 编辑器拓展 —— svn
- unity编辑器拓展八——unity里面分离模型
- unity编辑器拓展一——删除项目工程里的未使用Shader
- unity编辑器拓展五——将场景中物体以Obj格式导入max
- unity编辑器拓展九——删除场景中丢失的脚本
- unity编辑器拓展七——脚本创建模型
- unity编辑器拓展
- unity拓展编辑器
- Unity中制作trigger物件
- Unity如何实现在球表面移动并朝向一目标点 (二)
- unity编辑器拓展十——用代码实现unity菜单命令
- 【unity编辑器拓展】使用脚本添加Prefab到场景中
- Unity学习笔记(一)——基本概念之场景(Scene)
- 在unity中不销毁物件
- 服务器无法连接ssh_exchange_identification: read: Connection reset by peer
- 淘宝村峰会 | 马云希望互联网把亩产做到一千美金 这个淘宝村已经实现啦!
- Swift
- [KBEngine] 云服务器搭建KBEngine v1.1.0与远程连接调试 —— 详细流程记录
- Spring_day01
- unity编辑器拓展二——一键调整scene中物件坐标朝向
- ubuntu 16.04 GPU1080Ti 安装caffe
- Linux驱动学习之:WDT(watchdog)驱动
- JAVA中的流程控制
- Intellij IDEA Debug调试技巧
- 测试开发-Git常用命令总结
- 微服务架构设计 互联网金融平台微服务架构设计
- Android在一个TextView里显示不同样式的字体
- react的componentWillReceiveProps(nextProps) {} 生命周期