目标:

在UI层上 自由观察3D 模型,实现鼠标控制模型的旋转,缩放&& 触摸屏手势控制模型的旋转缩放控制;

这里仅将控制代码挂载到相机上,通过对相机的控制来观察模型。

基本上为最小单元了,1个控制脚本即可完成展示目标。

效果展示:

分别可以用鼠标和触摸屏来控制

脚本代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// 此代码挂载到相机上,
/// </summary>
public class SmoothUi3DCamera : MonoBehaviour
{public Transform pivot;public Vector3 pivotOffset = Vector3.zero;public Transform target;public float distance = 10.0f;public float minDistance = 5f;public float maxDistance = 15f;public float zoomSpeed = 1f;public float xSpeed = 250.0f;public float ySpeed = 250.0f;[Header("触摸旋转速度因子")]public float touchSpeed = 0.05f; // 测试显示0.05f 效果更佳public bool allowYTilt = true;public float yMinLimit = -90f;public float yMaxLimit = 90f;private float x = 0.0f;private float y = 0.0f;private float targetX = 0f;private float targetY = 0f;public float targetDistance = 0f;private float xVelocity = 1f;private float yVelocity = 1f;private float zoomVelocity = 1f;private Touch oldTouch1;  //上次触摸点1(手指1)private Touch oldTouch2;  //上次触摸点2(手指2)private void Start(){var angles = transform.eulerAngles;// 因为是操作的相机,所以我们实际的操作出来是向反的方向targetY = x = angles.x;targetX = y = ClampAngle(angles.y, yMinLimit, yMaxLimit);targetDistance = distance;}private void LateUpdate(){if (!pivot) return;#region 鼠标控制// 鼠标中键控制缩放var scroll = Input.GetAxis("Mouse ScrollWheel");if (scroll > 0.0f) targetDistance -= zoomSpeed;else if (scroll < 0.0f)targetDistance += zoomSpeed;targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);// 鼠标左右键操作旋转if (Input.GetMouseButton(0) || (Input.GetMouseButton(1) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)))){targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;if (allowYTilt){targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;targetY = ClampAngle(targetY, yMinLimit, yMaxLimit);}}x = Mathf.SmoothDampAngle(x, targetX, ref xVelocity, 0.3f);y = allowYTilt ? Mathf.SmoothDampAngle(y, targetY, ref yVelocity, 0.3f) : targetY;Quaternion rotation = Quaternion.Euler(y, x, 0);//Quaternion rotation = Quaternion.Euler(x, y, 0);distance = Mathf.SmoothDamp(distance, targetDistance, ref zoomVelocity, 0.5f);Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + pivot.position + pivotOffset;transform.rotation = rotation;transform.position = position;#endregion#region 触摸控制//没有触摸,就是触摸点为0// if (Input.touchCount <= 0)// {// return;// }//单点触摸, 水平上下旋转if (1 == Input.touchCount){Touch touch = Input.GetTouch(0);Vector2 deltaPos = touch.deltaPosition;targetX += deltaPos.x * xSpeed * 0.02f * touchSpeed;if (allowYTilt){targetY -= deltaPos.y * ySpeed * 0.02f * touchSpeed;targetY = ClampAngle(targetY, yMinLimit, yMaxLimit);}}if (2 == Input.touchCount){//多点触摸, 放大缩小Touch newTouch1 = Input.GetTouch(0);Touch newTouch2 = Input.GetTouch(1);//第2点刚开始接触屏幕, 只记录,不做处理if (newTouch2.phase == TouchPhase.Began){oldTouch2 = newTouch2;oldTouch1 = newTouch1;return;}//计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);//两个距离之差,为正表示放大手势, 为负表示缩小手势float offset = newDistance - oldDistance;if (offset > 0.0f) targetDistance -= zoomSpeed;else if (offset < 0.0f)targetDistance += zoomSpeed;targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);//记住最新的触摸点,下次使用oldTouch1 = newTouch1;oldTouch2 = newTouch2;}#endregion}private static float ClampAngle(float angle, float min, float max){if (angle < -360) angle += 360;if (angle > 360) angle -= 360;return Mathf.Clamp(angle, min, max);}
}

完整的演示程序包:

unity3D模型自由观察,旋转缩放-C#文档类资源-CSDN下载在UI上自由观察3D模型,鼠标控制模型的旋转,缩放&&触摸屏手势控制模型的旋转更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/u013509878/85648262文件为3d模型自由观察旋转缩放.unitypackage,直接导入项目即可查看

希望可以帮助大家。

unity 3D模型展示旋转缩放相关推荐

  1. Unity 3D模型展示框架篇之项目整理

    本项目将整合之前Unity程序基础小框架专栏在Unity 3D模型展示项目基础上进行整合,并记录了集成过程中对原脚本的调整过程.增加了Asset Bundle+ILRuntime热更新技术流程. 1. ...

  2. Unity 3D模型展示框架篇之自由观察(Cinemachine)

    本项目将整合之前Unity程序基础小框架专栏在Unity 3D模型展示项目基础上进行整合,并记录了集成过程中对原脚本的调整过程.增加了Asset Bundle+ILRuntime热更新技术流程. 在U ...

  3. Unity 3D模型展示之webGL平台展现

    在之前的项目基础上我们已经打包后在PC端进行展示了.这篇文章主要介绍在切换到webGL上时效果展示不出来需要进行调整,特此记录一下. 1.平台切换 选择WebGL平台切换,没有的可以进行安装,安装之后 ...

  4. Unity 3D模型展示框架篇之框架运用

    本项目将整合之前Unity程序基础小框架专栏在Unity 3D模型展示项目基础上进行整合,并记录了集成过程中对原脚本的调整过程.增加了Asset Bundle+ILRuntime热更新技术流程. 效果 ...

  5. Unity 3D模型展示框架篇之ILRuntime快速入门

    系列文章目录 Unity 3D模型展示框架篇之项目整理 Unity 3D模型展示框架篇之框架运用 Unity 3D模型展示框架篇之自由观察(Cinemachine) Unity 3D模型展示框架篇之资 ...

  6. Unity 3D模型展示之UI布局

    整体布局情况 导入UI资源并统一将Texture Type设置为Sprite(2D and UI) 1.标题 将Text中的文本设置为'Unity 3D物体展示实例'. 添加Shadow与Outlin ...

  7. Unity 3D模型展示之自由观察模型

    效果展示 进行调整将灯光放到Main Camera下,旋转的时候光就一直打在这面. 1.观察模型脚本 public class Smooth3DCamera : MonoBehaviour {publ ...

  8. Unity 3D模型展示之模型透明效果

    效果展示 1.透明效果实现思路 简单点就是将模型的材质替换成透明材质.需要注意:1.指定范围内的模型进行透明化.2.一个模型会有多个材质,都需要进行透明化.3.还能将模型的材质恢复最初的效果.4.选中 ...

  9. Unity 3D模型展示之生成标注

    1.效果图 2.生成标注 标注生成要求: 1.指定元件生成标注. 2.生成标注出现位置可以设置. 根据标注生成的要求,可以在元件添加脚本时将这些信息读取并进行实现.控制标注生成在元件的位置,生成标注的 ...

最新文章

  1. 2048(lj模拟)
  2. swoole 清除定时器提示no timer
  3. for循环中let,var 的经典面试题:for循环中 console.log(i)详解
  4. 阿里云Freeswtich部署
  5. c语言 汇编中断程序,ARM7 C语言嵌入汇编开关中断
  6. java中KMP模式,Java数据结构-串及其应用-KMP模式匹配算法
  7. Mysql原理+ 多实例 +表损坏
  8. C# 创建文件夹 Directory
  9. amaze 绝对位置 html,amaze ui 的使用详细教程
  10. 青海省海西蒙古族藏族自治州谷歌高清卫星地图下载
  11. borders.GetItem()方法 获取斜线Border (VC在Excel中画斜线格)
  12. Android APK反编译哟~~~
  13. 【CXY】JAVA基础 之 List
  14. DirectX11 板条箱示例Demo
  15. ACM题目————列变位法解密
  16. 我国人口14亿人那为什么汉语编程没有办法成为主流?
  17. Codeup 小白鼠排队
  18. 【阅读论文】博-自动化眼底图像分析技术可筛查糖尿病患者的视网膜疾病--第三章--QA
  19. 2018山西中考计算机软件名称,山西省2018中考信息技术试题(含操作步骤)1-10题
  20. windows删除顽固文件

热门文章

  1. 计算机画图怎样更改文字,计算机画图——文字工具的使用
  2. EF Core上下文DbContext相关配置和生命周期
  3. linux下图形界面登陆器
  4. 宇树A1机器狗手势控制
  5. linux汇编格式化磁盘,如何进行硬盘格式化命令操作【详解】
  6. 【架构】REST架构风格简洁
  7. AjaxControlExtender.vsi安装
  8. Invalid bound statement (not fond):com.ebusiness.reviewer.mapper.UsersMapper.register 报错的解决方案
  9. 双系统开机后直接进入windows没有切换界面(Ubuntu + Win10)
  10. 北大工学院计算机考研,2021-2022北京大学工学院机械专硕考研复习经验贴