Forum rules - please read before posting.

Texture menu

KteKte
edited September 2022 in Technical Q&A

Hello,

Sorry, I can't embed the images.

After doing a puzzle where it involved typing numbers on a keyboard to find a code, I would like to do the same with shapes (pictures).
Image
To which corresponds a Clavier_Formes menu declared in Unity UI Prefab. In this menu I have 9 shape buttons, an E (enter) button and a <- (Backspace) button, + 4 Graphics slots set to UI image and containing the ID of Forme1, Forme2, etc...
On the other hand, I have a variable Num_Emblacement (ID= 5) which determines the position of the Slot on the display screen (int 1, 2, 3 or 4) and int variables var_Slot1 to var_Slot4 with the numerical value of each key typed (ID from 1 to 4).
When you press a shape button, it triggers an ActionList: Key pressed Formes.cs which puts the number of the button (it's a parameter) in the variable var_Slot1, calls a script to transfer the shape (texture) into the Graphic slot n°1, and increments the value of Num_Location.
This works well, I get my screen with my 4 shapes.
What I don't understand is how to make the back key work. I want Slot_Num decreased by 1: OK, Slot1 variable reset to 0: OK, but I don't know how to set Forme1's image back to None, so I can grab another shape.
Here is my script to transfer the texture:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace AC
  {
        public class SlotFormeAffich : MonoBehaviour
        {

        public List<Texture> myNewTexture;

            public void ClavierFormesPress()
            {
                //On récupère le n° d'emplacement
                int numSlot = AC.GlobalVariables.GetIntegerValue(5);
                int numVar = numSlot + 1;
                int numForme = AC.GlobalVariables.GetIntegerValue(numVar);

                var elementName = "Slot" + numVar.ToString();

                MenuElement myElement = AC.PlayerMenus.GetElementWithName("Clavier_Formes", elementName);
                MenuGraphic myGraphic = (MenuGraphic)myElement;
                myGraphic.graphic.texture = myNewTexture[numForme];
             }
        }
  }

The script is placed as a component in Ecran_d_affichage.
Image
When I press a shape key again after backspace, I do get the new key value but the image doesn't change.
Thanks in advance.

Comments

  • Is the backspace intended to clear all images, or just the last one?

    What you can do is create an "empty" texture and assign that in the same way. If you're looking to clear all images, you can iterate through them:

    int min = 0; // Change to suit
    int max = 4; // Change to suit
    public Texture2D emptyTexture; // Assign in Inspector
    
    public void ClearAll ()
    {
        int (i = min; i < max; i++)
        {
            var elementName = "Slot" + i.ToString();
                    MenuElement myElement = AC.PlayerMenus.GetElementWithName("Clavier_Formes", elementName);
                    MenuGraphic myGraphic = (MenuGraphic)myElement;
                    myGraphic.graphic.ReplaceTexture (emptyTexture);
        }
    }
    

    I should mention, though, that if you're setting the Graphic textures entirely through script - there's no need to use AC's Graphic element. You could change the script to refer to the UI RawImage components directly, i.e.:

    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    namespace AC
    {
        public class SlotFormeAffich : MonoBehaviour
        {
    
            public List<Texture> myNewTexture;
            public List<RawImage> rawImages;
    
            public void ClavierFormesPress()
            {
                //On récupère le n° d'emplacement
                int numSlot = AC.GlobalVariables.GetIntegerValue(5);
                int numVar = numSlot + 1;
                int numForme = AC.GlobalVariables.GetIntegerValue(numVar);
                rawImages[numForme].texture = myNewTexture[numForme];
            }
        }
    }
    
  • Thank you for your kindness.
    I hadn't thought of creating an empty texture, I should have.
    However, after having done so, this empty texture did not replace the previous one, it was to be expected since clicking again on a key did nothing.
    But thanks to your code, I replaced my last line
    myGraphic.graphic.texture = myNewTexture[numForme];
    with
    myGraphic.graphic.ReplaceTexture(myNewTexture[numForme]);
    and everything works perfectly.
    I only wanted to empty the last slot.
    I haven't looked at the second code yet, but I'm sure I'll benefit from it in the future.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.