Skip to content

Commit 6764074

Browse files
authored
Merge pull request #93 from OpenCommissioning/development
Development
2 parents 0ab6b3c + d27ea08 commit 6764074

16 files changed

Lines changed: 240 additions & 0 deletions

Documentation/Images/SnapPoint.png

11.3 KB
Loading

Documentation/Images/SnapPoint.png.meta

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

Documentation/Images/SnappingHandles.png.meta

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

Documentation/Images/snapping.gif

1.4 MB
Loading

Documentation/Images/snapping.gif.meta

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

Editor/Scripts/SnappingTool.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using OC.SnapingTool;
4+
using UnityEditor;
5+
using UnityEngine;
6+
7+
namespace OC.SnappingTool
8+
{
9+
[CustomEditor(typeof(SnappingHandles)), CanEditMultipleObjects]
10+
public class SnappingHandlesEditor : UnityEditor.Editor
11+
{
12+
13+
private SnapPoint[] _allPoints;
14+
private bool _hotkey;
15+
private bool _focused;
16+
17+
private void OnSceneGUI()
18+
{
19+
HandleHotkeyEvent();
20+
var handle = (SnappingHandles)target;
21+
22+
if (!_hotkey) return;
23+
foreach (var snapPoint in handle.GetComponentsInChildren<SnapPoint>())
24+
{
25+
if (snapPoint.SnapType is SnapPoint.Type.Slot or SnapPoint.Type.None) continue;
26+
var newTargetPosition =Handles.PositionHandle(snapPoint.transform.position, snapPoint.transform.rotation);
27+
if (snapPoint.transform.position == newTargetPosition) continue;
28+
29+
Undo.RecordObject(snapPoint.Parent.transform, "Move SnapPoint Parent");
30+
Undo.RecordObject(snapPoint.transform, "Move SnapPoint");
31+
32+
_allPoints = FindObjectsOfType<SnapPoint>();
33+
var handleSize = HandleUtility.GetHandleSize(newTargetPosition);
34+
var targetPoint = GetSnapPoint(newTargetPosition, snapPoint, _allPoints.ToList(), handleSize);
35+
36+
if (targetPoint)
37+
{
38+
snapPoint.SetGlobalMatrix(targetPoint.transform.GetMatrix() * Matrix4x4.TRS(Vector3.zero,
39+
Quaternion.AngleAxis(180, Vector3.up), Vector3.one));
40+
}
41+
else
42+
{
43+
var matrix = Matrix4x4.TRS(newTargetPosition, snapPoint.transform.rotation, Vector3.one);
44+
snapPoint.SetGlobalMatrix(matrix);
45+
}
46+
}
47+
}
48+
49+
private static SnapPoint GetSnapPoint(Vector3 position, SnapPoint dragged, List<SnapPoint> points, float radius)
50+
{
51+
var distance = radius;
52+
var index = -1;
53+
54+
for (var i = 0; i < points.Count; i++)
55+
{
56+
if (points[i].Parent == dragged.Parent) continue;
57+
if (points[i].GroupId != dragged.GroupId) continue;
58+
if (points[i].SnapType == SnapPoint.Type.Plug) continue;
59+
var dist = Vector3.Distance(position, points[i].transform.position);
60+
if (dist < distance)
61+
{
62+
distance = dist;
63+
index = i;
64+
}
65+
else continue;
66+
}
67+
68+
return index < 0 ? null : points[index];
69+
}
70+
71+
private void HandleHotkeyEvent()
72+
{
73+
var e = Event.current;
74+
if (e.keyCode == KeyCode.S)
75+
{
76+
if (e.type == EventType.KeyDown) _hotkey = Tools.hidden = true;
77+
else if (e.type == EventType.KeyUp) _hotkey = Tools.hidden = false;
78+
}
79+
}
80+
}
81+
}

Editor/Scripts/SnappingTool/SnappingHandlesEditor.cs.meta

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

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,42 @@ You can call this function from the Material Flow component’s context menu in
10411041

10421042
![BoundBoxColliderSize_Inspector.png](Documentation/Images/BoundBoxColliderSize_Inspector.png)
10431043

1044+
1045+
# Snapping Tool
1046+
1047+
The Snapping Tool included in the package allows to snap GameObjects to other GameObjects on defined snap points. This functionality is used to align GameObjects in a scene with precision.
1048+
1049+
## Usage
1050+
1051+
### SnapPoint Class
1052+
The `SnapPoint` class is used to define snapping points on a game object. It is visualized by a blue sphere gizmo.
1053+
1054+
![SnapPoint Component](Documentation/Images/SnapPoint.png)
1055+
1056+
- **Group Id**: Only SnapPoints with the same *Group Id* can snap to each other
1057+
- **Parent**: The *Parent* property must be set to the GameObject that should move with the snapping points.
1058+
- **Type**: Possible Types: *Plug* or *Slot*. Snap Points of *Plug* type can be dragged and snap to *Slot* Snap Points. *Slot* Snap Points can not be dragged.
1059+
- **Draw Gizmos**, **Gizmos Color**, **Gizmos Radius**: controls the Gizmos
1060+
1061+
### SnappingHandles Object
1062+
Provides visual handles for all `SnapPoint` components in the GameObjects children.
1063+
When the scene window is active and the GameObject containing the `SnappingHandles` component is selected, press the **S-Key** to display the handles. These handles can be used to drag the GameObject and snap it to other snap points if they are within proximity.
1064+
1065+
![SnappingHandles Component](Documentation/Images/SnappingHandles.png)
1066+
1067+
## Workflow
1068+
1069+
1. Attach the `SnapPoint` class to a GameObject to define a snap point.
1070+
2. Ensure that the *Parent* property of each `SnapPoint` is set to the GameObject that should be moved when dragging the SnapPoint.
1071+
3. Attach the `SnappingHandles` object to the GameObject that should be moved.
1072+
4. In the scene window, select the GameObject with the `SnappingHandles` component.
1073+
5. Press the **S-Key** to display the snapping handles.
1074+
6. Use the handles to drag the GameObject and snap it to other `SnapPoints`.
1075+
1076+
![Snapping in action](Documentation/Images/snapping.gif)
1077+
1078+
1079+
10441080
# Contributing
10451081

10461082
We welcome contributions from everyone and appreciate your effort to improve this project.

0 commit comments

Comments
 (0)