下面展示一些 内联代码片
。
// An highlighted block
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using zSpace.Core;
//动态设置脚本执行顺序DefaultExecutionOrder
[DefaultExecutionOrder(ZCameraRig.ScriptPriority - 2)]
public class StylusDragable : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler,IPointerDownHandler,IPointerUpHandler
{
private Vector3 _initialGrabOffset = Vector3.zero;
private Quaternion _initialGrabRotation = Quaternion.identity;
private bool _isKinematic = false;
private enum Mode { None, Drag, Rotate }
private enum Source { None, Stylus, Touch, Mouse }
private Source m_souce = Source.None;
private Mode m_mode = Mode.None;
private float m_distance;
private float m_rotateSpeed = 0.2f;
private Camera m_eventCamera = null;
private int m_currentPointerId = -1000;
public event Action StartDragEvent;
public event Action EndDragEvent;
public Action<Collider> OnTriggerEnterListening;
public Action<Collider> OnTriggerExitListening;
private void OnTriggerEnter(Collider other)
{
OnTriggerEnterListening?.Invoke(other);
}
private void OnTriggerExit(Collider other)
{
OnTriggerExitListening?.Invoke(other);
}
public void OnBeginDrag(PointerEventData eventData)
{
if (m_currentPointerId > -10) return;
if (eventData.pointerId < 0)
MouseOnBeginDrag(eventData);
else
TouchOnBeginDrag(eventData);
StartDragEvent?.Invoke();
if (m_mode != Mode.None && m_currentPointerId > -10) RigidbodyFreeze();
}
public void OnDrag(PointerEventData eventData)
{
if (eventData.pointerId != m_currentPointerId) return;
if (m_souce == Source.Touch)
TouchOnDrag(eventData);
else if (m_souce == Source.Mouse)
MouseOnDrag(eventData);
StartDragEvent?.Invoke();
}
public void OnEndDrag(PointerEventData eventData)
{
if (eventData.pointerId != m_currentPointerId) return;
if (m_mode != Mode.None) RigidbodyRestore();
if (m_souce == Source.Touch)
TouchOnEndDrag(eventData);
else if (m_souce == Source.Mouse)
MouseOnEndDrag(eventData);
EndDragEvent?.Invoke();
m_mode = Mode.None;
m_souce = Source.None;
m_eventCamera = null;
m_currentPointerId = -1000;
}
#region Touch
public void TouchOnBeginDrag(PointerEventData eventData)
{
m_souce = Source.Touch;
m_mode = Mode.None;
if (eventData.pointerId != 0) return;
m_currentPointerId = eventData.pointerId;
if (Input.touchCount == 1)
{
m_mode = Mode.Drag;
RaycastResult rayRes = eventData.pointerCurrentRaycast;
if (rayRes.module == null || rayRes.module.eventCamera == null) return;
this.m_distance = (Camera.main.transform.position - rayRes.worldPosition).magnitude;
this.m_eventCamera = Camera.main;
this._initialGrabOffset = Quaternion.Inverse(this.transform.rotation) * (this.transform.position - rayRes.worldPosition);
this._initialGrabRotation = Quaternion.Inverse(Quaternion.Euler(rayRes.worldNormal)) * this.transform.rotation;
}
else
{
m_mode = Mode.Rotate;
this._initialGrabRotation = this.transform.rotation;
this._initialGrabOffset = eventData.position;
}
}
public void TouchOnDrag(PointerEventData eventData)
{
if (eventData.pointerId != 0) return;
if (m_mode == Mode.Drag)
{
if (this.m_eventCamera == null) return;
Ray ray = this.m_eventCamera.ScreenPointToRay(new Vector3(eventData.position.x, eventData.position.y, m_distance));
this.transform.position = (this.m_eventCamera.transform.position + ray.direction * m_distance) + (this.transform.rotation * this._initialGrabOffset);
}
else if (m_mode == Mode.Rotate)
{
Vector2 delta = eventData.position - new Vector2(_initialGrabOffset.x, _initialGrabOffset.y);
Quaternion q1 = Quaternion.AngleAxis(delta.x * m_rotateSpeed, Vector3.down);
Quaternion q2 = Quaternion.AngleAxis(delta.y * m_rotateSpeed, Vector3.right);
this.transform.rotation = q2 * q1 * this._initialGrabRotation;
}
}
public void TouchOnEndDrag(PointerEventData eventData)
{
}
#endregion
#region Mouse
public void MouseOnBeginDrag(PointerEventData eventData)
{
m_souce = Source.Mouse;
m_mode = Mode.None;
m_currentPointerId = eventData.pointerId;
if (eventData.button == PointerEventData.InputButton.Left)
{
m_mode = Mode.Drag;
RaycastResult rayRes = eventData.pointerCurrentRaycast;
//if (rayRes.module == null || rayRes.module.eventCamera == null) return;
this.m_distance = (Camera.main.transform.position - rayRes.worldPosition).magnitude;
this.m_eventCamera = Camera.main;
this._initialGrabOffset = Quaternion.Inverse(this.transform.rotation) * (this.transform.position - rayRes.worldPosition);
this._initialGrabRotation = Quaternion.Inverse(Quaternion.Euler(rayRes.worldNormal)) * this.transform.rotation;
}
else if (eventData.button == PointerEventData.InputButton.Right)
{
m_mode = Mode.Rotate;
this._initialGrabRotation = this.transform.rotation;
this._initialGrabOffset = eventData.position;
}
}
public void MouseOnDrag(PointerEventData eventData)
{
if (m_mode == Mode.Drag)
{
if (this.m_eventCamera == null) return;
Ray ray = this.m_eventCamera.ScreenPointToRay(new Vector3(eventData.position.x, eventData.position.y, m_distance));
this.transform.position = (this.m_eventCamera.transform.position + ray.direction * m_distance) + (this.transform.rotation * this._initialGrabOffset);
}
else if (m_mode == Mode.Rotate)
{
Vector2 delta = eventData.position - new Vector2(_initialGrabOffset.x, _initialGrabOffset.y);
Quaternion q1 = Quaternion.AngleAxis(delta.x * m_rotateSpeed, Vector3.down);
Quaternion q2 = Quaternion.AngleAxis(delta.y * m_rotateSpeed, Vector3.right);
this.transform.rotation = q2 * q1 * this._initialGrabRotation;
}
}
public void MouseOnEndDrag(PointerEventData eventData)
{
}
#endregion
private void RigidbodyFreeze()
{
var rigidbody = this.GetComponent<Rigidbody>();
if (rigidbody != null)
{
this._isKinematic = rigidbody.isKinematic;
rigidbody.isKinematic = true;
}
}
private void RigidbodyRestore()
{
var rigidbody = this.GetComponent<Rigidbody>();
if (rigidbody != null)
{
rigidbody.isKinematic = this._isKinematic;
}
}
private void OnDisable()
{
EndDragEvent?.Invoke();
}
public void OnPointerDown(PointerEventData eventData)
{
StartDragEvent?.Invoke();
}
public void OnPointerUp(PointerEventData eventData)
{
EndDragEvent?.Invoke();
}