You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Map : MonoBehaviour
{
public RectTransform ViewPort; //小地图的视野范围
public Transform Corner1, Corner2; //放置在地形两个对角上标识位置的空物体 ,以便获得地形的尺寸
public GameObject BlipPrefab;//显示玩家位置的预设
public static Map Current;
private Vector2 terrainSize; //地形的大小
private RectTransform mapRect; //获取当前地图的位置
public Map()
{
Current = this;
}
// Use this for initialization
void Start()
{
terrainSize = new Vector2(
Corner2.position.x - Corner1.position.x,
Corner2.position.z - Corner1.position.z);
mapRect = GetComponent<RectTransform>();
}
/// <summary>
/// 把世界位置转成地图位置的方法
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public Vector2 WorldPositionToMap(Vector3 point)
{
var pos = point - Corner1.position; //得到当前位置相对于地形起始角的位置
var mapPos = new Vector2(
point.x / terrainSize.x * mapRect.rect.width,
point.z / terrainSize.y * mapRect.rect.height); //缩放到能适配地图尺寸的点
return mapPos;
}
// Update is called once per frame
void Update()
{
ViewPort.position = WorldPositionToMap(Camera.main.transform.position);
}
}