Strange results when using Context Menu and Camera

I have a script that creates a mesh from each corner of the view fustrum at a given distance.

When the script is run by playing the scene, the mesh is generated as expected.

When the context menu is used while playing or not playing the scene, the mesh Y values are correct, but the X values appear to be half of what they should be.

Is there a difference when using Camera.main with Context Menu compared to play mode?


Edit 2:

To hopefully Clarify this issue, I am including a Debug.Log.

This is the Debug.Log for all 3 possible ways of calling the function Startup() and calculating each vertex position based on Camera.main .

Why am I getting different results for the same script? , the only difference being the way the function is called.

START 
UL (-0.8, 0.6, 1.0) : UR (0.8, 0.6, 1.0)
LL (-0.8, -0.6, 1.0) : LR (0.8, -0.6, 1.0)

Constructing Mesh from ContextMenu
UL (-0.4, 0.6, 1.0) : UR (0.4, 0.6, 1.0)
LL (-0.4, -0.6, 1.0) : LR (0.4, -0.6, 1.0)

UPDATE - mouse-click 
UL (-0.8, 0.6, 1.0) : UR (0.8, 0.6, 1.0)
LL (-0.8, -0.6, 1.0) : LR (0.8, -0.6, 1.0)

SO … hit play … function called … calculations are perfect.

Right-Click on the script and use context menu … whoops all the calculations are giving the wrong results.

Call the function by an input in Update … wow, the calculations are correct.

Spot the difference : the time the calculations were wrong, the same function was run using Context Menu.

This is the problem, I am looking for an answer as this is actually greatly affecting how I build my scenes. Thankyou.

The direct Debug values of Camera.main fieldofview and aspect, the results are :

START 
fieldOfView 60 : aspect 1.333333

Constructing Mesh from ContextMenu
fieldOfView 60 : aspect 0.5744382

UPDATE - mouse-click 
fieldOfView 60 : aspect 1.333333

Edit :

I have updated my question with pictures to demonstrate the problem. It is only when using Context Menu that the problem occurs. To me this can only be caused by how Unity sees Camera.main. I need to know why the camera FOV is off when using the context menu.

When the script runs by pressing play :

When the script runs by using Context Menu :


Here is my working script :

#pragma strict
@script RequireComponent(MeshFilter, MeshRenderer)

#if UNITY_EDITOR
@ContextMenu ("Construct Mesh")
function ConstructQuad() 
{
    Debug.Log("Constructing Mesh from ContextMenu");
	
	Startup();
}
#endif

var theCamera : Camera;
var cameraTransform : Transform;

private var mesh : Mesh;

private var uv : Vector2[];
private var verts : Vector3[];
private var tris : int[];
private var normals : Vector3[];

public var uvSize : Vector2 = Vector2.one;
public var uvOffset : Vector2 = Vector2.zero;

public var distance : float = 1.0;


function Start() 
{
	Startup();
}

function Update() 
{
	
}

function Startup() 
{
	if ( theCamera == null )
	{
		theCamera = Camera.mainCamera;
	}
	cameraTransform = theCamera.transform;
	
	if ( !mesh )
	{
		GetComponent(MeshFilter).mesh = mesh = new Mesh();
		mesh.name = "ScreenMesh";
	}
	
	Construct();
}

function Construct() 
{
	mesh.Clear();
	
	verts = new Vector3[4];  
	
	// calculate verts based on camera FOV
	var pos : Vector3 = cameraTransform.position - transform.position;
	
	var halfFOV : float = ( theCamera.fieldOfView * 0.5 ) * Mathf.Deg2Rad;
	var aspect : float = theCamera.aspect;
	
	var height : float = distance * Mathf.Tan( halfFOV );
	var width : float = height * aspect;
	
	// UpperLeft
	verts[0] = pos - (cameraTransform.right * width);
	verts[0] += cameraTransform.up * height;
	verts[0] += cameraTransform.forward * distance;
	
	// UpperRight
	verts[1] = pos + (cameraTransform.right * width);
	verts[1] += cameraTransform.up * height;
	verts[1] += cameraTransform.forward * distance;
	
	// LowerLeft
	verts[2] = pos - (cameraTransform.right * width);
	verts[2] -= cameraTransform.up * height;
	verts[2] += cameraTransform.forward * distance;
	
	// LowerRight
	verts[3] = pos + (cameraTransform.right * width);
	verts[3] -= cameraTransform.up * height;
	verts[3] += cameraTransform.forward * distance;
	
	// ** usual mesh stuff from here onwards **
}

Yes - there is a known problem in unity where the cameras (etc) go to hell, if the Play is not actually Playing.

For example, “viewport” -like calculations are messed up. My investigations suggested that certain of the ports would appear to default to squares, when it is not on Play.

I discovered this problem circa Aug 24, 2012 and posted it here, but nobody cared. I would have made a bug report but for this damn idleness.

PS Sorry - for some reason I cannot see your recent comments. We broke the comment system :slight_smile:

fix line

var width : float = height * aspect *4/3;

or
var width : float = height * aspect * Screen.width/Screen.height;

in unity3d, Screen world change when transform for do not scale 3dObject

if your camera proportion 4:3, camera display more 4-3

sry about my stupid english, but it will be solved…