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;
        }
    }


}

你的产品有必要投广告么?亚马逊站内广告策略常见问题解答

对于亚马逊站内广告,投放与否,其所带来的订单结果几乎是立竿见影的,一条表现不太差的Listing。只要投放了站内广告,其订单数量必然会有所增长。

Unity 内存管理和profiler详解

Unity Memory Management Unity 的 Memory 构造 实际上Unity游戏使用的内存一共有三种:程序代码、托管堆(Managed Heap)以及本机堆(Native Heap)。 程序代码包括了所有的Unity引擎,使用的库,以及你所写的所有的游戏代码。在编译后,得到的运行文件将会被加载到设备中执行,并占用一定内存。 这部分内存实际上是没有办法去“管理”的,它们...

Unity在Android设备中的icon适配

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

unity圆圈自动吸附屏幕边缘

效果: 设置如下:

Unity 导出 iOS 游戏并上架 App Store

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