Fitting a box collider to 3D text perfectly

Hello,

This question has been posed numerous times in various contexts, but I do not know that a solution has ever been found. I have not encountered one.

I have a menu with 3D text, and I have a box collider of nominal dimensions to detect mouse events (hover and click). So far, so good, except that I would like to size my box colliders according to the size of the text, not nominally. The text itself comprises a mesh, and the most obvious solution would be to read the size of the text from the top left vertex to bottom right vertex, then resize the box collider accordingly. However, I cannot find a way to do this.

Does anybody know how? I am using C#.

Thank you in advance,

Peter

Try this. I’m lazy so I just put the code in OnDrawGizmos.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(BoxCollider))]
public class ResizeCollider : MonoBehaviour{

	public float paddingX;
	public float paddingY;

	void OnDrawGizmos(){
		BoxCollider col = (BoxCollider)collider;
		col.center = new Vector3(renderer.bounds.extents.x, renderer.bounds.extents.y - renderer.bounds.size.y, transform.position.z);
		col.size = new Vector3(renderer.bounds.size.x + paddingX, renderer.bounds.size.y + paddingY, 1);
	}
}