美文网首页
Vector3与Vector2的Picture拖拽

Vector3与Vector2的Picture拖拽

作者: 咆哮的小老虎 | 来源:发表于2019-08-14 17:05 被阅读0次

    https://www.cnblogs.com/jiangshuai52511/p/6649843.html

    Vector3的拖拽

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    
    public class ControlTheMouseMove : MonoBehaviour {
    
        private Vector3 _vec3TargetScreenSpace;// 目标物体的屏幕空间坐标  
        private Vector3 _vec3TargetWorldSpace;// 目标物体的世界空间坐标  
        private Transform _trans;// 目标物体的空间变换组件  
        private Vector3 _vec3MouseScreenSpace;// 鼠标的屏幕空间坐标  
        private Vector3 _vec3Offset;// 偏移 
        GameObject obj;
        float speed = 5f;
        bool isClick = false;
        private Transform m_Transform;
        // Use this for initialization
    
    
        void Awake() {
            _trans = transform;
        }
    
        void Start () {
            m_Transform = gameObject.GetComponent<Transform>();
            obj = GameObject.Find("Cube");
        } 
        
        // Update is called once per frame
        void Update () {
    
           MoveControl();
            if(Input.GetKey(KeyCode.Mouse1))
            {
                isClick = true;
            }
            if(isClick)
            {
                obj.transform.Translate(Vector3.forward * speed * Time.deltaTime);
            }
        }
    
        void MoveControl()
        {
            m_Transform.Rotate(Vector3.up, Input.GetAxis("Mouse X"));
            m_Transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y"));
          m_Transform.Rotate(Vector3.forward, Input.GetAxis("Mouse forward"));
          m_Transform.Rotate(Vector3.back, Input.GetAxis("Mouse back"));
           
        }
        IEnumerator OnMouseDown()
    
        {
    
            // 把目标物体的世界空间坐标转换到它自身的屏幕空间坐标   
    
            _vec3TargetScreenSpace = Camera.main.WorldToScreenPoint(_trans.position);
    
            // 存储鼠标的屏幕空间坐标(Z值使用目标物体的屏幕空间坐标)   
    
            _vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _vec3TargetScreenSpace.z);
    
            // 计算目标物体与鼠标物体在世界空间中的偏移量   
    
            _vec3Offset = _trans.position - Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace);
    
            // 鼠标左键按下   
    
            while (Input.GetMouseButton(0))
            {
    
                // 存储鼠标的屏幕空间坐标(Z值使用目标物体的屏幕空间坐标)  
    
                _vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _vec3TargetScreenSpace.z);
    
                // 把鼠标的屏幕空间坐标转换到世界空间坐标(Z值使用目标物体的屏幕空间坐标),加上偏移量,以此作为目标物体的世界空间坐标  
    
                _vec3TargetWorldSpace = Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace) + _vec3Offset;
    
                // 更新目标物体的世界空间坐标   
    
                _trans.position = _vec3TargetWorldSpace;
    
                // 等待固定更新   
    
                yield return new WaitForFixedUpdate();
            }
        }
    
    
    }
    

    Vector2的拖拽

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    
    public class ImageMove : MonoBehaviour, IBeginDragHandler, IDragHandler {
    
        //鼠标第一次点击的位置    
        Vector2 firstMousePos = Vector3.zero;
    
        //鼠标拖拽的位置    
        Vector2 secondMousePos = Vector3.zero;
        //偏移量    
        Vector3 offsetPos = Vector3.zero;
    
        public void OnBeginDrag(PointerEventData eventData)    {
    
            firstMousePos = Input.mousePosition;
            print(123);
        }
        public void OnDrag(PointerEventData eventData)    {
            secondMousePos = Input.mousePosition;
            //需要移动的 向量       
            offsetPos = secondMousePos - firstMousePos;
            float x = transform.localPosition.x;
            float y = transform.localPosition.y;
            //向量偏移        
            x = x + offsetPos.x; y = y + offsetPos.y;
            //x = Mathf.Clamp(x, 380, 1550);        
            //y = Mathf.Clamp(y, 210, 850);        
            transform.localPosition = new Vector3(x, y, transform.localPosition.z);
            firstMousePos = secondMousePos;
    
            print(firstMousePos);
            print(secondMousePos);
        }
    }
    

    相关文章

      网友评论

          本文标题:Vector3与Vector2的Picture拖拽

          本文链接:https://www.haomeiwen.com/subject/hblnjctx.html