(ÉDITÉ)
Après quelques recherches, je trouve cette méthode excelent here pour vous:
public static Rect ObjectBounds(GameObject go)
{
Vector3 cen = go.GetComponent<Renderer>().bounds.center;
Vector3 ext = go.GetComponent<Renderer>().bounds.extents;
Vector2[] extentPoints = new Vector2[8]
{
HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z-ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z-ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z+ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z+ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z-ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z-ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z+ext.z)),
HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z+ext.z))
};
Vector2 min = extentPoints[0];
Vector2 max = extentPoints[0];
foreach (Vector2 v in extentPoints)
{
min = Vector2.Min(min, v);
max = Vector2.Max(max, v);
}
return new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
}
Un exemple d'appeler la méthode à un rect et le dessin sur l'écran:
void OnGUI() //EditorGUI.DrawRect must be in this function name
{
//create a rect using the method and the GameObject this script is attached to (gameObject)
Rect boundsRect = new Rect(GUIRectWithObject(gameObject));
//draw a blue rect on the box of the rect (will hide the object)
EditorGUI.DrawRect(boundsRect, Color.blue);
}
Pour que vous obteniez la largeur du rect, écrivez:
float boundswidth = boundsRect.width;
Ce que vous voulez réaliser n'est possible que si le téléphone peut d'abord reconnaître une sorte de marqueur (pourrait être un morceau de papier). Cela permettrait de mapper l'environnement 3d à l'environnement réel. Sinon, vous devrez deviner la taille réelle (disons en centimètres) de l'objet. Vous ne pouvez jamais dire la taille, car vous ne savez pas si l'image est prise 10 cm. de l'objet ou 1 m. de l'objet. – Hristo