
提示:素材来源网络,侵权必删
Unity3D--武器可视化窗口在UI界面控制3D物体,进行无死角浏览
提示:以下是本篇文章正文内容,下面案例可供参考
一、准备工作示例:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace HKZ
{
public class ItemMenu : MonoBehaviour
{
private GameObject Canvas; //当点击物品想要浏览时,隐藏UI界面
private Button btnNext; //点击浏览下一个
private Button btnPrevious; //点击浏览上一个
private int currentIndex = 0; //当前物品下标
public GameObject[] Items; //要展示的物品集合
private void Awake()
{
Canvas = GameObject.Find("Canvas");
btnNext = GameObject.Find("btnNext").GetComponent
2.效果图
示例:
三、浏览物品(点击拖拽) 1.新创建一个ItemView脚本
using UnityEngine;
namespace HKZ
{
public class ItemView : MonoBehaviour
{
private Vector3 baseScale = new Vector3(0.7f, 0.7f, 0.7f);
private bool isPhone = false, isFirst = true;
private float zRot = 0, yRot = 0;
private Vector3 startPos;
private void OnEnable()
{
transform.localScale = baseScale;
if (SystemInfo.deviceType == DeviceType.Handheld)//移动设备
{
isPhone = true;
}
isFirst = true;
}
private void Update()
{
if (isPhone)
{
if (Input.touchCount > 0)
{
if (isFirst)
{
isFirst = false;
startPos = Input.GetTouch(0).position;
return;
}
else
{
zRot += (Input.GetTouch(0).position.y - startPos.y) * 0.09f;
zRot = Mathf.Clamp(zRot, -55, 55);
yRot += (Input.GetTouch(0).position.x - startPos.x) * 0.25f;
startPos = Input.GetTouch(0).position;
}
ScaleUp();
}
else
{
isFirst = true;
zRot = Mathf.Lerp(zRot, 0, 0.08f);
yRot = Mathf.Lerp(yRot, 0, 0.08f);
ScaleDown();
}
transform.eulerAngles = new Vector3(0, 90 + yRot, zRot);
}
else
{
if (Input.GetMouseButton(0))
{
if (isFirst)
{
isFirst = false;
startPos = Input.mousePosition;
return;
}
else
{
zRot += (Input.mousePosition.y - startPos.y) * 0.1f;
zRot = Mathf.Clamp(zRot, -55, 55);
yRot += (Input.mousePosition.x - startPos.x) * 0.3f;
//yRot = Mathf.Clamp(yRot, -55, 55);
startPos = Input.mousePosition;
}
ScaleUp();
}
else
{
isFirst = true;
zRot = Mathf.Lerp(zRot, 0, 0.2f);
yRot = Mathf.Lerp(yRot, 0, 0.2f);
ScaleDown();
}
transform.eulerAngles = new Vector3(0, 90 + yRot, zRot);
}
}
private void ScaleUp()
{
transform.localScale = Vector3.Lerp(transform.localScale, Vector3.one, 0.075f);
}
private void ScaleDown()
{
transform.localScale = Vector3.Lerp(transform.localScale, baseScale, 0.075f);
}
}
}
2.效果图
示例: