找回密码
 立即注册
查看: 192|回复: 0

Unity3D--武器可视化窗口(支持移动和电脑设备)

[复制链接]
发表于 2023-1-28 13:33 | 显示全部楼层 |阅读模式
Unity3D--武器可视化窗口




Unity3D引擎运行下的效果图

<hr/>演示

以下是本篇文章正文内容,下面案例可供参考
一、准备工作

以下是本篇文章正文内容,下面案例可供参考

  • 新建场景
  • 在Hierarchy面板下新建一个Cube用作背景(可自行选择其他)
  • 新建一个GameObject用作放置要展示的物品,命名Container
  • 新建两个Button,用作切换物品,



实例

<hr/>二、点击事件(切换物品,显示隐藏Canvas)

1.新创建一个ItemMenu脚本

  • 把脚本挂在Container上,把其子物体拖到Items里,
/****************************************************
    文件:ItemMenu.cs
        作者:HKZ
    邮箱: 3046916186@qq.com
    日期:2021/11/15 15:35:27
        功能:点击事件(切换物品,显示隐藏Canvas)
*****************************************************/

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<Button>();
            btnNext.onClick.AddListener(OnNextItemClick);

            btnPrevious = GameObject.Find("btnPrevious").GetComponent<Button>();
            btnPrevious.onClick.AddListener(OnPreviousItemClick);

            Items[0].SetActive(true);        //为了方便这里就不查找了,可自行查找
        }
        private void OnNextItemClick()
        {
            Items[currentIndex].SetActive(false);
            currentIndex++;
            if (currentIndex >= Items.Length)
            {
                currentIndex = 0;
            }
            Items[currentIndex].SetActive(true);
        }
        private void OnPreviousItemClick()
        {
            Items[currentIndex].SetActive(false);
            currentIndex--;
            if (currentIndex < 0)
            {
                currentIndex = Items.Length - 1;
            }
            Items[currentIndex].SetActive(true);
        }
        private void Update()
        {
            if ((Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
                || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began
                && !EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)))
            {
                Canvas.SetActive(false);
            }
            else if (!Input.GetMouseButton(0))
            {
                Canvas.SetActive(true);
            }
        }
    }
}



效果图

<hr/>三、浏览物品(点击拖拽)

1.新创建一个ItemView脚本

  • 把ItemView脚本挂在Container下的每个物品身上
/****************************************************
    文件:ItemView.cs
        作者:HKZ
    邮箱: 3046916186@qq.com
    日期:2021/11/15 16:8:6
        功能:浏览物品(支持电脑和移动设备)
*****************************************************/

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);
        }
    }
}


效果图

<hr/>提示

素材来自Unity商店–免费资源

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-6-16 22:17 , Processed in 0.114399 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表