πŸ¦₯Placement Bounds

Check out how placement bounds function and how to implement your own!

Overview

For defining object's placement bounds interface IPlacementBounds is used, it must define bounds and rotation the object and its reference to gameObject itself.

Interface
/// <summary>
/// Interface for providing placement bounds for a placing object.
/// </summary>
public interface IPlacementBounds
{
    /// <summary>
    /// Root game object of the placing object.
    /// </summary>
    public GameObject gameObject { get; }

    /// <summary>
    /// Bounds relative to local position of the root "gameObject".
    /// </summary>
    public Bounds PlacementBounds { get; }

    /// <summary>
    /// Absolute rotation of the placing object.
    /// </summary>
    public Quaternion PlacementRotation { get; }
}

Provided components which implement this interface:

  • APlacementBounds: abstract class for implementing custom behaviour

  • CustomPlacementBounds: defines bounds by center & size properties

  • RendererPlacementBounds: supports referencing a specific renderer from which bounds are retrieved

  • ColliderPlacementBounds: supports referencing a specific collider from which bounds are calculated

  • IPlacementBounds or APlacementBounds can be used to implement your own behaviour for placement bounds

Best Practices

It is recommended that these bounds are defined in Editor and not at runtime. However it is still possible to do it in code.

Example 1:

// Assumes this script is attached to the placing object itself
void Awake() 
{
    CustomPlacementBounds bounds = AddComponent<CustomPlacementBounds>();
    bounds.center = Vector3.zero;
    bounds.size = new Vector3(10, 2, 5);
}

Example 2:

// Assumes this script is attached to the placing object itself
[SerializeField] private Renderer mainRenderer;

void Awake() 
{
    RendererPlacementBounds bounds = AddComponent<RendererPlacementBounds>();
    bounds.SetRenderer(mainRenderer);
}

Last updated