Skip to content

Commit e439621

Browse files
committed
Cleanup
1 parent ff95f57 commit e439621

28 files changed

+631
-517
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Assets/Example.unity renamed to Assets/Example/Example.unity

Lines changed: 108 additions & 345 deletions
Large diffs are not rendered by default.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Assets/MinimapIcon.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

Assets/MinimapObject.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

Assets/MinimapSystem.cs

Lines changed: 0 additions & 114 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace pointcache.Minimap {
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityEngine.UI;
6+
7+
public class MinimapIcon : MonoBehaviour {
8+
9+
public MinimapObject target;
10+
11+
private Image image;
12+
13+
public RectTransform rectTransform;
14+
15+
private void OnEnable() {
16+
17+
image = GetComponent<Image>();
18+
19+
rectTransform = GetComponent<RectTransform>();
20+
21+
if (target.config.icon)
22+
image.sprite = target.config.icon;
23+
24+
image.color = target.config.color;
25+
26+
}
27+
28+
}
29+
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace pointcache.Minimap {
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
public class MinimapObject : MonoBehaviour {
7+
8+
9+
[SerializeField]
10+
private Config m_config = new Config();
11+
12+
public Config config
13+
{
14+
get { return m_config; }
15+
}
16+
17+
[System.Serializable]
18+
public class Config {
19+
20+
public Sprite icon;
21+
public Color color = Color.white;
22+
23+
}
24+
25+
protected virtual void OnEnable() {
26+
MinimapSystem.Instance.RegisterMMObject(this);
27+
}
28+
29+
protected virtual void OnDisable() {
30+
MinimapSystem.Instance.UnRegisterMMObject(this);
31+
}
32+
}
33+
34+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
namespace pointcache.Minimap {
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
public class MinimapSystem : MonoBehaviour {
7+
8+
protected static MinimapSystem m_instance;
9+
public static MinimapSystem Instance
10+
{
11+
get {
12+
if (!m_instance)
13+
m_instance = GameObject.FindObjectOfType<MinimapSystem>();
14+
return m_instance;
15+
}
16+
}
17+
18+
protected List<MinimapIcon> m_iconsPool = new List<MinimapIcon>();
19+
20+
protected Dictionary<MinimapObject, MinimapIcon> m_objs_iconsDict = new Dictionary<MinimapObject, MinimapIcon>();
21+
22+
[SerializeField]
23+
protected Transform m_mapFocus;
24+
25+
[SerializeField]
26+
protected RectTransform m_iconsRoot;
27+
28+
[SerializeField]
29+
protected GameObject m_iconPrefab;
30+
31+
protected Vector3 minimapCenter
32+
{
33+
get {
34+
return m_iconsRoot.rect.center;
35+
}
36+
}
37+
38+
[SerializeField]
39+
protected float m_maxIconDistance = 100f;
40+
41+
[SerializeField]
42+
protected float m_Scale = 1f;
43+
44+
internal void RegisterMMObject(MinimapObject obj) {
45+
46+
var icon = ConstructIcon(obj);
47+
m_iconsPool.Add(icon);
48+
m_objs_iconsDict.Add(obj, icon);
49+
}
50+
51+
internal void UnRegisterMMObject(MinimapObject obj) {
52+
53+
MinimapIcon icon;
54+
55+
m_objs_iconsDict.TryGetValue(obj, out icon);
56+
57+
if (!icon) {
58+
Debug.LogError("Trying to unregister icon that is not registered, how did this happen?");
59+
return;
60+
}
61+
62+
m_iconsPool.Remove(icon);
63+
}
64+
65+
protected virtual void Update() {
66+
67+
int count = m_iconsPool.Count;
68+
69+
for (int i = 0; i < count; i++) {
70+
71+
var icon = m_iconsPool[i];
72+
73+
icon.gameObject.SetActive(CheckVisibility(icon));
74+
75+
icon.rectTransform.anchoredPosition = ConvertPosition(icon.target.transform.position) * m_Scale;
76+
77+
}
78+
}
79+
80+
protected virtual MinimapIcon ConstructIcon(MinimapObject mmobj) {
81+
82+
if (!m_iconPrefab) {
83+
Debug.Log("Icon prefab is null, aborting icon construction.");
84+
return null;
85+
}
86+
87+
m_iconPrefab.SetActive(false);
88+
89+
var go = Instantiate(m_iconPrefab, m_iconsRoot, false);
90+
91+
var icon = go.GetComponent<MinimapIcon>();
92+
93+
icon.target = mmobj;
94+
95+
icon.gameObject.SetActive(true);
96+
97+
m_iconPrefab.SetActive(true);
98+
99+
100+
return icon;
101+
102+
}
103+
104+
protected virtual bool CheckVisibility(MinimapIcon icon) {
105+
return Vector3.Distance(icon.rectTransform.anchoredPosition, Vector3.zero) < m_maxIconDistance;
106+
}
107+
108+
protected virtual Vector3 ConvertPosition(Vector3 position) {
109+
110+
Vector3 transformed = m_mapFocus.transform.InverseTransformPoint(position);
111+
return new Vector3(transformed.x, transformed.z, 0f);
112+
113+
}
114+
115+
}
116+
117+
}

0 commit comments

Comments
 (0)