Placing GUITextures on the screen dynamically

Hi All,

I’ve a problem that I think should be quite simple but I can’t figure out how to do it and wondered if anyone could point me in the right direction. I’m creating a space shooter game and during the game the player can deploy a shield. The shield dissolves once it’s taken 5 hits. I wanted to indicate how much shield strength was left by stacking five shield icons (using GUITextures) on the screen and deleting one every time a hit on the shield occurs. Creating a list of GUITextures is easy enough, as is the principle of deleting them as the shield takes hits, but I’m then at a loss as to how to get them to display on the screen. I’d rather use relative positions than pixel insets if possible.

Has anyone else tried something like this? If so, do you have a snippet of example C# code?

Thanks!

Orhan.

You can position them where they have to be shown and diable/enable them whenever they’re supposed to be shown.

But i just coded another possible way to do that and used a simple texture instead of GUITexture, the only thing you need for testing is a texture.

The input in Update is only for testing pursposes and represents your control of how many are supposed to be shown.

using UnityEngine;
using System.Collections;

public class shields : MonoBehaviour {

    public int textureCount;
    public Texture tex;

    float offSetx = 74;
    float offSety = 42;

    

	void Start () 
    {
        textureCount = 5;
	}
	
	
	void OnGUI () 
    {
        Rect rectangle = new Rect(Screen.width-offSetx,Screen.height-offSety, offSetx-10,offSety-10);

        for (short i = 0; i < textureCount; i++)
        {
            rectangle.y = Screen.height-offSety-i*(offSety-10);
            GUI.DrawTexture(rectangle, tex, ScaleMode.ScaleToFit);
        }
	}

// just for testing
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.T)  textureCount > 0)
        {
            textureCount--;
        }
        if (Input.GetKeyDown(KeyCode.Z)  textureCount < 5)
        {
            textureCount++;
        }
    }
}

Oh, just noticed you were asking for dynamic position control for GUITextures.

Actually that solution of positioning them on the screen then enabling and disabling the GUITexture as needed sounds just about perfect. It removes all the painful business of trying to instantiate and scale at a certain position. I’ll give it a go and post on how it turns out. Thanks for the help Suddoha!

Prospereau.

You’re a star Suddoha…enable and disable worked like a dream!

Prospereau.