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

Question Button problem

Discussion in 'Scripting' started by Leandre5, Jul 1, 2021.

  1. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    Hello everyone,
    I am currently creating a game and I need buttons for the creation. I have chosen some 3D buttons as you can see below in the attached files.

    To give this effect I am using two button sprites that are both the same size, but the second one with nothing on top so that it gives the expected effect.

    The problem I have is that I would like to be able to change the text inside the buttons. But I can't find a way to make the text that is attached to the button follow it when it goes down but also when it goes up.

    If you know a way to do what I'm looking for, I'd love to hear from you.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    This is super-super-super basic Unity UI 101 stuff, but like anything UI-related, it is not just code.

    You make a script with a reference to a Button to use the button, and if you need to change the text in it, make another reference to the Text and change it.

    Start with some googling about perhaps

    unity change button text

    EDIT: if you burn text into the graphic like the above, then the problem simply changes to "Swap out the image that the Button includes to display the graphic." Same process, same logic, different output.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Just a few things. I think you're not actually wanting to change the text, right? It's not like you want to change the text from saying Play to Stop for example.

    Instead, I think what you're running into is you have two image components and 1 text component. When you swap the top image component, the text no longer is where you want it because the button looks pressed in, but the text position is still where it would be as if the button isn't pressed.

    The end result, because you aren't actually moving anything when you do the sprite swap is that the components have no way to know that the text needs to go somewhere else. So you have to handle it yourself.

    If what I said is how you're handling it, then you either need to move the text to adjust for the sprite swap or you have two of the same text component and turn off and on the ones you need to.
     
    Kurt-Dekker likes this.
  4. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    The images I sent represent the result I have with a text attached to the button (the sprite) so there is no text attached to the button.

    What I would like is to have a text attached to the button (that I could change as I please via a script) that follows my image but I can't find a way to do this.

    The problem is not really to change the text ( I know how to do it ) but that the text follows the button
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    And I already answered that question for you. If all you're doing is swapping a sprite out, how do you expect it to know where to move the text object? You have to handle it yourself. Now, if you're actually moving the sprite to give it a pressed looked, the text should be a child of the moving image and it will move with it.
     
  6. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    yes that's exactly what I want to do, I understand the reasoning and I have already tried to implement it.

    But my problem is in the implementation, do you have a code example to support your propros or something similar by chance?
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    If the button looks pressed when you click on it, I'd probably add a script to the button that uses the OnPointerDown interface
    https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Selectable.OnPointerDown.html

    Then within the OnPointerDown method, I would swap the position of the text object. or if I went the two text method, turn off and on the appropriate objects.

    Then, the nice thing is, Unity has the OnPointerUp to handle when the user releases the mouse button.
    https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Selectable.OnPointerUp.html

    So it can handle resetting things to normal.
     
  8. Leandre5

    Leandre5

    Joined:
    Nov 1, 2017
    Posts:
    42
    I thank you very much for your answers I found a solution to my problem, for that I use the functions pressed and ondepressed buttons.
    I put you below for the people who are in the same case as me

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using TMPro;
    6. using UnityEngine.UI;
    7.  
    8.  
    9. public class ButtonPose : MonoBehaviour, IDeselectHandler, ISelectHandler
    10. {
    11.  
    12.     [SerializeField]
    13.     private Sprite ButtonUp;
    14.     [SerializeField]
    15.     private Sprite ButtonDown;
    16.  
    17.     public void OnSelect(BaseEventData eventData)
    18.     {
    19.         Debug.Log(this.gameObject.name + " was selected");
    20.         GetComponentInChildren<TMP_Text>().transform.position += Vector3.down * 6;
    21.         GetComponent<Image>().sprite = ButtonDown;
    22.     }
    23.  
    24.     public void OnDeselect(BaseEventData data)
    25.     {
    26.         Debug.Log("Deselected");
    27.         GetComponentInChildren<TMP_Text>().transform.position += Vector3.up * 6;
    28.         GetComponent<Image>().sprite = ButtonUp;
    29.     }
    30. }