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

Change GUI Button for Image

Discussion in 'Scripting' started by Paykoman, Nov 19, 2014.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys im working on a game and in my character selection i hv some buttons wish i want to add diferent images to them but im not finding the right way: this is part of the code and can be used for exemple: i hv a grid with 6 boxes wish i want to add 1 image to each one. can anyone help me plz?

    Code (CSharp):
    1.     public void DisplayClassSelections()
    2.     {
    3.         //A list of toggle buttons nad each button will be a diference class
    4.         //Selection grid
    5.         classSelection = GUI.SelectionGrid (new Rect (50,50, 250, 300), classSelection, classSelectionNames, 2);    //2 at end indicate the number of squares per line in class selection
    6.         GUI.Label(new Rect(450, 50, 300, 300), FindClassDescription(classSelection));
    7.         GUI.Label(new Rect(450, 100, 300, 300), FindClassStatValues(classSelection));
     
  2. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    You'll probably want to use the overload of SelectionGrid that takes an array of GUIContent. GUIContent can define the text and image(texture) for a given item in the grid.
    Code (csharp):
    1.  
    2. static int SelectionGrid(Rect position, int selected, GUIContent[] content, int xCount);
    3.  
    Code (csharp):
    1.  
    2. // new field in class.
    3. public Texture[] classTextures; // assign in inspector with texture.
    4. // other code
    5.  
    6. public void DisplayClassSelections()
    7. {
    8.   //A list of toggle buttons nad each button will be a diference class
    9.   //Selection grid
    10.   classSelection = GUI.SelectionGrid (new Rect (50,50, 250, 300), classSelection, ConstructContent(), 2);
    11.   // your other code
    12. }
    13.  
    14. private GUIContent[] ConstructContent()
    15. {
    16.    return new GUIContent[]
    17.    {
    18.      new GUIContent(classSelectionNames[0], classTextures[0]),
    19.      new GUIContent(classSelectionNames[1], classTextures[1]),
    20.      new GUIContent(classSelectionNames[2], classTextures[2]),
    21.      new GUIContent(classSelectionNames[3], classTextures[3]),
    22.      new GUIContent(classSelectionNames[4], classTextures[4]),
    23.      new GUIContent(classSelectionNames[5], classTextures[5])
    24.    };
    25. }
    26.  
    something like that
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    the question is that script is not monobehavior so i cant sign it to inspector... this is the full script... wt can i do in this situation? ty
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DisplayCharacterCreationFunctions
    5. {
    6.     private StatAllocationModule statAllocationModule = new StatAllocationModule();
    7.  
    8.     private int classSelection;
    9.     private string[] classSelectionNames = new string[] {"Mage", "Warrior", "Ranger", "Rogue", "Warlock", "Priest"};
    10.     private string playerName = "Enter Name";
    11.     private string playerBio = "Enter Player Bio";        //bio of player
    12.     private bool isMale;            //gender selection
    13.     private int genderSelection;
    14.     private string [] genderTypes = new string[2]{"Male", "Female"};
    15.  
    16.     public void DisplayClassSelections()
    17.     {
    18.         //A list of toggle buttons nad each button will be a diference class
    19.         //Selection grid
    20.         classSelection = GUI.SelectionGrid (new Rect (50,50, 250, 300), classSelection, classSelectionNames, 2);    //2 at end indicate the number of squares per line in class selection
    21.         GUI.Label(new Rect(450, 50, 300, 300), FindClassDescription(classSelection));
    22.         GUI.Label(new Rect(450, 100, 300, 300), FindClassStatValues(classSelection));
    23.     }
    24.  
    25.     private string FindClassDescription(int classSelection)
    26.     {
    27.         genderSelection = GUI.SelectionGrid (new Rect (115, 380, 120, 50), genderSelection, genderTypes, 2);
    28.  
    29.         if (classSelection == 0)
    30.         {
    31.             BaseCharacterClass tempClass = new BaseMageClass();
    32.             return tempClass.CharacterClassDescription;
    33.         }
    34.         else if (classSelection == 1)
    35.         {
    36.             BaseCharacterClass tempClass = new BaseWarriorClass();
    37.             return tempClass.CharacterClassDescription;
    38.         }
    39.         return "No Class Found!";
    40.     }
    41.  
    42.     private string FindClassStatValues(int classSelection)
    43.     {
    44.         if (classSelection == 0)
    45.         {
    46.             BaseCharacterClass tempClass = new BaseMageClass ();
    47.             string tempStats = "Stamina: " + tempClass.Stamina + "\n" + "Endurance: " + tempClass.Endurance + "\n" + "Strenght: " + tempClass.Strenght + "\n" + "Intellect: " + tempClass.Intellect + "\n" + "Agility: " + tempClass.Agility + "\n" + "Resistance: " + tempClass.Resistance;
    48.             return tempStats;
    49.         }
    50.         else if (classSelection == 1)
    51.         {
    52.             BaseCharacterClass tempClass = new BaseWarriorClass ();
    53.             string tempStats = "Stamina: " + tempClass.Stamina + "\n" + "Endurance: " + tempClass.Endurance + "\n" + "Strenght: " + tempClass.Strenght + "\n" + "Intellect: " + tempClass.Intellect + "\n" + "Agility: " + tempClass.Agility + "\n" + "Resistance: " + tempClass.Resistance;
    54.             return tempStats;
    55.         }
    56.         return "No Status Found!";
    57.     }
    58.  
    59.     public void DisplayStateAllocation()
    60.     {
    61.         //A list of stats with plus and minus buttons to add stats
    62.         //Logic to make sure the player dunnot add more then stats given
    63.         statAllocationModule.DisplayStatAllocationModule ();
    64.     }
    65.  
    66.     public void DisplayFinalSetup()
    67.     {
    68.         //name
    69.         playerName = GUI.TextArea (new Rect (20, 10, 150, 25), playerName, 25);
    70.  
    71.         //add a description to your character, a short bio
    72.         playerBio = GUI.TextArea (new Rect (20, 55, 250, 200), playerBio, 250);
    73.     }
    74.  
    75.     private void ChooseClass(int classSelection)
    76.     {
    77.         if (classSelection == 0)
    78.         {
    79.             GameInformation.PlayerClass = new BaseMageClass();  
    80.         }
    81.         else if (classSelection == 1)
    82.         {
    83.             GameInformation.PlayerClass = new BaseWarriorClass();
    84.         }
    85.     }
    86.  
    87.     public void DisplayMainCharacter()
    88.     {
    89.         Transform player = GameObject.FindGameObjectWithTag ("Player").transform;
    90.         GUI.Label (new Rect (Screen.width / 2, 20, 250, 250), "Create New Character");
    91.  
    92.         if (GUI.Button (new Rect (340, 370, 50, 50), "<<<"))
    93.         {
    94.             player.Rotate(Vector3.up * 10);      
    95.         }
    96.  
    97.         if (GUI.Button (new Rect (470, 370, 50, 50), ">>>"))
    98.         {
    99.             player.Rotate(Vector3.down * 10);      
    100.         }
    101.  
    102.         if (CreatePlayerGUI.currentState != CreatePlayerGUI.CreatePlayerStates.FINALSETUP)
    103.         {     //if we not in final state show Next Button
    104.             if (GUI.Button (new Rect (470, 450, 50, 50), "Next"))
    105.             {
    106.                 if (CreatePlayerGUI.currentState == CreatePlayerGUI.CreatePlayerStates.CLASSSELECTION)
    107.                 {
    108.                     ChooseClass (classSelection);
    109.                     CreatePlayerGUI.currentState = CreatePlayerGUI.CreatePlayerStates.STATEALLOCATION;
    110.                 } else if (CreatePlayerGUI.currentState == CreatePlayerGUI.CreatePlayerStates.STATEALLOCATION)
    111.                 {
    112.                     GameInformation.Stamina = statAllocationModule.pointsToAllocate[0];
    113.                     GameInformation.Endurance = statAllocationModule.pointsToAllocate[1];
    114.                     GameInformation.Strenght = statAllocationModule.pointsToAllocate[2];
    115.                     GameInformation.Intellect = statAllocationModule.pointsToAllocate[3];
    116.                     GameInformation.Agility = statAllocationModule.pointsToAllocate[4];
    117.                     GameInformation.Resistance = statAllocationModule.pointsToAllocate[5];
    118.  
    119.                     CreatePlayerGUI.currentState = CreatePlayerGUI.CreatePlayerStates.FINALSETUP;
    120.                 }
    121.             }  
    122.         } else if (CreatePlayerGUI.currentState == CreatePlayerGUI.CreatePlayerStates.FINALSETUP)
    123.         {
    124.             if (GUI.Button (new Rect (405, 450, 50, 50), "Finish"))
    125.             {
    126.                 //Final Save
    127.                 GameInformation.PlayerName = playerName;
    128.                 GameInformation.PlayerBio = playerBio;
    129.                 if(genderSelection == 0)
    130.                 {
    131.                     GameInformation.IsMale = true;
    132.                 }
    133.                 else if (genderSelection == 1)
    134.                 {
    135.                     GameInformation.IsMale = false;
    136.                 }
    137.                 SaveInformation.SaveAllInformation();
    138.  
    139.                 Application.LoadLevel("Level01");
    140.             }
    141.         }
    142.  
    143.         if (CreatePlayerGUI.currentState != CreatePlayerGUI.CreatePlayerStates.CLASSSELECTION)
    144.         {
    145.             if (GUI.Button (new Rect (340, 450, 50, 50), "Back"))
    146.             {
    147.                 if (CreatePlayerGUI.currentState == CreatePlayerGUI.CreatePlayerStates.STATEALLOCATION)
    148.                 {
    149.                     statAllocationModule.didRunOnce = false;
    150.                     GameInformation.PlayerClass = null;
    151.                     CreatePlayerGUI.currentState = CreatePlayerGUI.CreatePlayerStates.CLASSSELECTION;
    152.                 }
    153.                 else if (CreatePlayerGUI.currentState == CreatePlayerGUI.CreatePlayerStates.FINALSETUP)
    154.                 {
    155.                     CreatePlayerGUI.currentState = CreatePlayerGUI.CreatePlayerStates.STATEALLOCATION;
    156.                 }
    157.             }  
    158.         }
    159.     }
    160. }
    161.  
     
  4. mambo_2

    mambo_2

    Joined:
    Aug 19, 2013
    Posts:
    19
    check unity documentation for GUI.Skin or GUI.Style hopefully this will help , they allow you to customize your unity GUI anyway you want.
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Even use diferent images at same same time?
     
  6. mambo_2

    mambo_2

    Joined:
    Aug 19, 2013
    Posts:
    19
    Yes but each button must have it's own style , to have different images.
    you should check some other UI tools on the asset store , NGUI is a great solution plus it's much easier to use and in a game with lots of UI should provide a boost to your frame rate since it doesn't have as many draw calls as unity's native UI do.
     
  7. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    But if i go to NGUI i will hv to remake the code right? ty for help
     
  8. mambo_2

    mambo_2

    Joined:
    Aug 19, 2013
    Posts:
    19
    Probably but it will make your life easier later on once you get used to it :)