这是我参与11月更文挑战的第22天,活动详情查看:2021最后一次更文挑战
推荐阅读
大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。
一、前言
前段时间分享了UGUI的每个组件的属性和使giti轮胎用方法,比如Text、Button、开源代码网站githubImage、ToggleGitHub、InputField、Scrogit教程llView等等。
接着分享了UGUI的一些原理,开源阅读比如说UG开源代码网站githubUI的渲染模式、UGUI的缩放计github官网算、UGUI的描点定位、UGUI的自动布局等等。
相信大家看完后会对UGUI有一个比较全面的认识了。
下面,就继续开源节流是什么意思分享UGUI的UI组件进行应用的3d模型可以导入su吗实例。
二、介绍及效果图
童鞋给我带来了一个问题,能不能在UI上显示3D模型,并且还可以交互,而且还要弹窗口。
那就分享一下如何弹窗显示模型吧giticomfort是什么轮胎。
这里主要是使用UGUI的RawImage显示纹理组件来映射摄像机的信息到UI上,然后交互还是跟模型交互3d模型怎么转成su模型,但是在UI上就好像是控制UI一样。
效果图:

源代码: download.csdn.net/download/q7…
三、实现
33d模型素材网站-1gitee、搭建场景
我们首先搭建一个UI场景:






3-23d模型素材网站、设置弹窗
接下来,就需要设置一个层级最高的Panel作为弹窗口,也就是将弹窗Panelgithub永久回家地址放到最后渲染:







3-3、编写UI响应代码
新建脚本BugiteettonManager.cs:
using UnityEngine; using UnityEngine.UI; public class ButtonManager : MonoBehaviour { public Button btn1; public Button btn2; public Button btn3; public Button btn4; public Button CloseBtn; public GameObject popupPanel;//弹窗 public GameObject[] Gun;//四个模型 // Start is called before the first frame update void Start() { btn1.onClick.AddListener(()=>BtnOnClickEvent(btn1.name)); btn2.onClick.AddListener(()=>BtnOnClickEvent(btn2.name)); btn3.onClick.AddListener(()=>BtnOnClickEvent(btn3.name)); btn4.onClick.AddListener(()=>BtnOnClickEvent(btn4.name)); CloseBtn.onClick.AddListener(ClosePanel); popupPanel.SetActive(false); } private void BtnOnClickEvent(string btnName) { popupPanel.SetActive(true); for (int i = 0; i < Gun.Length; i++) { Gun[i].SetActive(false); } switch (btnName) { case "gun1": Gun[0].SetActive(true); break; case "gun2": Gun[1].SetActive(true); break; case "gun3": Gun[2].SetActive(true); break; case "gun4": Gun[3].SetActive(true); break; default: break; } } private void ClosePanel() { popupPanel.SetActive(false); } }
将脚本拖到相机上,并将对应的对象拖到对应的卡槽中:


3-4、实现模型交互
接下来实现模型的拖动、放大、旋转等交互 新建脚本ModelManager.cs:
using UnityEngine; public class ModelManager : MonoBehaviour { private Vector2 tempXY; private Vector2 targetAngles; private Vector3 currentAngles; public GameObject Gun;//同一个父物体 public float rotateSpeed = 1; public float moveSpeed = 1; public Camera userCamera; void Update() { ModelControl(); } public void ModelControl() { if (Input.GetMouseButton(0)) { tempXY.x -= -Input.GetAxis("Mouse X") * moveSpeed; tempXY.y += Input.GetAxis("Mouse Y") * moveSpeed; Vector3 temp = new Vector3(tempXY.x, tempXY.y, 0); Gun.transform.position = temp; } else if (Input.GetMouseButton(1)) { targetAngles.y -= Input.GetAxis("Mouse X") * rotateSpeed; currentAngles = Vector3.Lerp(currentAngles, targetAngles, Time.deltaTime / 0.2f); Gun.transform.eulerAngles = currentAngles; } else if (Input.GetAxis("Mouse ScrollWheel") != 0) { //鼠标滚动滑轮 值就会变化 if (Input.GetAxis("Mouse ScrollWheel") < 0) { //范围值限定 if (userCamera.fieldOfView <= 100) userCamera.fieldOfView += 2; if (userCamera.orthographicSize <= 20) userCamera.orthographicSize += 0.5F; } //Zoom in if (Input.GetAxis("Mouse ScrollWheel") > 0) { //范围值限定 if (userCamera.fieldOfView > 16) userCamera.fieldOfView -= 2; if (userCamera.orthographicSize >= 1) userCamera.orthographicSize -= 0.5F; } } } }
将4个枪的父物体设置为同一个父物体:

将这个脚本附到MainCamera身上,将对应的参数拖进去:

效果如图:

四、后言
模型的移动、旋转,跟子级父级的位置旋转有很大的关联,子级的位置或者父级的位置旋转没有设置好。 会有各gitlab种各样的GitHub不对应的情况,比如说子物体没有按照自身的Git坐标旋转,绕父物体的坐标旋转,或者移动的时候模型会先自动移动到中心位置,然后移动。
这样情况下最好的解决方案就是: 1、将父物体和子物体的坐标都归零 。 2、子物体保存自3d模型软件己的旋转位置即可。
评论(0)