Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class MinimapGlLineManager : MonoBehaviour
- {
- private List<Transform> icons = new List<Transform>();
- // Choose the Unlit/Color shader in the Material Settings
- // You can change that color, to change the color of the connecting lines
- [SerializeField]
- private Material lineMat;
- public Camera miniMapCamera;
- public MeshRenderer miniMap;
- private List<GameObject> debugObjects = new List<GameObject>();
- [SerializeField]
- private float heightAboveMap = 0.05f;
- void Awake()
- {
- }
- public void RegisterIcon(Transform icon)
- {
- icons.Add(icon);
- GameObject dot = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- dot.transform.localScale = Vector3.one * 0.1f;
- dot.GetComponent<MeshRenderer>().sharedMaterial = lineMat;
- debugObjects.Add(dot);
- }
- public void DeregisterIcon(Transform icon)
- {
- if (icons.Contains(icon))
- icons.Remove(icon);
- else
- {
- Debug.LogError("icon not found in list. Cannot deregister: " + icon.gameObject.name);
- }
- }
- void DrawConnectingLines(Vector3 go1, Vector3 go2)
- {
- GL.Begin(GL.LINES);
- lineMat.SetPass(0);
- GL.Color(new Color(lineMat.color.r, lineMat.color.g, lineMat.color.b, lineMat.color.a));
- GL.Vertex3(go1.x, go1.y, go1.z);
- GL.Vertex3(go2.x, go2.y, go2.z);
- GL.End();
- }
- private Vector3 GetPointOnMiniMap(Vector3 iconWorldsPosition)
- {
- // Get screen point
- Vector3 screenPoint = miniMapCamera.WorldToScreenPoint(iconWorldsPosition);
- // Convert that to a % of the screen (0,0 in lower left)
- Vector2 screenPointPercent = new Vector2(
- screenPoint.x / miniMapCamera.pixelWidth,
- screenPoint.y / miniMapCamera.pixelHeight);
- // thank god the minimap is planar
- // get point on minimap via size of the mesh renderer
- Vector3 lowerLeft = miniMap.bounds.min;
- Vector3 miniMapPoint = new Vector3(
- lowerLeft.x + miniMap.bounds.size.x * screenPointPercent.x,
- miniMap.transform.position.y + heightAboveMap,
- lowerLeft.z + miniMap.bounds.size.z * screenPointPercent.y);
- return miniMapPoint;
- }
- private bool doOnce;
- private void OnPostRender()
- {
- for (int i = 0; i < icons.Count; i++)
- {
- if (icons[i] == null)
- continue;
- DrawConnectingLines(
- icons[i].transform.position,
- GetPointOnMiniMap(icons[i].transform.position));
- debugObjects[i].transform.position = GetPointOnMiniMap(icons[i].transform.position);
- if (!doOnce)
- {
- doOnce = true;
- Debug.LogFormat(
- "Draw line from: {0} to {1}",
- icons[i].transform.position,
- GetPointOnMiniMap(icons[i].transform.position));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement