Search Unity

Upload images to buttons

Discussion in 'Scripting' started by Dgaizer31, Mar 17, 2019.

?

Upload images to button

  1. The error is that I did not do the cycle correctly

    1 vote(s)
    100.0%
  2. Or here it is necessary to make a new script differently

    0 vote(s)
    0.0%
  1. Dgaizer31

    Dgaizer31

    Joined:
    Oct 14, 2018
    Posts:
    2
    Good day friends tell me the solution. I want to make a game in the likeness of Mortal Kombat and make icons of the characters on the buttons. There is prefab button which is created on Canvas - >Panel (hanging script №1) depending on how many Models(characters) in the game
    script №1
    Code (CSharp):
    1. using UnityEngine.UI;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. public class ShowModelUIScript : MonoBehaviour
    5. {
    6.     [SerializeField] private ShowModelButton buttonPrefab;//кнопка
    7.     public Sprite[] ImgList; //динамический список картинов
    8.     void Start()
    9.     {
    10.      
    11.         var models = FindObjectOfType<SwitchPerson>().GetModels();
    12.         foreach (var model in models)
    13.         {
    14.             CreateButtonForModel(model);
    15.          
    16.         }
    17.      
    18.     }
    19.     //выдать кнопки выбора на панели изходя от SwitchPerson сколько их чего и как
    20.     public void CreateButtonForModel(Transform model)
    21.     {
    22.         var button = Instantiate(buttonPrefab);
    23.         button.transform.SetParent(this.transform);//сделать его в иерархии подчиненным родитель-(он) сын
    24.         button.transform.localScale = Vector3.one; //размер растянуть
    25.         button.transform.localRotation = Quaternion.identity;
    26.         //переменная для доступа к картинке
    27.         // SpriteRenderer Sprite;
    28.         //доступ к компоненту
    29.         // Sprite = GetComponent<SpriteRenderer>();
    30.         var textbutton= buttonPrefab.GetComponentInChildren<Text>();
    31.         Sprite image;
    32.         //при обновлении экрана задаем картинку по номеру из списка
    33.         for (int i = 0; i < ImgList.Length; i++)
    34.         {
    35.            image=ImgList[i];
    36.          
    37.             buttonPrefab.GetComponent<Image>().sprite = image;
    38.          
    39.             //  buttonPrefab.GetComponent<Image>().sprite = Sprite.sprite; //загрузка в спрайт картиинки
    40.             //   button.Initialize(model, controller.EnableModel);
    41.             //  button.GetComponent<Image>().sprite = Sprite.sprite;
    42.         }
    43.         // button.Initialize(model, FindObjectOfType<SwitchPerson>().EnableModel);
    44.         var controller = FindObjectOfType<SwitchPerson>();
    45.         button.Initialize(model, controller.EnableModel);
    46.        //button.GetComponent<Image>().sprite = Sprite.sprite;
    47.     }
    48. }
    The initialization script button on prepare button ShowModelButton Script No. 2
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System;
    4. public class ShowModelButton : MonoBehaviour
    5. {
    6.     private Transform objectToShow;
    7.     private Action<Transform> clickAction;
    8.  
    9.     public void Initialize(Transform objectToShow,Action<Transform>clickAction)
    10.     {
    11.      
    12.         this.objectToShow = objectToShow;
    13.         this.clickAction = clickAction;
    14.         GetComponentInChildren<Text>().text = objectToShow.gameObject.name; //получить имя модели чтобы показывала на кнопке
    15.      
    16.      
    17.     }
    18.     void Start()
    19.     {
    20.         GetComponent<Button>().onClick.AddListener(HandleButtonClick);
    21.     }
    22.     public void HandleButtonClick()
    23.     {
    24.         clickAction(objectToShow);
    25.     }
    26. }
    The problem is that when the launch takes place, the button in Image Source Image = records the last picture of Sprite from the array ImgList [] script №1 On the attachments you can see what I'm doing wrong and how to fix it????

    Screenshot_1.jpg
    Screenshot_2.jpg Screenshot_3.jpg
     
  2. Dgaizer31

    Dgaizer31

    Joined:
    Oct 14, 2018
    Posts:
    2
    Help me to fix this error