- Home /
UI Box to appear on mouse position
Im creating a UI box to appear as a "loading" bar before instantiating an object over it and destroying the GUI box. The way my game is set up now is the object that appears after the loading bar completes is instantiated on my mouse position in an OnMouse button down method. Everything works correctly except the UI Box is spawned in the upper left corner of my screen when I insatiate the game object. Ill post the code below to clarify.
My script attached to my object im instantiating:
public class buildProgBar : MonoBehaviour
{
private buildItem[] gameObjects;
float timeLeft;
public float barDisplay;
private Vector2 mousePos;
private Vector2 size;
public Texture2D emptyTex;
public Texture2D fullTex;
private SpriteRenderer sprite;
private void Start()
{
gameObjects = GameObject.Find("BG").GetComponent<GameManager>().buildItems;
timeLeft = gameObjects[GameManager.selectedStruct].BuildTime;
sprite = gameObject.GetComponent<SpriteRenderer>();
sprite.enabled = false;
size = new Vector2(100, 15);
mousePos = new Vector2(GameManager.spawnPosition.x, GameManager.spawnPosition.y);
}
void OnGUI()
{
//BG
GUI.BeginGroup(new Rect(mousePos.x, mousePos.y, size.x, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), fullTex);
//Fill
GUI.BeginGroup(new Rect(0, 0, size.x * barDisplay, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), emptyTex);
GUI.EndGroup();
GUI.EndGroup();
}
private void Update()
{
if (timeLeft > 0)
{
timeLeft -= Time.deltaTime;
barDisplay = timeLeft / gameObjects[GameManager.selectedStruct].BuildTime;
}
if (timeLeft <= 0)
{
sprite.enabled = true;
enabled = false;
}
}
My script called by my Game Manager:
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0) && !helpers.IsPointerOverUIObject())
{
spawnPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
spawnPosition.z = 0.0f;
print(spawnPosition);
selectedBI = buildItems[selectedStruct];
toInstantiate = selectedBI.structure;
if (buildStats.buildPoints >= selectedBI.cost && buildStats.maxPowerLevel + selectedBI.powerCost >= buildStats.maxPowerLevel)
{
buildStats.buildPoints -= selectedBI.cost;
GameObject toBuild = Instantiate(toInstantiate, spawnPosition, Quaternion.identity);
toBuild.transform.SetParent(mainCanvas.transform);
buildStats.availablePower += selectedBI.powerCost;
buildStats.maxPowerLevel += selectedBI.powerGain;
}
}
}
Ive only been working in unity for around 3 days so pardon my mistakes. Thank you!
Answer by niorg2606 · Jun 06, 2019 at 01:19 AM
Hey, welcome to Unity! You should use the new UI system instead of hard-coding it. Scroll through this doc in the manual. Essentially, you can make the UI objects actual GameObjects in your hierarchy.
Hope this helps!
Thank you for your answer! In the unity inspector after clicking the dropdown for game objects and selecting UI I don't see any object that would represent a loading bar. There are the usual slider, scroll view, etc. but nothing to represent a "loading" bar. Am I missing something?
Answer by $$anonymous$$ · Jun 06, 2019 at 02:10 AM
I used a UI image to create a loading bar, made it a game object, instantiated, and then adjusted the fill amount. Thank you niorg2606!
Your answer
Follow this Question
Related Questions
Mouse moves over GUI 1 Answer
Can't display PNG texture within GUI calls 1 Answer
How to go to a new gui.window by clicking a gui.button? 1 Answer
strange Rect behavior on Y axis 1 Answer