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

3D轮转图

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


# 前言

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


一、效果图

Unity3D--用2D图片实现3D轮转图_第1张图片

二、实现步骤–准备工作

1.先创建Unity工程

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

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

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

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

Unity3D--用2D图片实现3D轮转图_第4张图片

4.如图

Unity3D--用2D图片实现3D轮转图_第5张图片

三、代码步骤–核心

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<float> moveAction;

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

        private RectTransform rect;
        private RectTransform Rect
        {
     
            get
            {
     
                if (rect == null)
                {
     
                    rect = GetComponent<RectTransform>();
                }
                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<float> 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<RotationDiagramItem> itemList;
        private List<ItemPosDate> posDateList;

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

        private void Start()
        {
     
            ///btns = transform.GetComponentInChildren

四、Hierarchy面板讲解

Unity3D--用2D图片实现3D轮转图_第6张图片


总结

Unity工程中人物图片来源网络,侵权必删
在这里插入图片描述

你可能感兴趣的:(笔记,2D图片实现3D轮转图,unity3d,c#)