1.脚本一
using UnityEngine;
using UnityEngine.UI;
[AddComponentMenu("MiniMap/Inner map")]
[RequireComponent(typeof(Image))]
public class InnerMap : MonoBehaviour
{
private RectTransform _innerMapRect;
public RectTransform InnerMapRect
{
get
{
if (!_innerMapRect)
{
_innerMapRect = GetComponent<RectTransform>(); /**/
}
return _innerMapRect;
}
}
public float getMapRadius()
{
Vector3[] array = new Vector3[4];
InnerMapRect.GetLocalCorners(array);
if (Mathf.Abs(array[0].x) < Mathf.Abs(array[0].y))
{
return Mathf.Abs(array[0].x);
}
return Mathf.Abs(array[0].y);
}
}
2.脚本二
using UnityEngine;
[AddComponentMenu("MiniMap/Map arrow")]
public class MapArrow : MonoBehaviour
{
private RectTransform _arrowRect;
public RectTransform ArrowRect
{
get
{
if (!_arrowRect)
{
_arrowRect = GetComponent<RectTransform>();
if (!_arrowRect)
{
Debug.LogError("没有找到RectTransform。MapArrow脚本必须附加到一个图像。"); /*RectTransform not found. MapArrow script must by attached to an Image.*/
}
}
return _arrowRect;
}
}
public void rotate(Quaternion quat)
{
ArrowRect.rotation = quat;
}
}
3.脚本三
using UnityEngine;
[AddComponentMenu("MiniMap/Map canvas controller")]
[RequireComponent(typeof(RectTransform))]
public class MapCanvasController : MonoBehaviour
{
private static MapCanvasController _instance;
public Transform playerTransform;
public float radarDistance = 10f;
public bool hideOutOfRadius = true;
public bool useOpacity = true;
public float maxRadarDistance = 10f;
public bool rotateMap;
public float scale = 1f;
public float minimalOpacity = 0.3f;
private RectTransform mapRect;
private InnerMap innerMap;
private MapArrow mapArrow;
private MarkerGroup markerGroup;
private float innerMapRadius;
public static MapCanvasController Instance
{
get
{
if (!_instance)
{
MapCanvasController[] array = Object.FindObjectsOfType<MapCanvasController>();
if (array.Length != 0)
{
if (array.Length == 1)
{
_instance = array[0];
}
else
{
Debug.LogError("你在场景中有不止一个MapCanvasController。"); /*You have more than one MapCanvasController in the scene.*/
}
}
else
{
Debug.LogError("你应该在画布上添加Map预制"); /*You should add Map prefab to your canvas*/
}
}
return _instance;
}
}
public InnerMap InnerMapComponent
{
get
{
return innerMap;
}
}
public MarkerGroup MarkerGroup
{
get
{
return markerGroup;
}
}
private void Awake()
{
if (!playerTransform)
{
Debug.LogError("你必须指定播放器转换"); /*You must specify the player transform*/
}
mapRect = GetComponent<RectTransform>();
innerMap = GetComponentInChildren<InnerMap>();
if (!innerMap)
{
Debug.LogError("子组件中缺少InnerMap组件"); /*InnerMap component is missing from children*/
}
mapArrow = GetComponentInChildren<MapArrow>();
if (!mapArrow)
{
Debug.LogError("子组件中缺少MapArrow组件"); /*MapArrow component is missing from children*/
}
markerGroup = GetComponentInChildren<MarkerGroup>();
if (!markerGroup)
{
Debug.LogError("缺少MerkerGroup组件。它必须是InnerMap的子元素"); /*MerkerGroup component is missing. It must be a child of InnerMap*/
}
innerMapRadius = innerMap.getMapRadius();
}
private void Update()
{
if ((bool)playerTransform)
{
if (rotateMap)
{
mapRect.rotation = Quaternion.Euler(new Vector3(0f, 0f, playerTransform.eulerAngles.y));
mapArrow.rotate(Quaternion.identity);
}
else
{
mapArrow.rotate(Quaternion.Euler(new Vector3(0f, 0f, 0f - playerTransform.eulerAngles.y)));
}
}
}
public void checkIn(MapMarker marker)
{
if (!playerTransform)
{
return;
}
float num = radarDistance * scale; /*雷达距离*/
float num2 = maxRadarDistance * scale; /*最大雷达距离*/
if (marker.isActive)
{
float num3 = distanceToPlayer(marker.getPosition()); /*玩家与场景中敌人三维平面距离*/
float num4 = 1f;
if (num3 > num)
{
if (hideOutOfRadius)
{
if (marker.isVisible())
{
marker.hide();
}
return;
}
if (num3 > num2)
{
if (marker.isVisible())
{
marker.hide();
}
return;
}
if (useOpacity)
{
float num5 = num2 - num;
if (num5 <= 0f)
{
Debug.LogError("最大雷达距离应大于雷达距离"); /*Max radar distance should be bigger than radar distance*/
return;
}
float num6 = num3 - num;
num4 = 1f - num6 / num5;
if (num4 < minimalOpacity)
{
num4 = minimalOpacity;
}
}
num3 = num;
}
if (!marker.isVisible())
{
marker.show();
}
Vector3 vector = marker.getPosition() - playerTransform.position; /*玩家至场景怪物中的方向向量*/
Vector3 localPos = new Vector3(vector.x, vector.z, 0f); /*场景三维向量转换为 小地图二维向量*/
localPos.Normalize();
float num7 = marker.markerSize / 2f;
float num8 = num3 / num * (innerMapRadius - num7);
localPos *= num8;
marker.setLocalPos(localPos);
marker.setOpacity(num4);
}
else if (marker.isVisible())
{
marker.hide();
}
}
private float distanceToPlayer(Vector3 other)
{
return Vector2.Distance(new Vector2(playerTransform.position.x, playerTransform.position.z), new Vector2(other.x, other.z));
}
}
4.脚本四
using UnityEngine;
using UnityEngine.UI;
[AddComponentMenu("MiniMap/Map marker")]
public class MapMarker : MonoBehaviour
{
public Sprite markerSprite; /*hoangd红点*/
public float markerSize = 6.5f;
public bool isActive = true;
private Image markerImage;
public Image MarkerImage
{
get
{
return markerImage;
}
}
private void Start()
{
if (!markerSprite)
{
Debug.LogError(" Please, specify the marker sprite.");
}
GameObject gameObject = new GameObject("Marker");
gameObject.AddComponent<Image>();
MapCanvasController instance = MapCanvasController.Instance;
if (!instance)
{
Object.Destroy(base.gameObject);
return;
}
gameObject.transform.SetParent(instance.MarkerGroup.MarkerGroupRect);
markerImage = gameObject.GetComponent<Image>();
markerImage.sprite = markerSprite;
markerImage.rectTransform.localPosition = Vector3.zero;
markerImage.rectTransform.localScale = Vector3.one;
markerImage.rectTransform.sizeDelta = new Vector2(markerSize, markerSize);
markerImage.gameObject.SetActive(false);
}
private void Update()
{
MapCanvasController instance = MapCanvasController.Instance;
if ((bool)instance)
{
MapCanvasController.Instance.checkIn(this);
markerImage.rectTransform.rotation = Quaternion.identity;
}
}
private void OnDestroy()
{
if ((bool)markerImage)
{
Object.Destroy(markerImage.gameObject);
}
}
public void show()
{
markerImage.gameObject.SetActive(true);
}
public void hide()
{
markerImage.gameObject.SetActive(false);
}
public bool isVisible()
{
return markerImage.gameObject.activeSelf;
}
public Vector3 getPosition()
{
return base.gameObject.transform.position;
}
public void setLocalPos(Vector3 pos)
{
markerImage.rectTransform.localPosition = pos;
}
public void setOpacity(float opacity)
{
markerImage.color = new Color(1f, 1f, 1f, opacity);
}
}
5.脚本五
using UnityEngine;
[AddComponentMenu("MiniMap/Marker Group")]
[RequireComponent(typeof(RectTransform))]
public class MarkerGroup : MonoBehaviour
{
private RectTransform _rectTransform;
public RectTransform MarkerGroupRect
{
get
{
if (!_rectTransform)
{
_rectTransform = GetComponent<RectTransform>();
}
return _rectTransform;
}
}
}