Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Placing GUITextures on the screen dynamically

Discussion in 'Scripting' started by Prospereau, May 4, 2014.

  1. Prospereau

    Prospereau

    Joined:
    Mar 1, 2014
    Posts:
    4
    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.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class shields : MonoBehaviour {
    6.  
    7.     public int textureCount;
    8.     public Texture tex;
    9.  
    10.     float offSetx = 74;
    11.     float offSety = 42;
    12.  
    13.    
    14.  
    15.     void Start ()
    16.     {
    17.         textureCount = 5;
    18.     }
    19.    
    20.    
    21.     void OnGUI ()
    22.     {
    23.         Rect rectangle = new Rect(Screen.width-offSetx,Screen.height-offSety, offSetx-10,offSety-10);
    24.  
    25.         for (short i = 0; i < textureCount; i++)
    26.         {
    27.             rectangle.y = Screen.height-offSety-i*(offSety-10);
    28.             GUI.DrawTexture(rectangle, tex, ScaleMode.ScaleToFit);
    29.         }
    30.     }
    31.  
    32. // just for testing
    33.     void Update()
    34.     {
    35.         if (Input.GetKeyDown(KeyCode.T)  textureCount > 0)
    36.         {
    37.             textureCount--;
    38.         }
    39.         if (Input.GetKeyDown(KeyCode.Z)  textureCount < 5)
    40.         {
    41.             textureCount++;
    42.         }
    43.     }
    44. }
    45.  
    Oh, just noticed you were asking for dynamic position control for GUITextures.
     
    Last edited: May 4, 2014
  3. Prospereau

    Prospereau

    Joined:
    Mar 1, 2014
    Posts:
    4
    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.
     
  4. Prospereau

    Prospereau

    Joined:
    Mar 1, 2014
    Posts:
    4
    You're a star Suddoha...enable and disable worked like a dream!

    Prospereau.