Unity对象旋转功能
来源:互联网 发布:华润儿童漆怎么样知乎 编辑:程序博客网 时间:2024/06/08 16:30
今天上的是一个我们会经常使用的功能---物体旋转(包括UI对象和游戏对象)。使用方法很简单,将脚本挂在目标对象上,在Inspector面板上添加和选中具体参数即可,如下图所示:
下面上功能代码;还有一份编辑器代码。
using UnityEngine;/// <summary>/// 直接挂在需要旋转的对象上/// 提供旋转的功能/// </summary>public class RotateFunction : MonoBehaviour{ /// <summary> /// 旋转模式 /// </summary> public RotateMode MyRotateMode; /// <summary> /// 旋转轴 /// </summary> public RotateAxle MyRotate_Axle; /// <summary> /// 绕某目标旋转时的目标对象 /// </summary> public GameObject RotateTargetObj; Transform mytransform; void Start() { mytransform = this.transform; } //旋转的速度 public float RotateSpeed = 360f; void FixedUpdate() { //自转 RotateAroundSelf(); //绕某点转 RotateAroundTarget(); } /// <summary> /// 自传 /// </summary> void RotateAroundSelf() { if (MyRotateMode == RotateMode.Rotate_Self) { switch (MyRotate_Axle) { case RotateAxle.X: mytransform.Rotate(new Vector3(1f, 0, 0) * RotateSpeed * Time.deltaTime, Space.Self); break; case RotateAxle.Y: mytransform.Rotate(new Vector3(0, 1f, 0) * RotateSpeed * Time.deltaTime, Space.Self); break; case RotateAxle.Z: mytransform.Rotate(new Vector3(0, 0, 1f) * RotateSpeed * Time.deltaTime, Space.Self); break; } } } /// <summary> /// 绕目标旋转 /// </summary> void RotateAroundTarget() { if (MyRotateMode == RotateMode.Rotate_Target && RotateTargetObj != null) { switch (MyRotate_Axle) { case RotateAxle.X: mytransform.RotateAround(RotateTargetObj.transform.position, Vector3.right, RotateSpeed * Time.deltaTime); break; case RotateAxle.Y: mytransform.RotateAround(RotateTargetObj.transform.position, Vector3.up, RotateSpeed * Time.deltaTime); break; case RotateAxle.Z: mytransform.RotateAround(RotateTargetObj.transform.position, Vector3.forward, RotateSpeed * Time.deltaTime); break; } } }}
下面上功能编辑器脚本
using UnityEngine;#if UNITY_EDITORusing UnityEditor;[CustomEditor(typeof(RotateFunction))]public class RotateFunctionEditor : Editor{ public RotateFunction m_RotateFunction = null; public void OnEnable() { m_RotateFunction = (RotateFunction)target; } public override void OnInspectorGUI() { bool isChanged = false; //绘制MyRotateMode RotateMode oldMyRotateMode = m_RotateFunction.MyRotateMode; m_RotateFunction.MyRotateMode = (RotateMode)EditorGUILayout.EnumPopup("MyRotateMode", m_RotateFunction.MyRotateMode); if (oldMyRotateMode != m_RotateFunction.MyRotateMode) isChanged = true; //根据选择的RotateMode来确定RotateTarget if (m_RotateFunction.MyRotateMode == RotateMode.Rotate_Target) { GameObject oidObj = m_RotateFunction.RotateTargetObj; m_RotateFunction.RotateTargetObj = (GameObject)EditorGUILayout.ObjectField("RotateTargetObj", m_RotateFunction.RotateTargetObj, typeof(GameObject), true); if (oidObj != m_RotateFunction.RotateTargetObj) isChanged = true; } //绘制MyRotate_Axle RotateAxle oldMyRotate_Axle = m_RotateFunction.MyRotate_Axle; m_RotateFunction.MyRotate_Axle = (RotateAxle)EditorGUILayout.EnumPopup("MyRotate_Axle", m_RotateFunction.MyRotate_Axle); if (oldMyRotate_Axle != m_RotateFunction.MyRotate_Axle) isChanged = true; //绘制Rotate Speed float oldRotateSpeed = m_RotateFunction.RotateSpeed; m_RotateFunction.RotateSpeed = EditorGUILayout.FloatField("Rotate Speed", m_RotateFunction.RotateSpeed); if (oldRotateSpeed != m_RotateFunction.RotateSpeed) isChanged = true; if (isChanged) EditorUtility.SetDirty(m_RotateFunction); }}#endif
其实功能脚本倒没啥技术含量,值得初学者看看的是第二份是功能的编辑器脚本!
阅读全文
1 0
- Unity对象旋转功能
- unity 控制对象移动、旋转
- Unity对象的简单平移与旋转
- 【Unity快速实现小功能】实现物体绕轴旋转
- Unity 3D:控制相机旋转、移动、缩放等功能
- [Unity]旋转
- Unity之2d对象跟随鼠标旋转
- Unity对象up轴指向目的方向进行旋转
- 在条码打印软件中怎么使用旋转对象功能
- Unity 屏幕旋转
- unity 旋转函数
- Unity物体全方位旋转
- Unity的旋转问题
- unity旋转相关
- unity 刚体的旋转
- unity旋转 缩放摄像机
- Unity的旋转
- Unity 旋转角度计算
- Day6:磁盘配额与高级文件系统管理
- 常量池
- oracle导出导入数据和创建用户以及授权命令
- RBTree(红黑树)
- 从键盘输入n名学生数据,转存到磁盘文件,然后回显
- Unity对象旋转功能
- Mybatis--入门
- 《视觉SLAM十四讲》书籍图片资源
- 好用的天气插件
- Installation failed with message Failed to establish session.
- js点击当前元素增加样式
- 深入理解AJAX系列第四篇--跨域问题
- nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/util/DefaultIndenter
- Python re模块