Skip to content

Commit 0bef5d2

Browse files
committed
added comments to sample code + added hover sample
1 parent b9fcced commit 0bef5d2

File tree

5 files changed

+186
-2
lines changed

5 files changed

+186
-2
lines changed

Assets/Samples/MouseEvents/OnMouseEventSample.cs renamed to Assets/Samples/MouseEvents/OnMouseEventDragSample.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
using UnityEngine.InputSystem.EnhancedTouch;
55
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
66

7-
public class OnMouseEventSample : MonoBehaviour
7+
public class OnMouseEventDragSample : MonoBehaviour
88
{
99
private Vector3 screenPoint;
1010
private Vector3 offset;
1111

12+
private Vector3 startPosition;
13+
private bool wasDragging => Vector3.Distance(startPosition, transform.position) > 0.01f;
14+
private bool selected;
15+
16+
// This logic starts the drag operation and safes the offset of the object position to the cursor position, to preserve the relative position.
1217
void OnMouseDown()
1318
{
1419
if (Pointer.current == null)
@@ -18,8 +23,10 @@ void OnMouseDown()
1823
float x = Pointer.current.position.x.value;
1924
float y = Pointer.current.position.y.value;
2025
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(x, y, screenPoint.z));
26+
startPosition = transform.position;
2127
}
2228

29+
// This logic allows dragging the object.
2330
void OnMouseDrag()
2431
{
2532
if (Pointer.current == null)
@@ -29,4 +36,14 @@ void OnMouseDrag()
2936
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
3037
transform.position = curPosition;
3138
}
39+
40+
// This can be used to select objects. This logic allows dragging without selection.
41+
private void OnMouseUp()
42+
{
43+
if (wasDragging)
44+
return;
45+
46+
selected = !selected;
47+
gameObject.GetComponent<Renderer>().material.SetColor("_Color", !selected ? Color.blue : Color.white);
48+
}
3249
}
File renamed without changes.

Assets/Samples/MouseEvents/OnMouseEventsTestScene.unity

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,131 @@ Transform:
216216
m_Children: []
217217
m_Father: {fileID: 0}
218218
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
219+
--- !u!1 &755211103
220+
GameObject:
221+
m_ObjectHideFlags: 0
222+
m_CorrespondingSourceObject: {fileID: 0}
223+
m_PrefabInstance: {fileID: 0}
224+
m_PrefabAsset: {fileID: 0}
225+
serializedVersion: 6
226+
m_Component:
227+
- component: {fileID: 755211108}
228+
- component: {fileID: 755211107}
229+
- component: {fileID: 755211106}
230+
- component: {fileID: 755211105}
231+
- component: {fileID: 755211104}
232+
m_Layer: 0
233+
m_Name: HoverObject
234+
m_TagString: Untagged
235+
m_Icon: {fileID: 0}
236+
m_NavMeshLayer: 0
237+
m_StaticEditorFlags: 0
238+
m_IsActive: 1
239+
--- !u!114 &755211104
240+
MonoBehaviour:
241+
m_ObjectHideFlags: 0
242+
m_CorrespondingSourceObject: {fileID: 0}
243+
m_PrefabInstance: {fileID: 0}
244+
m_PrefabAsset: {fileID: 0}
245+
m_GameObject: {fileID: 755211103}
246+
m_Enabled: 1
247+
m_EditorHideFlags: 0
248+
m_Script: {fileID: 11500000, guid: dfa3000288be14ff2b00dd409f1298c7, type: 3}
249+
m_Name:
250+
m_EditorClassIdentifier: Assembly-CSharp::OnMouseHoverSample
251+
--- !u!65 &755211105
252+
BoxCollider:
253+
m_ObjectHideFlags: 0
254+
m_CorrespondingSourceObject: {fileID: 0}
255+
m_PrefabInstance: {fileID: 0}
256+
m_PrefabAsset: {fileID: 0}
257+
m_GameObject: {fileID: 755211103}
258+
m_Material: {fileID: 0}
259+
m_IncludeLayers:
260+
serializedVersion: 2
261+
m_Bits: 0
262+
m_ExcludeLayers:
263+
serializedVersion: 2
264+
m_Bits: 0
265+
m_LayerOverridePriority: 0
266+
m_IsTrigger: 0
267+
m_ProvidesContacts: 0
268+
m_Enabled: 1
269+
serializedVersion: 3
270+
m_Size: {x: 1, y: 1, z: 1}
271+
m_Center: {x: 0, y: 0, z: 0}
272+
--- !u!23 &755211106
273+
MeshRenderer:
274+
m_ObjectHideFlags: 0
275+
m_CorrespondingSourceObject: {fileID: 0}
276+
m_PrefabInstance: {fileID: 0}
277+
m_PrefabAsset: {fileID: 0}
278+
m_GameObject: {fileID: 755211103}
279+
m_Enabled: 1
280+
m_CastShadows: 1
281+
m_ReceiveShadows: 1
282+
m_DynamicOccludee: 1
283+
m_StaticShadowCaster: 0
284+
m_MotionVectors: 1
285+
m_LightProbeUsage: 1
286+
m_ReflectionProbeUsage: 1
287+
m_RayTracingMode: 2
288+
m_RayTraceProcedural: 0
289+
m_RayTracingAccelStructBuildFlagsOverride: 0
290+
m_RayTracingAccelStructBuildFlags: 1
291+
m_SmallMeshCulling: 1
292+
m_ForceMeshLod: -1
293+
m_MeshLodSelectionBias: 0
294+
m_RenderingLayerMask: 1
295+
m_RendererPriority: 0
296+
m_Materials:
297+
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
298+
m_StaticBatchInfo:
299+
firstSubMesh: 0
300+
subMeshCount: 0
301+
m_StaticBatchRoot: {fileID: 0}
302+
m_ProbeAnchor: {fileID: 0}
303+
m_LightProbeVolumeOverride: {fileID: 0}
304+
m_ScaleInLightmap: 1
305+
m_ReceiveGI: 1
306+
m_PreserveUVs: 0
307+
m_IgnoreNormalsForChartDetection: 0
308+
m_ImportantGI: 0
309+
m_StitchLightmapSeams: 1
310+
m_SelectedEditorRenderState: 3
311+
m_MinimumChartSize: 4
312+
m_AutoUVMaxDistance: 0.5
313+
m_AutoUVMaxAngle: 89
314+
m_LightmapParameters: {fileID: 0}
315+
m_GlobalIlluminationMeshLod: 0
316+
m_SortingLayerID: 0
317+
m_SortingLayer: 0
318+
m_SortingOrder: 0
319+
m_MaskInteraction: 0
320+
m_AdditionalVertexStreams: {fileID: 0}
321+
--- !u!33 &755211107
322+
MeshFilter:
323+
m_ObjectHideFlags: 0
324+
m_CorrespondingSourceObject: {fileID: 0}
325+
m_PrefabInstance: {fileID: 0}
326+
m_PrefabAsset: {fileID: 0}
327+
m_GameObject: {fileID: 755211103}
328+
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
329+
--- !u!4 &755211108
330+
Transform:
331+
m_ObjectHideFlags: 0
332+
m_CorrespondingSourceObject: {fileID: 0}
333+
m_PrefabInstance: {fileID: 0}
334+
m_PrefabAsset: {fileID: 0}
335+
m_GameObject: {fileID: 755211103}
336+
serializedVersion: 2
337+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
338+
m_LocalPosition: {x: 577.55, y: 14, z: 2876.7}
339+
m_LocalScale: {x: 1, y: 1, z: 1}
340+
m_ConstrainProportionsScale: 0
341+
m_Children: []
342+
m_Father: {fileID: 0}
343+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
219344
--- !u!1 &1370017262
220345
GameObject:
221346
m_ObjectHideFlags: 0
@@ -316,6 +441,7 @@ MeshRenderer:
316441
m_SortingLayerID: 0
317442
m_SortingLayer: 0
318443
m_SortingOrder: 0
444+
m_MaskInteraction: 0
319445
m_AdditionalVertexStreams: {fileID: 0}
320446
--- !u!33 &1370017266
321447
MeshFilter:
@@ -334,7 +460,7 @@ Transform:
334460
m_GameObject: {fileID: 1370017262}
335461
serializedVersion: 2
336462
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
337-
m_LocalPosition: {x: 574.96, y: 14, z: 2876.7}
463+
m_LocalPosition: {x: 571.24, y: 14, z: 2876.7}
338464
m_LocalScale: {x: 1, y: 1, z: 1}
339465
m_ConstrainProportionsScale: 0
340466
m_Children: []
@@ -439,3 +565,4 @@ SceneRoots:
439565
- {fileID: 696383077}
440566
- {fileID: 2103964653}
441567
- {fileID: 1370017267}
568+
- {fileID: 755211108}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using UnityEngine;
3+
4+
public class OnMouseHoverSample : MonoBehaviour
5+
{
6+
private bool selected;
7+
8+
// This can be used to select objects.
9+
private void OnMouseUpAsButton()
10+
{
11+
selected = !selected;
12+
gameObject.GetComponent<Renderer>().material.SetColor("_Color", selected ? Color.blue : Color.white);
13+
}
14+
15+
// Highlight the object when the cursor is over it.
16+
private void OnMouseEnter()
17+
{
18+
if (selected)
19+
return;
20+
gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.green);
21+
}
22+
23+
// Set back to a normal state.
24+
private void OnMouseExit()
25+
{
26+
if (selected)
27+
return;
28+
gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.white);
29+
}
30+
31+
// This allows highlighting the object right after deselection, where the cursor is over the object.
32+
private void OnMouseOver()
33+
{
34+
if (selected)
35+
return;
36+
gameObject.GetComponent<Renderer>().material.SetColor("_Color", Color.green);
37+
}
38+
}

Assets/Samples/MouseEvents/OnMouseHoverSample.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)