当前位置 : 主页 > 手机开发 > harmonyos >

Unity空间中移动物体

来源:互联网 收集:自由互联 发布时间:2023-12-16
using UnityEngine; public class MovableContent : MonoBehaviour { [SerializeField] private float m_ClickHoldTime = 0.1f; private float m_timeHold = 0f; private bool m_EditingContent = false; private Transform m_CameraTransform; private float


using UnityEngine;


    public class MovableContent : MonoBehaviour
    {
        [SerializeField]
        private float m_ClickHoldTime = 0.1f;
        private float m_timeHold = 0f;

        private bool m_EditingContent = false;

        private Transform m_CameraTransform;
        private float m_MovePlaneDistance;

        private void Start()
        {
            m_CameraTransform = Camera.main.transform;
            StoreContent();
        }

        private void Update()
        {
            if (m_EditingContent)
            { 
                Vector3 projection = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, m_MovePlaneDistance));
                transform.position = projection;
            }
        }

        private void StoreContent()
        {
            //if (!ContentStorageManager.Instance.contentList.Contains(this))
            //{
            //    ContentStorageManager.Instance.contentList.Add(this);
            //}
            //ContentStorageManager.Instance.SaveContents();
        }

        public void RemoveContent()
        {
            //if (ContentStorageManager.Instance.contentList.Contains(this))
            //{
            //    ContentStorageManager.Instance.contentList.Remove(this);
            //}
            //ContentStorageManager.Instance.SaveContents();
            //Destroy(gameObject);
        }

        private void OnMouseDrag()
        {
            m_timeHold += Time.deltaTime;

            if (m_timeHold >= m_ClickHoldTime && !m_EditingContent)
            {
                m_MovePlaneDistance = Vector3.Dot(transform.position - m_CameraTransform.position, m_CameraTransform.forward) / m_CameraTransform.forward.sqrMagnitude;
                m_EditingContent = true;
            }
        }

        private void OnMouseUp()
        {
            StoreContent();
            m_timeHold = 0f;
            m_EditingContent = false;
        }
    }
上一篇:使用libevent实现回显服务器
下一篇:没有了
网友评论