Skip to content

Commit

Permalink
fix: proper scale for automated snap zones out of child objects (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Quiroz authored and SimonTheSourcerer committed Aug 21, 2020
1 parent 4a1fcb8 commit 1a3f5c7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Editor/Properties/SnappablePropertyEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,44 @@ public override void OnInspectorGUI()

private void CreateSnapZone(SnappableProperty snappable)
{
// Retrieves a SnapZoneSettings and creates a clone for the snappable object
SnapZoneSettings settings = SnapZoneSettings.Settings;
GameObject snapZoneBlueprint = DuplicateObject(snappable.gameObject);

// Sets the highlight materials to the cloned object and saves it as highlight prefab.
SetHighlightMaterial(snapZoneBlueprint, settings.HighlightMaterial);
GameObject snapZonePrefab = SaveSnapZonePrefab(snapZoneBlueprint);


// Creates a new object for the SnapZone.
GameObject snapObject = new GameObject($"{CleanName(snappable.name)}_SnapZone");
Undo.RegisterCreatedObjectUndo(snapObject, $"Create {snapObject.name}");
EditorUtility.CopySerialized(snappable.transform, snapObject.transform);

// Positions the Snap Zone at the same position, rotation and scale as the snappable object.
snapObject.transform.SetParent(snappable.transform);
snapObject.transform.SetPositionAndRotation(snappable.transform.position, snappable.transform.rotation);
snapObject.transform.localScale = Vector3.one;
snapObject.transform.SetParent(null);

// Adds a Snap Zone component to our new object.
SnapZone snapZone = snapObject.AddComponent<SnapZoneProperty>().SnapZone;
snapZone.ShownHighlightObject = snapZonePrefab;
settings.ApplySettingsToSnapZone(snapZone);

// Calculates the volume of the Snap Zone out of the snappable object.
Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);

foreach (Renderer renderer in snapZoneBlueprint.GetComponentsInChildren<Renderer>())
{
bounds.Encapsulate(renderer.bounds);
}

// Adds a BoxCollider and sets it up.
BoxCollider boxCollider = snapObject.AddComponent<BoxCollider>();
boxCollider.center = bounds.center;
boxCollider.size = bounds.size;
boxCollider.isTrigger = true;

// Disposes the cloned object.
DestroyImmediate(snapZoneBlueprint);
}

Expand Down

0 comments on commit 1a3f5c7

Please sign in to comment.