鼠标进入 Button 缓慢上升的效果

2020-10-10  本文已影响0人  烂醉花间dlitf

效果展示

效果展示

目录结构

Hierarchy 面板
Button Inspector
BackImage Inspector
UiImage Inspector

素材

LauncherUI_1.png
LauncherUI_2.png

代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace ZhangQrTools
{
    public enum DirectionType8 : byte
    {
        Top,
        TopRight,
        Right,
        BottomRight,
        Botton,
        BottonLeft,
        Left,
        TopLeft
    };
    public enum DirectionType4 : byte
    {
        Top,
        Right,
        Botton,
        Left
    };

    static class TwoDimensionHelper
    {
        public static Vector2 GetDirection(DirectionType8 type8)
        {
            Vector2 ret;
            switch (type8)
            {
                case DirectionType8.Top:
                    ret = new Vector2(0, 1);
                    break;
                case DirectionType8.Botton:
                    ret = new Vector2(0, -1);
                    break;
                case DirectionType8.Right:
                    ret = new Vector2(1, 0);
                    break;
                case DirectionType8.Left:
                    ret = new Vector2(-1, 0);
                    break;
                case DirectionType8.BottomRight:
                    ret = new Vector2(1, -1);
                    break;
                case DirectionType8.BottonLeft:
                    ret = new Vector2(-1, -1);
                    break;
                case DirectionType8.TopLeft:
                    ret = new Vector2(-1, 1);
                    break;
                case DirectionType8.TopRight:
                    ret = new Vector2(1, 1);
                    break;
                default:
                    ret = Vector2.zero;
                    break;
            }
            return ret;
        }

        public static Vector2 GetDirection(DirectionType4 type4)
        {
            Vector2 ret;
            switch (type4)
            {
                case DirectionType4.Top:
                    ret = new Vector2(0, 1);
                    break;
                case DirectionType4.Botton:
                    ret = new Vector2(0, -1);
                    break;
                case DirectionType4.Right:
                    ret = new Vector2(1, 0);
                    break;
                case DirectionType4.Left:
                    ret = new Vector2(-1, 0);
                    break;
                default:
                    ret = Vector2.zero;
                    break;
            }
            return ret;
        }
    }
}

public class LauncherButton :Selectable, ISubmitHandler, IEventSystemHandler,IPointerClickHandler
{
    public float MaxOffset; // 最大位移量

    public float Speed; // Speed 不能大于等于 MaxOffset
    
    [SerializeField]
    private Sprite HeightedImage;

    [SerializeField]
    private Image UIImage;

    public ZhangQrTools.DirectionType4 MoveDirection;

    private Sprite originImage;
    private Vector3 originPosition;
    private bool isEnter = false;

    protected override void OnEnable()
    {
        base.OnEnable();
        originImage = UIImage.sprite;
        originPosition = this.transform.localPosition;
    }

    private void OnNormal()
    {
        UIImage.sprite = originImage;
        isEnter = false;
    }

    private void OnHighLight()
    {
        if (HeightedImage != null)
        {
            UIImage.sprite = HeightedImage;
        }
        isEnter = true;
    }

    private void FixedUpdate()
    {
        if (isEnter)
        {
            Vector2 dic_vector = ZhangQrTools.TwoDimensionHelper.GetDirection(MoveDirection);
            Vector3 target_position = originPosition + new Vector3(dic_vector.x, dic_vector.y, 0) * MaxOffset;
            if (Vector3.Distance(transform.localPosition, target_position) > 0.1f)
            {
                transform.localPosition += new Vector3(dic_vector.x, dic_vector.y, 0) * Speed;
            }
        }
        else
        {
            Vector2 dic_vector = -ZhangQrTools.TwoDimensionHelper.GetDirection(MoveDirection);
            Vector3 target_position = originPosition;
            if (Vector3.Distance(transform.localPosition, target_position) > 0.1f)
            {
                transform.localPosition += new Vector3(dic_vector.x, dic_vector.y, 0) * Speed;
            }
        }
    }


    public override void OnPointerEnter(PointerEventData eventData)
    {
        base.OnPointerEnter(eventData);
        OnHighLight();
    }

    public override void OnPointerExit(PointerEventData eventData)
    {
        base.OnPointerExit(eventData);
        OnNormal();
    }

    public void OnSubmit(BaseEventData eventData)
    {
        
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("Clicked");
        Renderer r = GameObject.Find("Cube").GetComponent<Renderer>();
        if (r.material.color != Color.red)
        {
            r.material.color = Color.red;
            return;
        }
        if (r.material.color != Color.blue)
        {
            r.material.color = Color.blue;
            return;
        }
    }
}

遗留问题

这样写的话速度不能大于或者等于 MaxOffset,因为瞬间会超过目的地,从而越移动越远。

上一篇下一篇

猜你喜欢

热点阅读