小型自动朝向转盘

Posted joke-crazy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小型自动朝向转盘相关的知识,希望对你有一定的参考价值。

技术分享图片
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

以上是关于小型自动朝向转盘的主要内容,如果未能解决你的问题,请参考以下文章

Unity笔记将角色的碰撞体朝向鼠标点击方向——角色朝向鼠标

PHP jQuery微信大转盘抽奖源代码分享

微信小程序转盘怎么无限制

微信小程序项目实例——幸运大转盘

WeMall微信商城源码插件大转盘代码详情

微信大转盘 案例源码提供