栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

Unity3D--用2D图片实现3D轮转图

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity3D--用2D图片实现3D轮转图

3D轮转图
  • 一、效果图
  • 二、实现步骤--准备工作
    • 1.先创建Unity工程
    • 2.在Hierarchy下右键-->UI-->Canvas
    • 3.在Canvas下创建空物体对象
    • 4.如图
  • 三、代码步骤--核心
    • 1.先创建RotationDiagramItem脚本
    • 2.其次创建RotationDiagram2D脚本
  • 四、Hierarchy面板讲解
  • 总结


# 前言

用2D图片实现3D轮转图,


一、效果图

二、实现步骤–准备工作 1.先创建Unity工程

2.在Hierarchy下右键–>UI–>Canvas

3.在Canvas下创建空物体对象

4.如图

三、代码步骤–核心 1.先创建RotationDiagramItem脚本

代码如下(示例):

using DG.Tweening;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace HKZ
{
    public class RotationDiagramItem : MonoBehaviour, IDragHandler, IEndDragHandler
    {
        public int PosId;
        private float offset;
        private float aniTime = 0.5f;

        private Action moveAction;

        private Image image;
        private Image Image
        {
            get
            {
                if (image == null)
                {
                    image = GetComponent();
                }
                return image;
            }
        }

        private RectTransform rect;
        private RectTransform Rect
        {
            get
            {
                if (rect == null)
                {
                    rect = GetComponent();
                }
                return rect;
            }
        }

        private void Change(int index)
        {
            Debug.Log(index);
        }

        public void SetParent(Transform parent)
        {
            transform.SetParent(parent);
        }

        public void SetSprite(Sprite sprite)
        {
            Image.sprite = sprite;
        }

        public void SetPosDate(ItemPosDate date)
        {
            Rect.DOAnchorPos(Vector2.right * date.X, aniTime);
            Rect.DOScale(Vector3.one * date.ScaleTimes, aniTime);
            //Rect.anchoredPosition = Vector2.right*date.X;
            //Rect.localScale = Vector3.one * date.ScaleTimes;
            StartCoroutine(Wait(date));
        }

        private IEnumerator Wait(ItemPosDate date)
        {
            yield return new WaitForSeconds(aniTime * 0.5f);
            transform.SetSiblingIndex(date.Order);
        }

        public void OnDrag(PointerEventData eventData)
        {
            offset += eventData.delta.x;
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            moveAction(offset);
            offset = 0;
        }

        public void AddMoveListener(Action onMove)
        {
            moveAction = onMove;
        }

        public void ChangeId(int symbol, int totalItemNum)
        {
            int id = PosId;
            id += symbol;
            if (id < 0)
            {
                id += totalItemNum;
            }
            PosId = id % totalItemNum;
        }
    }
}

2.其次创建RotationDiagram2D脚本

把此脚本挂在空物体身上

代码如下(示例):

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

namespace HKZ
{
    public class RotationDiagram2D : MonoBehaviour
    {
        public Vector2 ItemSize;
        public Sprite[] ItemSprites;
        public float offset;
        public float ScaleTimesMin;
        public float ScaleTimesMax;

        private List itemList;
        private List posDateList;

        private void Awake()
        {
            itemList = new List();
            posDateList = new List();
            CreateItem();
            CalulateDate();
            SetItemDate();
        }

        private void Start()
        {
            ///btns = transform.GetComponentInChildren
四、Hierarchy面板讲解


总结

Unity工程中人物图片来源网络,侵权必删

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900286.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号