.vs
Assets
Library
APIUpdater
Artifacts
Bee
BuildPlayerData
PackageCache
com.unity.collab-proxy@2.5.2
com.unity.editorcoroutines@1.0.0
com.unity.ext.nunit@1.0.6
com.unity.feature.development@1.0.1
com.unity.ide.rider@3.0.31
com.unity.ide.visualstudio@2.0.22
com.unity.ide.vscode@1.2.5
com.unity.modules.ai@1.0.0
com.unity.modules.androidjni@1.0.0
com.unity.modules.animation@1.0.0
com.unity.modules.assetbundle@1.0.0
com.unity.modules.audio@1.0.0
com.unity.modules.cloth@1.0.0
com.unity.modules.director@1.0.0
com.unity.modules.imageconversion@1.0.0
com.unity.modules.imgui@1.0.0
com.unity.modules.jsonserialize@1.0.0
com.unity.modules.particlesystem@1.0.0
com.unity.modules.physics2d@1.0.0
com.unity.modules.physics@1.0.0
com.unity.modules.screencapture@1.0.0
com.unity.modules.subsystems@1.0.0
com.unity.modules.terrain@1.0.0
com.unity.modules.terrainphysics@1.0.0
com.unity.modules.tilemap@1.0.0
com.unity.modules.ui@1.0.0
com.unity.modules.uielements@1.0.0
com.unity.modules.umbra@1.0.0
com.unity.modules.unityanalytics@1.0.0
com.unity.modules.unitywebrequest@1.0.0
com.unity.modules.unitywebrequestassetbundle@1.0.0
com.unity.modules.unitywebrequestaudio@1.0.0
com.unity.modules.unitywebrequesttexture@1.0.0
com.unity.modules.unitywebrequestwww@1.0.0
com.unity.modules.vehicles@1.0.0
com.unity.modules.video@1.0.0
com.unity.modules.vr@1.0.0
com.unity.modules.wind@1.0.0
com.unity.modules.xr@1.0.0
com.unity.performance.profile-analyzer@1.2.2
com.unity.settings-manager@2.0.1
com.unity.test-framework@1.1.33
com.unity.testtools.codecoverage@1.2.6
com.unity.textmeshpro@3.0.6
com.unity.timeline@1.7.6
com.unity.ugui@1.0.0
Documentation~
Editor
Runtime
EventSystem
EventData
InputModules
Raycasters
BaseRaycaster.cs
BaseRaycaster.cs.meta
Physics2DRaycaster.cs
Physics2DRaycaster.cs.meta
PhysicsRaycaster.cs
PhysicsRaycaster.cs.meta
UIElements
EventData.meta
EventHandle.cs
EventHandle.cs.meta
EventInterfaces.cs
EventInterfaces.cs.meta
EventSystem.cs
EventSystem.cs.meta
EventTrigger.cs
EventTrigger.cs.meta
EventTriggerType.cs
EventTriggerType.cs.meta
ExecuteEvents.cs
ExecuteEvents.cs.meta
InputModules.meta
MoveDirection.cs
MoveDirection.cs.meta
RaycastResult.cs
RaycastResult.cs.meta
RaycasterManager.cs
RaycasterManager.cs.meta
Raycasters.meta
UIBehaviour.cs
UIBehaviour.cs.meta
UIElements.meta
Properties
UI
EventSystem.meta
Properties.meta
UI.meta
UnityEngine.UI.asmdef
UnityEngine.UI.asmdef.meta
Tests
CHANGELOG.md
CHANGELOG.md.meta
Editor.meta
LICENSE.md
LICENSE.md.meta
README.md
README.md.meta
Runtime.meta
Tests.meta
package.json
package.json.meta
com.unity.visualscripting@1.9.4
PackageManager
PlayModeViewStates
PlayerDataCache
ScriptAssemblies
Search
ShaderCache
SplashScreenCache
StateCache
UIElements
AnnotationManager
ArtifactDB
ArtifactDB-lock
BuildPlayer.prefs
BuildSettings.asset
EditorOnlyScriptingSettings.json
EditorOnlyVirtualTextureState.json
EditorSnapSettings.asset
EditorUserBuildSettings.asset
InspectorExpandedItems.asset
LastBuild.buildreport
LastSceneManagerSetup.txt
LibraryFormatVersion.txt
MonoManager.asset
SceneVisibilityState.asset
ScriptMapper
ShaderCache.db
SourceAssetDB
SourceAssetDB-lock
SpriteAtlasDatabase.asset
Style.catalog
expandedItems
ilpp.pid
Logs
Packages
ProjectSettings
UserSettings
obj
.vsconfig
Assembly-CSharp.Player.csproj
Assembly-CSharp.csproj
TM1.sln
Unity.CollabProxy.Editor.csproj
Unity.EditorCoroutines.Editor.csproj
Unity.Performance.Profile-Analyzer.Editor.csproj
Unity.PlasticSCM.Editor.csproj
Unity.Rider.Editor.csproj
Unity.Settings.Editor.csproj
Unity.TestTools.CodeCoverage.Editor.OpenCover.Model.csproj
Unity.TestTools.CodeCoverage.Editor.OpenCover.Mono.Reflection.csproj
Unity.TestTools.CodeCoverage.Editor.csproj
Unity.TextMeshPro.Editor.csproj
Unity.TextMeshPro.Player.csproj
Unity.TextMeshPro.csproj
Unity.Timeline.Editor.csproj
Unity.Timeline.Player.csproj
Unity.Timeline.csproj
Unity.VSCode.Editor.csproj
Unity.VisualScripting.Core.Editor.csproj
Unity.VisualScripting.Core.Player.csproj
Unity.VisualScripting.Core.csproj
Unity.VisualScripting.Flow.Editor.csproj
Unity.VisualScripting.Flow.Player.csproj
Unity.VisualScripting.Flow.csproj
Unity.VisualScripting.SettingsProvider.Editor.csproj
Unity.VisualScripting.Shared.Editor.csproj
Unity.VisualScripting.State.Editor.csproj
Unity.VisualScripting.State.Player.csproj
Unity.VisualScripting.State.csproj
Unity.VisualStudio.Editor.csproj
UnityEditor.TestRunner.csproj
UnityEditor.UI.csproj
UnityEngine.TestRunner.Player.csproj
UnityEngine.TestRunner.csproj
UnityEngine.UI.Player.csproj
UnityEngine.UI.csproj
208 lines
7.7 KiB
C#
208 lines
7.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine.UI;
|
|
|
|
namespace UnityEngine.EventSystems
|
|
{
|
|
/// <summary>
|
|
/// Simple event system using physics raycasts.
|
|
/// </summary>
|
|
[AddComponentMenu("Event/Physics Raycaster")]
|
|
[RequireComponent(typeof(Camera))]
|
|
/// <summary>
|
|
/// Raycaster for casting against 3D Physics components.
|
|
/// </summary>
|
|
public class PhysicsRaycaster : BaseRaycaster
|
|
{
|
|
/// <summary>
|
|
/// Const to use for clarity when no event mask is set
|
|
/// </summary>
|
|
protected const int kNoEventMaskSet = -1;
|
|
|
|
protected Camera m_EventCamera;
|
|
|
|
/// <summary>
|
|
/// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
|
|
/// </summary>
|
|
[SerializeField]
|
|
protected LayerMask m_EventMask = kNoEventMaskSet;
|
|
|
|
/// <summary>
|
|
/// The max number of intersections allowed. 0 = allocating version anything else is non alloc.
|
|
/// </summary>
|
|
[SerializeField]
|
|
protected int m_MaxRayIntersections = 0;
|
|
protected int m_LastMaxRayIntersections = 0;
|
|
|
|
#if PACKAGE_PHYSICS
|
|
RaycastHit[] m_Hits;
|
|
#endif
|
|
|
|
protected PhysicsRaycaster()
|
|
{}
|
|
|
|
public override Camera eventCamera
|
|
{
|
|
get
|
|
{
|
|
if (m_EventCamera == null)
|
|
m_EventCamera = GetComponent<Camera>();
|
|
|
|
if (m_EventCamera == null)
|
|
return Camera.main;
|
|
|
|
return m_EventCamera ;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Depth used to determine the order of event processing.
|
|
/// </summary>
|
|
public virtual int depth
|
|
{
|
|
get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Event mask used to determine which objects will receive events.
|
|
/// </summary>
|
|
public int finalEventMask
|
|
{
|
|
get { return (eventCamera != null) ? eventCamera.cullingMask & m_EventMask : kNoEventMaskSet; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used.
|
|
/// </summary>
|
|
public LayerMask eventMask
|
|
{
|
|
get { return m_EventMask; }
|
|
set { m_EventMask = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Max number of ray intersection allowed to be found.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A value of zero will represent using the allocating version of the raycast function where as any other value will use the non allocating version.
|
|
/// </remarks>
|
|
public int maxRayIntersections
|
|
{
|
|
get { return m_MaxRayIntersections; }
|
|
set { m_MaxRayIntersections = value; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a ray going from camera through the event position and the distance between the near and far clipping planes along that ray.
|
|
/// </summary>
|
|
/// <param name="eventData">The pointer event for which we will cast a ray.</param>
|
|
/// <param name="ray">The ray to use.</param>
|
|
/// <param name="eventDisplayIndex">The display index used.</param>
|
|
/// <param name="distanceToClipPlane">The distance between the near and far clipping planes along the ray.</param>
|
|
/// <returns>True if the operation was successful. false if it was not possible to compute, such as the eventPosition being outside of the view.</returns>
|
|
protected bool ComputeRayAndDistance(PointerEventData eventData, ref Ray ray, ref int eventDisplayIndex, ref float distanceToClipPlane)
|
|
{
|
|
if (eventCamera == null)
|
|
return false;
|
|
|
|
var eventPosition = MultipleDisplayUtilities.RelativeMouseAtScaled(eventData.position);
|
|
if (eventPosition != Vector3.zero)
|
|
{
|
|
// We support multiple display and display identification based on event position.
|
|
eventDisplayIndex = (int)eventPosition.z;
|
|
|
|
// Discard events that are not part of this display so the user does not interact with multiple displays at once.
|
|
if (eventDisplayIndex != eventCamera.targetDisplay)
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
// The multiple display system is not supported on all platforms, when it is not supported the returned position
|
|
// will be all zeros so when the returned index is 0 we will default to the event data to be safe.
|
|
eventPosition = eventData.position;
|
|
}
|
|
|
|
// Cull ray casts that are outside of the view rect. (case 636595)
|
|
if (!eventCamera.pixelRect.Contains(eventPosition))
|
|
return false;
|
|
|
|
ray = eventCamera.ScreenPointToRay(eventPosition);
|
|
// compensate far plane distance - see MouseEvents.cs
|
|
float projectionDirection = ray.direction.z;
|
|
distanceToClipPlane = Mathf.Approximately(0.0f, projectionDirection)
|
|
? Mathf.Infinity
|
|
: Mathf.Abs((eventCamera.farClipPlane - eventCamera.nearClipPlane) / projectionDirection);
|
|
return true;
|
|
}
|
|
|
|
public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
|
|
{
|
|
#if PACKAGE_PHYSICS
|
|
Ray ray = new Ray();
|
|
int displayIndex = 0;
|
|
float distanceToClipPlane = 0;
|
|
if (!ComputeRayAndDistance(eventData, ref ray, ref displayIndex, ref distanceToClipPlane))
|
|
return;
|
|
|
|
int hitCount = 0;
|
|
|
|
if (m_MaxRayIntersections == 0)
|
|
{
|
|
if (ReflectionMethodsCache.Singleton.raycast3DAll == null)
|
|
return;
|
|
|
|
m_Hits = ReflectionMethodsCache.Singleton.raycast3DAll(ray, distanceToClipPlane, finalEventMask);
|
|
hitCount = m_Hits.Length;
|
|
}
|
|
else
|
|
{
|
|
if (ReflectionMethodsCache.Singleton.getRaycastNonAlloc == null)
|
|
return;
|
|
if (m_LastMaxRayIntersections != m_MaxRayIntersections)
|
|
{
|
|
m_Hits = new RaycastHit[m_MaxRayIntersections];
|
|
m_LastMaxRayIntersections = m_MaxRayIntersections;
|
|
}
|
|
|
|
hitCount = ReflectionMethodsCache.Singleton.getRaycastNonAlloc(ray, m_Hits, distanceToClipPlane, finalEventMask);
|
|
}
|
|
|
|
if (hitCount != 0)
|
|
{
|
|
if (hitCount > 1)
|
|
System.Array.Sort(m_Hits, 0, hitCount, RaycastHitComparer.instance);
|
|
|
|
for (int b = 0, bmax = hitCount; b < bmax; ++b)
|
|
{
|
|
var result = new RaycastResult
|
|
{
|
|
gameObject = m_Hits[b].collider.gameObject,
|
|
module = this,
|
|
distance = m_Hits[b].distance,
|
|
worldPosition = m_Hits[b].point,
|
|
worldNormal = m_Hits[b].normal,
|
|
screenPosition = eventData.position,
|
|
displayIndex = displayIndex,
|
|
index = resultAppendList.Count,
|
|
sortingLayer = 0,
|
|
sortingOrder = 0
|
|
};
|
|
resultAppendList.Add(result);
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#if PACKAGE_PHYSICS
|
|
private class RaycastHitComparer : IComparer<RaycastHit>
|
|
{
|
|
public static RaycastHitComparer instance = new RaycastHitComparer();
|
|
public int Compare(RaycastHit x, RaycastHit y)
|
|
{
|
|
return x.distance.CompareTo(y.distance);
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
}
|