原理:
1将物体世界坐标转为屏幕坐标 计算物体所代表的屏幕宽度比例
Start{
Vector3 v1 = Camera.main.WorldToScreenPoint(GizmoObj.transform.position);
Vector3 v2 = Camera.main.WorldToScreenPoint(GizmoObj.transform.position+ Camera.main.transform.right);
float Scale= Mathf.Abs(v2.x - v1.x);//屏幕宽度 ---》先获取默认比例下的宽度值
}
2 物体位置改变后执行
private void ChangeGizmoScale()
{
Debug.Log(Camera.main.transform.right);
Vector3 v1 = Camera.main.WorldToScreenPoint(GizmoObj.transform.position);
Vector3 v2 = Camera.main.WorldToScreenPoint(GizmoObj.transform.position+ Camera.main.transform.right);
float n = Scale / Mathf.Abs(v2.x - v1.x);//得到比例差
Vector3 v3=GizmoObj.transform.localScale;
GizmoObj.transform.localScale=new Vector3(v3.x*n,v3.y*n,v3.z*n);//改变新比例
}