- Home /
GameObject List and For Loop with OnGUI
I've been googling and thinking and I just can't seem to figure this out.
I have a Window, w: 180 h:385. I am using this as a "radar list".
I want to get everything tagged "Enemy" and propagate said list. One column, straight down. The window is draggable, but I'd love to throw a scroll in it incase you have so much in your list.
I also do have icons that would be to the left of the gameobject in the lists name.
I know this should be a simple for loop.. but I just can't figure it out :/
If anyone can help me, I'd love you forever!
Thanks!
This is what I've started with.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PSK_RadarList : MonoBehaviour {
public List<GameObject> radarContacts = new List<GameObject>();
public Rect radarScreen;
void Start () {
radarScreen = new Rect(Screen.width *0.86f,Screen.height * 0.01f,180,385);
}
void OnGUI(){
radarScreen = GUI.Window (1, radarScreen, DrawRadarList, "Radar");
}
void Update () {
}
void DrawRadarList(int WindowID){
GUI.DragWindow(new Rect(0, 0, 180, 20));
}
}
Answer by zharik86 · Oct 10, 2014 at 06:55 AM
As I understood, you need to make show of the list of your "Enemy" in a window. I will write you a small example:
public List<GameObject> radarContacts = new List<GameObject>();
public Rect radarScreen;
public Vector2 scrollPosition = Vector2.zero; //add variable for scroll
void Start() {
radarScreen = new Rect(Screen.width *0.86f,Screen.height * 0.01f,180,385);
//You need initialization your List. I initialization on the simple
GameObject[] tempic = GameObject.FindGameObjectsWithTag("Enemy");
//Of course, you can to use System.Linq for convert
radarContacts.Clear();
for(int i = 0; i < tempic.Length; i++) {
radarContacts.Add(tempic[i]);
}
}
void Update() {
}
void OnGUI() {
//Create your window. Size you know.
radarScreen = GUI.Window (1, radarScreen, DrawRadarList, "Radar");
}
void DrawRadarList(int WindowID) {
//Create scroll list. For example, 1 line have size 200x60
//About parameters: 1 - rectangle of view, 2 - position scroll, 3 - rectangle of full list
scrollPosition = GUI.BeginScrollView(new Rect(0, 0, radarScreen.width, radarScreen.height), scrollPosition, new Rect(0, 0, radarScreen.width, 60*radarContacts.Count));
for(int i = 0; i < radarContacts.Count; i++) {
//For example, show name object
GUI.Label(new Rect(0, 60*i, radarScreen.width, 60), radarContacts[i].name);
}
GUI.EndScrollView();
GUI.DragWindow(new Rect(0, 0, 180, 20));
}
I hope that it will help you.
With a little modification. I got what I wanted out of it. I made a function that repeats every 5 seconds (a radar ping if you will).
I will need to add a bit to it, handling things leaving and entering it, etc. Since its meant to be active all the time.
But good going thank you! :)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to select an Game Object from an Item List 0 Answers
I keep getting an error on my for loop. 1 Answer
How completly remove GUI.Repaint() calls ? 1 Answer
Moving Target Circle 2 Answers