码迷,mamicode.com
首页 > 其他好文 > 详细

小型自动朝向转盘

时间:2018-06-28 13:55:54      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:alt   transform   spl   部分   ++   cti   sha   inter   wak   

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

小型自动朝向转盘

标签:alt   transform   spl   部分   ++   cti   sha   inter   wak   

原文地址:https://www.cnblogs.com/Joke-crazy/p/9238088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!