Unity3D世界的坐标与小地图UI上对应坐标之间的转换
程序员文章站
2022-04-04 16:04:22
...
有些游戏开发中需要将某些元素通过图片的方式现实在UI界面上的小地图上,其中就涉及世界坐标与屏幕坐标之间的转换,小编自己写了其中转换过程的代码,方便新学者理解世界坐标与屏幕坐标。代码如下:
/* 该函数作用:世界坐标转换为UI中在地图的图片上对应的坐标
*
* _WordMapRect为世界坐标下地图模型所占区域(x和y为世界坐标中地图模型左上角的值,宽和高分别为地图模型在世界坐标中的实际宽和高的值);
* _ScreenMapRect为屏幕坐标下地图图片所占区域(x和y为地图的图片左上角在屏幕中的位置的坐标值,宽和高分别为地图的图片在屏幕中的所占的大小值);
* _WordPos为世界坐标下物体实际的x和z的坐标值;
*
* 该函数返回一个屏幕坐标点
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class UIPointAnd3DPoint {
public static Rect WorldRect = new Rect(-23.5f, 219.8f, 636.5f, 480);
public static Rect ScreenRect = new Rect(3026.5f, 396.5f, 789, 347);
public static Vector2 WordPosToScreenPos(Rect _WordMapRect, Rect _ScreenMapRect, Vector3 _WordPos, bool IsReverse = false)
{
Vector2 screenPos = Vector2.zero;
screenPos.x = _WordPos.x - _WordMapRect.x;
screenPos.y = _WordPos.z - _WordMapRect.y;
float Ratio_X = screenPos.x / _WordMapRect.width;
float Ratio_Y = screenPos.y / _WordMapRect.height;
if (!IsReverse)
{
screenPos.x = Ratio_X * _ScreenMapRect.width;
screenPos.y = Ratio_Y * _ScreenMapRect.height;
screenPos.x -= _ScreenMapRect.width / 2;
screenPos.y += _ScreenMapRect.height / 2;
}
else if (IsReverse)
{
screenPos.x = Ratio_X * _ScreenMapRect.width;
screenPos.y = Ratio_Y * _ScreenMapRect.height;
screenPos.x -= _ScreenMapRect.width / 2;
screenPos.y += _ScreenMapRect.height / 2;
screenPos.x = -screenPos.x;
screenPos.y = -screenPos.y;
}
return screenPos;
}
public static Vector3 ScreenPosToWordPos(Rect _WordMapRect, Rect _ScreenMapRect, Vector2 _ScreenPos, Vector3 _WorldPos, bool IsReverse = false)
{
Vector3 worldPos = new Vector3(0, _WorldPos.y, 0);
if (!IsReverse)
{
worldPos.x = (_ScreenPos.x - _ScreenMapRect.x) * _WordMapRect.width / _ScreenMapRect.width;
worldPos.z = (_ScreenPos.y - _ScreenMapRect.y) * _WordMapRect.height / _ScreenMapRect.height;
if (_WordMapRect.x <= 0 && _WordMapRect.y >= 0)
{
worldPos.x -= Mathf.Abs(_WordMapRect.x);
worldPos.z += Mathf.Abs(_WordMapRect.y);
}
else if (_WordMapRect.x <= 0 && _WordMapRect.y < 0)
{
worldPos.x -= Mathf.Abs(_WordMapRect.x);
worldPos.z -= Mathf.Abs(_WordMapRect.y);
}
else if (_WordMapRect.x > 0 && _WordMapRect.y >= 0)
{
worldPos.x += Mathf.Abs(_WordMapRect.x);
worldPos.z += Mathf.Abs(_WordMapRect.y);
}
else if (_WordMapRect.x > 0 && _WordMapRect.y < 0)
{
worldPos.x += Mathf.Abs(_WordMapRect.x);
worldPos.z -= Mathf.Abs(_WordMapRect.y);
}
}
else if (IsReverse)
{
worldPos.x = (_ScreenPos.x - _ScreenMapRect.x) * _WordMapRect.width / _ScreenMapRect.width;
worldPos.z = (_ScreenPos.y - _ScreenMapRect.y) * _WordMapRect.height / _ScreenMapRect.height;
worldPos.x = _WordMapRect.width - worldPos.x;
worldPos.z = Mathf.Abs(worldPos.z) - _WordMapRect.height;
if (_WordMapRect.x <= 0 && _WordMapRect.y >= 0)
{
worldPos.x -= Mathf.Abs(_WordMapRect.x);
worldPos.z += Mathf.Abs(_WordMapRect.y);
}
else if (_WordMapRect.x <= 0 && _WordMapRect.y < 0)
{
worldPos.x -= Mathf.Abs(_WordMapRect.x);
worldPos.z -= Mathf.Abs(_WordMapRect.y);
}
else if (_WordMapRect.x > 0 && _WordMapRect.y >= 0)
{
worldPos.x += Mathf.Abs(_WordMapRect.x);
worldPos.z += Mathf.Abs(_WordMapRect.y);
}
else if (_WordMapRect.x > 0 && _WordMapRect.y < 0)
{
worldPos.x += Mathf.Abs(_WordMapRect.x);
worldPos.z -= Mathf.Abs(_WordMapRect.y);
}
}
return worldPos;
}
}
上一篇: php如何改变数组键名