zoukankan      html  css  js  c++  java
  • NGUI 触摸拖动,并限制拖动区域

    using UnityEngine;
    using System.Collections;
    
    public class UDragManager : MonoBehaviour 
    {
        public Transform target;
        
        private Vector3 offset;
        private Bounds bounds;
    
        public static float ImageWidth;
        public static float ImageHeight;
        public static float ScreenWidth; 
        public static float ScreenHeight;
        public static float m_nOffect;
    
        void Start()
        {
            InitConfig ();
        }
    
        void InitConfig()
        {
            ImageWidth         = 2490;
            ImageHeight     = 1536;
            ScreenWidth     = 2048; 
            ScreenHeight    = 1536;
    
    //        ImageWidth         = this.transform.localScale.x;
    //        ImageHeight     = this.transform.localScale.y;
    //        ScreenWidth     = Screen.width; 
    //        ScreenHeight    = Screen.height;
    
            m_nOffect         = ImageHeight - ScreenHeight;
    
        }
        
        void OnPress(bool pressed)
        {
            if (target == null) return;
            if (pressed) 
            {
                bounds = NGUIMath.CalculateRelativeWidgetBounds(target.transform);
                Vector3 position = UICamera.currentCamera.WorldToScreenPoint(target.position);
                offset = new Vector3(Input.mousePosition.x - (position.x - bounds.size.x / 2), Input.mousePosition.y - (position.y - bounds.size.y / 2),0f);
            }
        }
        
        void OnDrag(Vector2 delta)
        {
            Vector3 currentPoint = new Vector3 (Input.mousePosition.x - offset.x, Input.mousePosition.y - offset.y, 0f);
                        
            currentPoint.x += bounds.size.x / 2;
    
            currentPoint.y += bounds.size.x / 2;
            currentPoint.z = 2;
        
            target.position = UICamera.currentCamera.ScreenToWorldPoint (currentPoint);
        }
    
        void Update()
        {
            if(this.transform.localPosition.x > (-ScreenWidth))
            {
                this.transform.localPosition = new Vector3(-ScreenWidth,this.transform.localPosition.y,this.transform.localPosition.z);
            }
            if (this.transform.localPosition.x < (-ImageWidth)) 
            {
                this.transform.localPosition = new Vector3(-ImageWidth,this.transform.localPosition.y,this.transform.localPosition.z);
            }
            if(this.transform.localPosition.y > 0)
            {
                this.transform.localPosition = new Vector3(this.transform.localPosition.x,0,this.transform.localPosition.z);
            }
            if (this.transform.localPosition.y < (-m_nOffect)) 
            {
                this.transform.localPosition = new Vector3(this.transform.localPosition.x,-m_nOffect,this.transform.localPosition.z);
            }
        }
    }
  • 相关阅读:
    普通人如何做到30分钟读一本书并做完笔记?
    谈谈MySQL死锁之二 死锁检测和处理源码分析
    谈谈MySQL死锁 一
    八种架构设计模式及其优缺点概述(中)
    八种架构设计模式及其优缺点概述(上)
    轻量级开源小程序SDK发车啦
    Magicodes.IE编写多框架版本支持和执行单元测试
    Magicodes.Sms短信库的封装和集成
    Magicodes.IE之导入学生数据教程
    如何基于k8s快速搭建TeamCity(YAML分享)
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4613386.html
Copyright © 2011-2022 走看看