92 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			92 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
using System.Collections;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Linq;
 | 
						|
using static MyFrameworkPure.CollectionsExtension;
 | 
						|
using UnityEngine;
 | 
						|
using UnityEngine.EventSystems;
 | 
						|
using MyFrameworkPure;
 | 
						|
using CommonScript;
 | 
						|
 | 
						|
public class MouseDragable : MonoBehaviour
 | 
						|
{
 | 
						|
    [SerializeField] private MouseButton mouseButton = MouseButton.Left;
 | 
						|
 | 
						|
    [SerializeField] private Collider[] attachColliders;//约束区域碰撞体
 | 
						|
 | 
						|
    [SerializeField] private bool limitToCollider;//是否将拖拽物体限制到碰撞体表面
 | 
						|
 | 
						|
    private float depth;
 | 
						|
    private Vector3 lastRayCastPos;
 | 
						|
    public bool only_z=false;
 | 
						|
    // Start is called before the first frame update
 | 
						|
    void Start()
 | 
						|
    {
 | 
						|
        EventTriggerListener.Get(gameObject).onBeginDrag_Data += OnBeginDrag;
 | 
						|
 | 
						|
        EventTriggerListener.Get(gameObject).onDrag_Data += OnDrag;
 | 
						|
    }
 | 
						|
 | 
						|
    void OnBeginDrag(GameObject go,PointerEventData e)
 | 
						|
    {
 | 
						|
        if(!enabled)
 | 
						|
            return;
 | 
						|
        if(e.button != (PointerEventData.InputButton)mouseButton)
 | 
						|
            return;
 | 
						|
        if(!e.enterEventCamera)
 | 
						|
            return;
 | 
						|
        depth = e.enterEventCamera.WorldToScreenPoint(transform.position).z;
 | 
						|
    }
 | 
						|
 | 
						|
    void OnDrag(GameObject go, PointerEventData e)
 | 
						|
    {
 | 
						|
        if(!enabled)
 | 
						|
            return;
 | 
						|
        if (e.button != (PointerEventData.InputButton)mouseButton)
 | 
						|
            return;
 | 
						|
 | 
						|
        Vector3 mousePos = new Vector3(e.position.x,e.position.y,depth);
 | 
						|
        //transform.position = e.pressEventCamera.ScreenToWorldPoint(mousePos);
 | 
						|
        if (only_z == true)
 | 
						|
        {
 | 
						|
            transform.position = new Vector3(transform.position.x, e.pressEventCamera.ScreenToWorldPoint(mousePos).y, e.pressEventCamera.ScreenToWorldPoint(mousePos).z);
 | 
						|
        }
 | 
						|
        else {
 | 
						|
 | 
						|
            transform.position = e.pressEventCamera.ScreenToWorldPoint(mousePos);
 | 
						|
        }
 | 
						|
 | 
						|
        if (!attachColliders.IsNullOrEmpty())
 | 
						|
        {
 | 
						|
            Ray ray = e.pressEventCamera.ScreenPointToRay(e.position);
 | 
						|
 | 
						|
            RaycastHit[] hits = Physics.RaycastAll(ray);
 | 
						|
            RaycastHit hit = hits.FirstOrDefault(x => attachColliders.Contains(x.collider));
 | 
						|
            if (hit.collider != null)
 | 
						|
            {
 | 
						|
               // transform.position = hit.point;
 | 
						|
                //lastRayCastPos = hit.point;
 | 
						|
                if (only_z == true)
 | 
						|
                {
 | 
						|
                    lastRayCastPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
 | 
						|
 | 
						|
                }
 | 
						|
                else {
 | 
						|
 | 
						|
                    transform.position = hit.point;
 | 
						|
                    lastRayCastPos = hit.point;
 | 
						|
 | 
						|
                }
 | 
						|
 | 
						|
 | 
						|
                }
 | 
						|
            else if (limitToCollider)
 | 
						|
            {
 | 
						|
                
 | 
						|
                    transform.position = lastRayCastPos;
 | 
						|
              
 | 
						|
            }
 | 
						|
          
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |