Home > Enterprise >  How can I create a list with images and change which one is displayed in Unity
How can I create a list with images and change which one is displayed in Unity

Time:07-13

I'm trying to make a ui that has a video shown and after that video is done, it recommends 3 different videos, these videos are dependent on the video that was just watched.

Now did I manage to get the buttons to display different text with a list system.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    using UnityEngine.UI;
    // using UnityEngine.Video;



    public class Recommendations : MonoBehaviour
    {
    public Button Rec1;
    public Button Rec2;
    public Button Rec3;

private int[] recommendedButtonValues;
private float[] recommendedSliderValues;

public List<DescriptionContainer> descriptionContainer = new List<DescriptionContainer>();
public List<ImageContainer> ImageContainer = new List<ImageContainer>();


public VideoplayerScript VideoPlayer;

public void SetRecommendationButtons(int buttonValue, float sliderValue) {
    CheckRecommendations(buttonValue, sliderValue); 
    
    Rec1.GetComponentInChildren<TextMeshProUGUI>().text = descriptionContainer[recommendedButtonValues[0]].descriptions[(int)recommendedSliderValues[0]];
    Rec2.GetComponentInChildren<TextMeshProUGUI>().text = descriptionContainer[recommendedButtonValues[1]].descriptions[(int)recommendedSliderValues[1]];
    Rec3.GetComponentInChildren<TextMeshProUGUI>().text = descriptionContainer[recommendedButtonValues[2]].descriptions[(int)recommendedSliderValues[2]];

        private void CheckRecommendations(int buttonValue, float sliderValue) {
    if (buttonValue == 0) {
        if (sliderValue == 0) {
            recommendedButtonValues = new[] { 2, 0, 0 };
            recommendedSliderValues = new[] { 0f, 2f, 4f };

            //currentInterviewPhoto = ImageContainer[recommendedButtonValues].images[(int)recommendedSliderValues];
        }
        else if (sliderValue == 1) {
            recommendedButtonValues = new[] { 1, 0, 3 };
            recommendedSliderValues = new[] { 1f, 4f, 0f };
        }
    }
    //this is repeated multiple times but I dont think its needed to show to get the point across, both for multiple button as slider values. 

        public void OnClickButton1() {
    VideoPlayer.buttonValue = recommendedButtonValues[0];
    VideoPlayer.sliderValue = recommendedSliderValues[0];

    Debug.Log("Recommendation pressed for the following video, Button value: "   recommendedButtonValues[0]   ". Slider value: "   recommendedSliderValues[0]);

    //repeated for all three buttons

    [System.Serializable]
    public class DescriptionContainer {
        [TextArea(10, 10)]
        public List<string> descriptions;
    }
    

All this works but I'm now trying to do something similar with an images but I cant really get a list to work with an images. The thing I want to do is have 3 different images, depending on the recommendedButtonValues[0,1 or 2] and the recommendedSliderValues[0,1 or 2] it decides which one to show. I've tried creating a list with textures like this:

    [System.Serializable]
    public class ImageContainer
    {
        public List<Texture> images;
    }

        public void WhatImag1e()
        {

            Image1 = ImageContainer[recommendedButtonValues[0]].images[(int)recommendedSliderValues[0]];

        }

But I got the error that Unity cant convert UnityEngine.Texture to UnityEngine.UI.Image. So my questions is what kind of list can I make which does work? Or is this not possible with a list and does I need to do.

I havent copied my entire code since that seems like an overkill but if I missed something I can show it. I'm using Unity version 2019.4.18f1 for this.

CodePudding user response:

A UI.Image is a sprite container, it has a property 'sprite' where you can set sprites and it will change the texture of the widget to the sprite passed.

In your case, if you have textures, you need to change your UI.Image to UI.RawImage (change component in the editor), which accepts textures in its property 'texture'.

  • Related