Unity中通过DoTween实现转盘效果

摘要:
通过转盘的数量,来计算需要旋转到的角度我这里是有12个旋转的位置,故360/12=30,所以,以30度为一个单位,进行偏移计算..实现方式如下:DrawRotateScr脚本:/***********************************************Title:大转盘的实现**Description:通过转盘的数量,来计算需要旋转到的角度.我这里是有12个旋转的位置,故360

通过转盘的数量,来计算需要旋转到的角度我这里是有12个旋转的位置,故360/12=30,所以,以30度为一个单位,进行偏移计算..

实现方式如下:

DrawRotateScr脚本:

/*********************************************
 *
 *   Title: 大转盘的实现
 *
 *   Description: 通过转盘的数量,来计算需要旋转到的角度.我这里是有 12个 旋转的位置, 故 360/12 = 30
 *                所以,以30度为一个单位,进行偏移计算
 *
 *   Author: 自律
 *
 *   Date: 2019.4.1
 *
 *   Modify: 
 * 
 *********************************************/
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
usingUnityEngine.UI;
usingDG.Tweening;

public classDrawRotateScr : MonoBehaviour
{
    /// <summary>
    ///Arrow图片
    /// </summary>
Image arrowImg;
    /// <summary>
    ///开始按钮
    /// </summary>
Button drawBtn;
    /// <summary>
    ///Light父物体
    /// </summary>
Transform lightCircle;
    /// <summary>
    ///停留的坐标激活组件 从而达到渐隐渐现的效果
    /// </summary>
Transform lightIndex;
    /// <summary>
    ///是否开始旋转
    /// </summary>
    bool isStart = false;
    /// <summary>
    ///箭头欧拉角
    /// </summary>
    floatangle;
    /// <summary>
    ///旋转角度 30° - 330°
    /// </summary>
    floatrotateAngle;
    /// <summary>
    ///最后的偏移的角度
    /// </summary>
    floatlastAngle;

    voidStart()
    {
        //获取组件
        arrowImg = transform.Find("Arrow").GetComponent<Image>();
        drawBtn = transform.Find("DrawBtn").GetComponent<Button>();
        lightCircle = transform.Find("LightCircle");
        drawBtn.onClick.RemoveAllListeners();
        //绑定事件
drawBtn.onClick.AddListener(OnTest);
    }

    voidOnTest()
    {
        float curRotateAngle =0f;
        curRotateAngle = SetRotateAngle(150);
        StartRotate(curRotateAngle);
    }


    voidUpdate()
    {
        if (!isStart)
        {
            return;
        }
        //设置欧拉角
        angle =arrowImg.transform.localEulerAngles.z;
        //设置旋转角
        rotateAngle = (int)((angle + 15) / 30) * 30;
        //激活动画组件
        lightIndex = lightCircle.Find("Light" +rotateAngle);
        //如果到达了该目标
        if(lightIndex)
        {
            //设置激活
            lightIndex.gameObject.SetActive(true);
            //如果没有获取到这个组件,就添加
            if (!lightIndex.GetComponent<LightAnimate>())
            {
                lightIndex.gameObject.AddComponent<LightAnimate>();
            }
            //开启动画
            lightIndex.GetComponent<LightAnimate>().StartAnimate();
        }
    }
    /// <summary>
    ///开始旋转
    /// </summary>
    /// <param name="_rotateAngle">传入的旋转角度.</param>
    void StartRotate(float_rotateAngle)
    {
        if(isStart)
        {
            return;
        }
        isStart = true;
        float rotateTemp =_rotateAngle;
        //获取 设置偏移的 特效 点亮
        //持续时间
        float duration = 4.0f;
        //重复的旋转次数
        int repeatCount = 4;
        //DoTween 旋转 顺时针旋转
        Tween tweenRotate = arrowImg.transform.DORotate(new Vector3(0, 0, -(rotateTemp + 360 *repeatCount)), duration, RotateMode.FastBeyond360);
        //重置时间
        Invoke("RotateComplete", 6f);
    }

    /// <summary>
    ///旋转完成重置
    /// </summary>
    voidRotateComplete()
    {
        isStart = false;
        Tween tweenReset = arrowImg.transform.DORotate(new Vector3(0, 0, 0), .4f, RotateMode.FastBeyond360);
    }
    /// <summary>
    ///设置偏转角所要停留的位置
    /// </summary>
    /// <returns>The rotate angle.</returns>
    /// <param name="value">Value.</param>
    float SetRotateAngle(intvalue)
    {
        float rotateAngle =.0f;
        switch(value)
        {
            case 0:
                {
                    rotateAngle = 0.0f;
                }
                break;
            case 330:
                {
                    rotateAngle = 330.0f;
                }
                break;
            case 300:
                {
                    rotateAngle = 300.0f;
                }
                break;
            case 270:
                {
                    rotateAngle = 270.0f;
                }
                break;
            case 240:
                {
                    rotateAngle = 240.0f;
                }
                break;
            case 210:
                {
                    rotateAngle = 210.0f;
                }
                break;
            case 180:
                {
                    rotateAngle = 180.0f;
                }
                break;
            case 150:
                {
                    rotateAngle = 150.0f;
                }
                break;
            case 120:
                {
                    rotateAngle = 120.0f;
                }
                break;
            case 90:
                {
                    rotateAngle = 90.0f;
                }
                break;
            case 60:
                {
                    rotateAngle = 60.0f;
                }
                break;
            case 30:
                {
                    rotateAngle = 30.0f;
                }
                break;
            default:
                break;
        }
        returnrotateAngle;
    }
}

LightAnimate脚本:

/*********************************************
 *
 *   Title: Light特效
 *
 *   Description: 动态绑定,当开始旋转的时候,做一个渐隐渐现的效果
 *
 *   Author: 自律
 *
 *   Date: 2019.4.1
 *
 *   Modify: 
 * 
 *********************************************/
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine;
usingUnityEngine.UI;

public classLightAnimate : MonoBehaviour
{
    bool isStare = false;
    Image lightImage;

    voidAwake ()
    {
        lightImage = GetComponent<Image>();
    }

    /// <summary>
    ///开始动画
    /// </summary>
    public voidStartAnimate ()
    {
        //初始值
        lightImage.color = new Color (lightImage.color.r, lightImage.color.g, lightImage.color.b, 1.0f);
    }

    voidUpdate ()
    {
        if (lightImage.color.a <= 0.0f)
            return;
        float alpha = lightImage.color.a - 0.05f;
        //渐隐效果
        if (alpha <= 0)
            alpha = 0;
        lightImage.color = newColor (lightImage.color.r, lightImage.color.g, lightImage.color.b, alpha);
    }
}

UI上:Unity中通过DoTween实现转盘效果第1张

这里需要注意的是Arrow中的Pivot,正常来说,Pivot是通过中心来旋转,这里我们要将它调整为以底部基准来进行旋转..将Unity设置为Pivot模式即可调整..

Unity中通过DoTween实现转盘效果第2张

点击运行即可

Git链接 :https://github.com/KinJin-Ristina/Circle-Rotate/tree/master

免责声明:文章转载自《Unity中通过DoTween实现转盘效果》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇批量重命名文件关于通用计算下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【Unity】经验汇总

移动平台使用顶点动画或UV动画的问题 5.4.2 在开发中,游戏打包到移动平台常出现的两种问题: 1.顶点动画:卡顿,动画变得生硬。 2.UV动画:贴图马赛克,模糊。 在实际问题解决中发现,导致这种问题的一般都是精度问题,移动平台开发针对大量性能不一的GPU。各种无法预料的问题。 很多效果在PC上的模拟器正常,打包到安卓还有ios就会出现各种奇怪的问题。...

External Tools

Preferences偏好设置-External Tools External Tools:     External Script Editor:外部脚本编辑器,通过此项可以切换您所擅用的脚本的编辑器     Editor Script Editor Args:     MonoDevelop Solution Properties:unity...

UE4/Unity绘制地图基础元素-面和体

前言 绘制地图基础元素-线(上篇) 绘制地图基础元素-线(下篇) 搞定地图画线之后,接下来就是绘制面和体了: 面作为地图渲染的基本元素之一,在地图中可以代表各种形式的区域,例如海面、绿地等。面数据通常以离散点串形式存储,因此渲染时最关注的是如何将其展现为闭合的图形。 体可以理解为带有高度的面,在地图中代表各种建筑,通常是由其顶部面数据和高度数据处理得到。...

unity中实现静态的3D对象对其他对象的跟随

using UnityEngine; public class FollowPosition : MonoBehaviour { public Transform targetTrans; public Transform lookAtTrans; public Vector3 offsetPos; //与本跟随者...

这篇说的是Unity Input 输入控制器

关于Unity3D是什么。我就不多做解释了。由于工作原因,该系列原创教程不定期更新。每月必然有更新。谢谢各位 Unity Input---输入控制管理器: Edit->Project Setting->input 如上图,就是Unity为游戏软体受命于玩家控制的操作转化中心。大家看到了默认是17个。 要查看和编辑这些设置,可以点击名称旁边的小箭...

教你高速高效接入SDK——Unity统一接入渠道SDK(Android篇)

U8SDK的设计之初,就是为了可以支持各种游戏引擎开发的游戏,而不不过Android的原生平台。眼下一大半的手游,都是採用Unity3D和Cocos2dx开发,那么这里,我们就先来一步步给大家演示,用Unity开发的游戏,怎样通过U8SDK来高速地完毕多家渠道SDK的接入。 Unity研发的手游,仅仅须要调用U8SDK抽象层就可以完毕多家渠道SDK的接...