Following on from my experimentation in the "RealRadar" class, I wanted to make something that
To improve performance, I wanted to create a database of objects that were "searchable" at design time (editor) and then let the user pick form the list of objects at run time. They would then be added to a list of "objects I'm searching for" which the script would find in the database, assign an icon, then display it on the screen using OnGUI(). To this end, I will be using a utlity class I shared in a previous post, the hashlist.
//
// Radar - Was originally based off community script, but now completely rewritten
//
using UnityEngine;
using System.Collections;
public class IconRadar : MonoBehaviour
{
public enum IconRadarType : int { FullCircle };
// Display Location
public Vector2 radarLocationCustom;
public IconRadarType radarType = IconRadarType.FullCircle;
public GUISkin gSkin;
public float labelYoffset = 32; // The amount of the screen the radar will use
public float labelLength = 100;
public float labelHeight = 32;
public float yoffset = 1.0f;
public float minRadarDistance = 1.0f;
//searching objects
public static Hashlist searchableItems = new Hashlist();
public static Hashlist searchingItems = new Hashlist();
public Rect searchingGuiScreenRect = new Rect(0, 0, 0, 0);
public Rect searchingGuiButtonRect = new Rect(0, 0, 100, 32);
private bool showSearchWindow = false;
private Vector2 scrollPos = new Vector2();
// Blip information
public Texture2D radarBlip1Icon;
public string radarBlip1Tag;
public Texture2D radarBlip2Icon;
public string radarBlip2Tag;
public Texture2D radarBlip3Icon;
public string radarBlip3Tag;
public Texture2D radarBlip4Icon;
public string radarBlip4Tag;
private GameObject _centerObject;
// Initialize the radar
void Start()
{
// Get our center object
_centerObject = this.gameObject;
}
Texture2D GetIconForObject(Transform t)
{
if (t.tag == radarBlip1Tag)
{
return radarBlip1Icon;
}
else if (t.tag == radarBlip2Tag)
{
return radarBlip2Icon;
}
else if (t.tag == radarBlip3Tag)
{
return radarBlip3Icon;
}
else if (t.tag == radarBlip4Tag)
{
return radarBlip4Icon;
}
else return null;
}
// OnGUI is twice per frame
void OnGUI()
{
if (gSkin != null)
GUI.skin = gSkin;
GUILayout.BeginArea(searchingGuiButtonRect);
if (GUILayout.Button("Search", "Search"))
{
showSearchWindow = !showSearchWindow;
}
GUILayout.EndArea();
if (showSearchWindow)
searchingGuiScreenRect = GUILayout.Window(7337, searchingGuiScreenRect, SearchWindow, "Look For Objects");
foreach (string k in searchingItems.GetList().Keys)
{
Transform t = searchingItems.GetItemFromList(k);
Color color = Color.white;
Texture2D icon;
if (t == null)
continue;
if ((icon = GetIconForObject(t)) == null)
{
continue;
}
GameObject go = t.gameObject;
{
updateBlip(go, color, icon);
}
}
}
void SearchWindow(int windowID)
{
if (!searchableItems.IsEmpty())
{
GUILayout.Label("Select which objects below..");
GUILayout.Label("They will show up in the world as icons to follow");
scrollPos = GUILayout.BeginScrollView(scrollPos);
GUILayout.BeginVertical();
foreach (string k in searchableItems.GetList().Keys)
{
Transform t = searchableItems.GetItemFromList(k);
if (t != null)
{
GUILayout.BeginHorizontal();
if (searchingItems.DoesItemExist(k))
GUILayout.Box(GetIconForObject(t), GUILayout.Width(32));
if (GUILayout.Toggle(searchingItems.DoesItemExist(k), t.gameObject.name))
{
if (!searchingItems.DoesItemExist(k))
searchingItems.AddItemToList(k, t);
}
else
{
if (searchingItems.DoesItemExist(k))
searchingItems.RemoveItemFromList(k);
}
GUILayout.EndHorizontal();
}
else
{
//GUILayout.Label("GUID :'" + k + "' transform empty");
}
}
GUILayout.EndVertical();
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Clear All"))
{
searchingItems.Clear();
}
if (GUILayout.Button("Close"))
{
showSearchWindow = false;
}
GUILayout.EndHorizontal();
}
}
// Draw a blip for an object
void updateBlip(GameObject go, Color blipcolor, Texture2D icon)
{
if (_centerObject && icon != null)
{
//Vector3 centerPos = _centerObject.transform.position;
Vector3 extPos = go.transform.position;
// Get the distance to the object from the centerObject
//float dist = Vector3.Distance(centerPos, extPos);
//float scalef = dist / radarMaxDistance;
//Vector3 unit = (extPos - centerPos).normalized;
//Vector3 pos = centerPos + unit * (radarSize * scalef) + minRadarDistance * unit;
Vector3 pos = extPos;
pos = new Vector3(pos.x, _centerObject.transform.position.y + yoffset, pos.z);
Vector3 screenpos = Camera.main.WorldToScreenPoint(pos);
Rect screenpos_i = new Rect(screenpos.x, Camera.main.pixelHeight - screenpos.y, icon.width, icon.height);
Rect screenpos_l = new Rect(screenpos.x, Camera.main.pixelHeight - screenpos.y + labelYoffset, labelLength, labelHeight);
if (screenpos.z > 0)
{
GUI.DrawTexture(screenpos_i, icon);
if (screenpos_i.Contains(Event.current.mousePosition)) GUI.Label(screenpos_l, go.name); ;
}
}
}
public static void Refresh()
{
Hashtable newList = new Hashtable();
foreach (string k in searchingItems.GetList().Keys)
{
Transform t = searchingItems.GetItemFromList(k);
if (t != null)
newList.Add(k, t);
}
searchingItems.OverriteList(newList);
}
void Awake()
{
searchableItems.Clear();
searchingItems.Clear();
}
}