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 应用,具体流程可以参考本文末尾列出的教程。这

unity圆圈自动吸附屏幕边缘

效果: 设置如下:

Android开发:简述Android布局优化的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blo…g.csdn.net/qq_/article/details/ ndroid开…发:简述ndroid布局优化的方法作为android应用来讲,无论应用本身多么美观,

Unity和Cocos2D在2D游戏开发上的对比

游戏开发的最好技术是什么:nity还是ocos?在网上你可以找到很多这两种技术的对比…。在我们开发游戏之前,我们要了解相关数据和信息并决定使用那种技术。但是人们对这两种技…术的对比大多都比较主观。擅长ocos的人会偏向于ocos。而使用nity的人则偏向于

关于“如何学习游戏开发不知道从哪方面入手”的相关建议

前言:作为一名上海上市游戏公司的关卡和战斗策划,简单来聊一下我对学习游戏制作的方法吧…。一、明确我的工作内容游戏的制作由三个领域的专业人士共同完成。我们通常见到的游戏,比…如有哪些功能,什么样的画面好不好看,玩法是有哪些怎么玩,主要是由三个部分组成:游戏策