public Vector2 GlobalToLocal(Vector2 point)
{
Container wsc = this.worldSpaceContainer;
if (wsc != null)//I am in a world space
{
Camera cam = wsc.GetRenderCamera();
Vector3 worldPoint;
Vector3 direction;
Vector3 screenPoint = new Vector3();
screenPoint.x = point.x;
screenPoint.y = Screen.height - point.y;
if (wsc.hitArea is MeshColliderHitTest)
{
if (((MeshColliderHitTest)wsc.hitArea).ScreenToLocal(cam, screenPoint, ref point))
{
worldPoint = Stage.inst.cachedTransform.TransformPoint(point.x, -point.y, 0);
direction = Vector3.back;
}
else //当射线没有击中模型时,无法确定本地坐标
return new Vector2(float.NaN, float.NaN);
}
else
{
screenPoint.z = cam.WorldToScreenPoint(this.cachedTransform.position).z;
worldPoint = cam.ScreenToWorldPoint(screenPoint);
Ray ray = cam.ScreenPointToRay(screenPoint);
direction = Vector3.zero - ray.direction;
}
return this.WorldToLocal(worldPoint, direction);
}
else //I am in stage space
{
Vector3 worldPoint = Stage.inst.cachedTransform.TransformPoint(point.x, -point.y, 0);
return this.WorldToLocal(worldPoint, Vector3.back);
}
}