unity圆圈自动吸附屏幕边缘

效果:


设置如下:


using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;

public class AutoAdsorptiontoEdge : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{

    private RectTransform popupTransform;

    private bool isPopupBeingDragged = false;
    private IEnumerator moveToPosCoroutine = null;

    private Vector2 halfSize;


    void Awake()
    {
        popupTransform = (RectTransform)transform;

    }
    void Start()
    {
        halfSize = popupTransform.sizeDelta * 0.5f * popupTransform.root.localScale.x;
        Init();
    }



    public void Init()
    {
        halfSize = popupTransform.sizeDelta * 0.5f * popupTransform.root.localScale.x;
        OnEndDrag(null);
    }

    public void OnPointerClick(PointerEventData data)
    {
        if (!isPopupBeingDragged)
        {

        }
    }


    public void OnBeginDrag(PointerEventData data)
    {
        isPopupBeingDragged = true;

        if (moveToPosCoroutine != null)
        {
            StopCoroutine(moveToPosCoroutine);
            moveToPosCoroutine = null;
        }
    }

    public void OnDrag(PointerEventData data)
    {
        popupTransform.position = data.position;
    }

    public void OnEndDrag(PointerEventData data)
    {
        int screenWidth = Screen.width;
        int screenHeight = Screen.height;

        Vector3 pos = popupTransform.position;

        float distToLeft = pos.x;
        float distToRight = Mathf.Abs(pos.x - screenWidth);

        float distToBottom = Mathf.Abs(pos.y);
        float distToTop = Mathf.Abs(pos.y - screenHeight);

        float horDistance = Mathf.Min(distToLeft, distToRight);
        float vertDistance = Mathf.Min(distToBottom, distToTop);

        if (horDistance < vertDistance)
        {
            if (distToLeft < distToRight)
                pos = new Vector3(halfSize.x, pos.y, 0f);
            else
                pos = new Vector3(screenWidth - halfSize.x, pos.y, 0f);

            pos.y = Mathf.Clamp(pos.y, halfSize.y, screenHeight - halfSize.y);
        }
        else
        {
            if (distToBottom < distToTop)
                pos = new Vector3(pos.x, halfSize.y, 0f);
            else
                pos = new Vector3(pos.x, screenHeight - halfSize.y, 0f);

            pos.x = Mathf.Clamp(pos.x, halfSize.x, screenWidth - halfSize.x);
        }

        if (moveToPosCoroutine != null)
            StopCoroutine(moveToPosCoroutine);

        moveToPosCoroutine = MoveToPosAnimation(pos);
        StartCoroutine(moveToPosCoroutine);

        isPopupBeingDragged = false;
    }

    private IEnumerator MoveToPosAnimation(Vector3 targetPos)
    {
        float modifier = 0f;
        Vector3 initialPos = popupTransform.position;

        while (modifier < 1f)
        {
            modifier += 4f * Time.unscaledDeltaTime;
            popupTransform.position = Vector3.Lerp(initialPos, targetPos, modifier);

            yield return null;
        }
    }


}

Unity 导出 iOS 游戏并上架 App Store

logread/1878Unity 导出 iOS 游戏并上架 App Store 0. 前言 因为之前已经上架过一款 App(Swift 语言),所以开发者账号、证书这些都已经搞定了,如果你是第一次上架 iOS 应用,具体流程可以参考本文末尾列出的教程。这

程序员专用的几个高质量 Chrome 插件

当然,就是从提高自己的效率开始,为自己减轻压力,节省更多时间去做别的事。今天我就给大家推荐几个,我个人感觉用的不错的,能够提高效率的,而且是有关程序员工作使用的Chrome浏览器插

unity圆圈自动吸附屏幕边缘

效果: 设置如下:

Unity在Android设备中的icon适配

应用icon图标适配 前言: 游戏上了 Google Play 之后,若没有做 Android 8.0 及更高版本的 icon 适配. 就会出现以下3个不适配的图标样式. 谷歌从 Android 8.0 后就设计了一套将 icon 分为前景和背景的方式拼接,这样还可以实现很好的 icon 效果。 详细科普请见以下链接: Understanding Android Adaptive Icons...

棋牌游戏运营需注意的几点策略

现在关于一个新的棋牌游戏来说,获取的用户许多可能都是通过运营方法从其他途径挖过来的,所以做好一个棋牌游戏的运营直接抉择这款游戏的生命长度。