zoukankan      html  css  js  c++  java
  • UGUI:窗口限制以及窗口缩放

    版权申明:

    • 本文原创首发于以下网站:
    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;        
            }
        }
    }
    
  • 相关阅读:
    Laya中使用Protobuf
    Laya中第三方库的使用
    Laya的骨骼换装
    Laya的粒子效果
    Laya的预设Prefab (预制件)
    Egret EUI Tab + ViewStack
    Egret 划线手势动画 (切水果)
    Egret3.2.6老项目转成5.2.22微信小游戏,遇到exml加载不了问题
    AndroidUI设计 之 图片浏览器
    Android应用的自动更新模块
  • 原文地址:https://www.cnblogs.com/raymondking123/p/11554875.html
Copyright © 2011-2022 走看看