• 小型自动朝向转盘


    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class CircleScroll : MonoBehaviour, IDragHandler, IEndDragHandler
    {
        public float speed = 0.1f;
        public List<CircleScrollItem> items;
    
        private bool limitAngle;
        private bool direction;
        private int heroMin;
        private int heroMax;
        private float angle_Min;
        private float _angle_Max;
    
        private float angle_Max
        {
            get { return _angle_Max; }
            set
            {
                _angle_Max = value;
                if (_angle_Max == 0)
                {
                    limitAngle = false;
                }
                else
                {
                    limitAngle = true;
                    heroMin = 1;
                    heroMax = Convert.ToInt32(_angle_Max / stepValue);
                }
            }
        }
    
        private Transform target;   //轮盘
        private int share = 12;     //总共多少块
        private float stepValue;    //块的大小
    
        private int currentPosition = 1;
        private Action<int> callBack;
    
        private void Awake()
        {
            if (target == null)
            {
                target = transform;
            }
            stepValue = 360 / share;
        }
    
        public void StartUp(int share, float angle, Action<int> callBack)
        {
            this.share = share;
            this.callBack = callBack;
            stepValue = 360 / share;
            angle_Max = angle;
        }
    
        public void OnDrag(PointerEventData eventData)
        {
            direction = eventData.delta.y >= 0;
            if (AngleLimit())
            {
                RotateTarget(eventData.delta.y);
                for (int i = 0; i < items.Count; i++)
                {
                    items[i].OnDrag(-Vector3.forward * eventData.delta.y * speed);
                }
            }
        }
    
        public void OnEndDrag(PointerEventData eventData)
        {
            float stepAngle = 0;
            float currentAngle = FormatAngle(target.transform.localRotation.eulerAngles.z);
    
            for (int i = 0; i < share; i++)
            {
                stepAngle += stepValue;
                if (stepAngle > currentAngle)
                {
                    currentPosition = i + 1;
                    break;
                }
            }
    
            if (currentPosition > heroMax)
            {
                if (direction)
                    currentPosition = heroMax;
                else
                    currentPosition = heroMin;
            }
    
            if (callBack != null)
            {
                callBack(currentPosition);
            }
        }
    
        private void RotateTarget(float touchSpeed)
        {
            target.Rotate(Vector3.forward * touchSpeed * speed);
        }
    
        private float FormatAngle(float angle)
        {
            if (angle >= 360)
            {
                angle = angle % 360;
            }
            else if (angle <= 0)
            {
                angle = 360 + angle;
            }
            else
            {
                angle = Math.Abs(angle);
            }
            return angle;
        }
    
        private float FormatAngle_Half(float angle)
        {
            if (angle >= 180)
            {
                angle -= 360;
            }
            return angle;
        }
    
        private bool AngleLimit()
        {
            if (!limitAngle)
            {
                return true;
            }
            else
            {
                float angle = FormatAngle_Half(target.transform.localRotation.eulerAngles.z);
                if (angle <= angle_Min && !direction
                    || angle >= angle_Max && direction)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
    }
    View Code
    using UnityEngine;
    
    public class CircleScrollItem : MonoBehaviour
    {
        public void OnDrag(Vector3 angle)
        {
            transform.Rotate(angle);
        }
    }
    View Code

    目前有两部分呢组成,一个是展示圆盘,另一个是展示物品Item

  • 相关阅读:
    20160405小结
    [HTML表格]在databases显示行的附加信息
    [django]django+datatable简单运用于表格中
    [django]django xlrd处理xls中日期转换问题
    烦!
    【数据库】Navicat Premium12远程连接MySQL数据库
    关于定义变量名为"name"的坑!!!
    【前端】前端面试题整理
    Firebug控制台详解
    什么是跨域?跨域解决方法
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/9238088.html
Copyright © 2020-2023  润新知