Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

c# - How can I find when moving the mouse pointer over raw image to what file it belong to?

I have on the hard disk some images and saved files. Each image have his saved file. 10 images and 10 saved files :

images and saved games files on the hard disk

Then I'm loading the images to rawimages and when running the game it looks like that :

Images in raw images

This script is loading the images to raw images :

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class SavedGamesSlots : MonoBehaviour
{
    public GameObject saveSlotPrefab;
    public float gap;

    private Transform slots;
    private string[] imagesToLoad;

    // Start is called before the first frame update
    void Start()
    {
        imagesToLoad = Directory.GetFiles(Application.dataPath + "/screenshots", "*.png");
        slots = GameObject.FindGameObjectWithTag("Slots Content").transform;

        for (int i = 0; i < imagesToLoad.Length; i++)
        {
            var go = Instantiate(saveSlotPrefab);
            go.transform.SetParent(slots);

            Texture2D thisTexture = new Texture2D(100, 100);
            string fileName = imagesToLoad[i];
            byte[] bytes = File.ReadAllBytes(fileName);
            thisTexture.LoadImage(bytes);
            thisTexture.name = fileName;
            go.GetComponent<RawImage>().texture = thisTexture;
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}

And the last script is for moving the mouse over the raw images and then it's making each image the mouse cursor is over to be brighter :

using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MouseHover : MonoBehaviour
{
    public SaveLoad saveLoad;
    public SceneFader sceneFader;
    public RawImagePixelsChange rawImagePixelsChange;

    private bool loadGame = false;

    public void OnHover()
    {
        Debug.Log("Enter");
        rawImagePixelsChange.modifyPixels(0.3f);
        PlaySoundEffect();
        loadGame = true;
    }
    public void OnHoverExit()
    {
        Debug.Log("Exit");
        rawImagePixelsChange.restorePixels();
        loadGame = false;
    }

    private void Update()
    {
        if(loadGame)
        {
            if (Input.GetMouseButtonDown(0))
            {
                StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, "Game"));

                loadGame = false;
            }
        }
    }

    private void PlaySoundEffect()
    {
        transform.GetComponent<AudioSource>().Play();
    }
}

Because I have the files names of the images in the SavedGamesSlots script, How can I bring this files names of each belonging image to the other script MouseHover ?

 string fileName = imagesToLoad[i];

In this script the MouseHover I want to find and get the save file name the mouse cursor is over the image that belong to the saved file. Either inside the OnHover function or inside the GetMouseButtonDown.

Maybe it's be if I will save each image and it's saved file in another folder to identify easier what image belong to what saved file.

question from:https://stackoverflow.com/questions/65945713/how-can-i-find-when-moving-the-mouse-pointer-over-raw-image-to-what-file-it-belo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You already stored the fileName in

string fileName = imagesToLoad[i];
...
thisTexture.name = fileName;

so I guess you could simply access it via

theAccordingRawImage.texture.name

which still contains the trailing .png so you can replace it by e.g.

private const string PATTERN = @".png";
private const string REPLACE = ".savegame";

...

var fileToLoadName = Regex.Replace(theAccordingRawImage.texture.name, PATTERN, REPLACE, RegexOptions.IgnoreCase);

so assuming the MouseHover is attached to the same object as the RawImage component you could simply use

public class MouseHover : MonoBehaviour
{
    ...

    // Reference the according RawImage here via the Inspector
    [SerializeField] private RawImage rawImage;

    private void Awake()
    {
        if(!rawImage) rawImage = GetComponent<RawImage>();
    }

    private string ImageNameToSaveName(string imageName)
    {
        return Regex.Replace(imageName.texture.name, PATTERN, REPLACE, RegexOptions.IgnoreCase);
    }

    public void OnHover()
    {
        Debug.Log($"Entered {rawImage.texture.name}", this);
        Debug.Log($"LoadFile is {ImageNameToSaveName(rawImage.texture.name)}", this);

        ...
    }

    ...

    private void Update()
    {
        if(loadGame)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log($"Clicked on {rawImage.texture.name}", this);
                Debug.Log($"LoadFile is {ImageNameToSaveName(rawImage.texture.name)}", this);
                StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, "Game"));

                loadGame = false;
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...