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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Spritesheet as a GUI element

Discussion in 'Immediate Mode GUI (IMGUI)' started by nepomuko, Jun 14, 2014.

  1. nepomuko

    nepomuko

    Joined:
    Mar 17, 2014
    Posts:
    24
    i' like to have a spritesheet (a flipping coin pixel graphic) to be part of the gui. when i use the sprite on a simple polygon model i can animate the coin flipping by changing the tileset.. this does not work for GUI elements.

    is there a way to change the tiling of GUI elements OR is there a way to get another object in front the GUI layer in order to have it visible in front of the GUI?

    thanks in advance :)
     
  2. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    You can crop or tile a texture using GUI.DrawTextureWithTexCoords

    Parameters

    position: Represent position and "total" size of texture.
    texCoords: Use percentage per unit. Relative Left-Lower corner, and relative scale.

    Axes Origin

    OnGUI origin is Left-Upper corner: position parameter use this origin.
    But, DrawTextureWithTexCoords origin is Left-Lower corner: texCoords use this origin.

    With texCoords parameter you can crop(percentage between 0-1) or tile image (width or height upper to 1).

    To maintain aspect, also you must change "total" size of texture(position parameter).

    Before this, you need save texture initial size(width,height).

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [ExecuteInEditMode]
    4. public class NewBehaviourScript : MonoBehaviour {
    5.     public Texture image;
    6.     [Range(0f,1f)]
    7.     public float x=0f;
    8.     [Range(0f,1f)]
    9.     public float y=0f;
    10.     [Range(0f,1f)]
    11.     public float width=1f;
    12.     [Range(0f,1f)]
    13.     public float height=1f;
    14.     float initialWidth,initialHeight;
    15.     bool imageEnabled=false;
    16.     void Update(){
    17.         if(!this.imageEnabled) if(this.image!=null){
    18.             this.initialWidth = this.image.width;
    19.             this.initialHeight = this.image.height;
    20.             this.imageEnabled=true;
    21.         }
    22.     }
    23.     void OnGUI(){
    24.         if(imageEnabled){
    25.             Rect position = new Rect(0f,0f,this.initialWidth*this.width,this.initialHeight*this.height);
    26.             Rect coords = new Rect(this.x,this.y,this.width,this.height);
    27.             GUI.DrawTextureWithTexCoords(position,this.image,coords);
    28.         }
    29.     }
    30. }
     
  3. nepomuko

    nepomuko

    Joined:
    Mar 17, 2014
    Posts:
    24
    thanks alot!!!!
     
  4. nepomuko

    nepomuko

    Joined:
    Mar 17, 2014
    Posts:
    24
    actually this works for me to get the sprite displays but it overrides the properties of the GUI component, so the positioning of the transform and the pixel inset does work anymore. how can i get this properties working again in order to set the position of the sprite.

    also i'd like to have an animated sprite. i used to change the tiling of the sprite on normal meshes.

    but this way i just can't get around how to do it.