UGUI:ウィンドウとウィンドウスケーリング上限

著作権:

  • 次のWebサイトで始まるこの記事オリジナル:
  1. ブログパーク「優れた夢のメイカーズムーブメント」空間にします。https://www.cnblogs.com/raymondking123
  2. 優秀な夢メイカーズムーブメント公式ブログます。https://91make.top
  3. 優れたゲーム夢のメーカー文化講演します。https://91make.ke.qq.com
  4. マイクロチャンネル公衆数の「優秀な夢メイカーズムーブメント」:umaketop
  • あなたは転載は自由ですが、完全な著作権表示を含める必要があります

ウィンドウをドラッグして、特定の領域に制限さ

public class Scene : MonoBehaviour, IDragHandler, IPointerDownHandler
{
    public RectTransform RtTransform;
    public RectTransform RTParent;
    Vector2 downpos;
    Vector2 newpos;
    public void OnPointerDown(PointerEventData eventData)
    {
       RectTransformUtility.ScreenPointToLocalPointInRectangle
       (RtTransform, eventData.position
       ,eventData.pressEventCamera, out downpos);
        //print(downpos);
    }
    
    public void OnDrag(PointerEventData eventData)
    {
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle
         (RtTransform, eventData.position
         , eventData.pressEventCamera, out newpos))
        {
           
            Vector2 offset = newpos - downpos;
            transform.parent.position = (Vector2)transform.parent.position + offset;
            downpos = newpos;
        }

        if (RTParent.position.x < 0)
        {
            Vector2 tmp = RTParent.position;
            tmp.x = 0;
            RTParent.position = tmp;
        }
        else if (RTParent.position.x > RtTransform.rect.width - RTParent.rect.width)
        {
            Vector2 tmp = RTParent.position;
            tmp.x = RtTransform.rect.width - RTParent.rect.width;
            RTParent.position = tmp;
        }

        if (RTParent.position.y < 0)
        {
            Vector2 tmp = RTParent.position;
            tmp.y = 0;
            RTParent.position = tmp;
        }
        else if (RTParent.position.y > RtTransform.rect.height - RTParent.rect.height)
        {
            Vector2 tmp = RTParent.position;
            tmp.y = RtTransform.rect.height - RTParent.rect.height;
            RTParent.position = tmp;
        }

    }

ウィンドウスケーリング

public class Zoom : MonoBehaviour,IPointerDownHandler,IDragHandler   
{
    public RectTransform canvasWindow;
    public RectTransform parentWinfow;
    Vector2 downpos;
    Vector2 newpos;
    Vector2 origsize;

    public void OnPointerDown(PointerEventData eventData)
    {
        origsize = parentWinfow.sizeDelta;
        RectTransformUtility.ScreenPointToLocalPointInRectangle
        (canvasWindow, eventData.position, eventData.pressEventCamera, out downpos);
    }

    public void OnDrag(PointerEventData eventData)
    {

        if (RectTransformUtility.ScreenPointToLocalPointInRectangle
           (canvasWindow, eventData.position, eventData.pressEventCamera, out newpos))
        {
            Vector2 offpos=newpos-downpos;
           // offpos.y *= -1;
            parentWinfow.sizeDelta =origsize + offpos;        
        }
    }
}

おすすめ

転載: www.cnblogs.com/raymondking123/p/11554875.html