• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
~Joke_crazy
爱生活,爱拉芳!
   首页    新随笔       管理     

小型自动朝向转盘

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

posted @ 2018-06-28 12:17  ~Joke_crazy  阅读(320)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3