Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Must be repeated 5 times #143

Open
le0327 opened this issue Sep 4, 2024 · 0 comments
Open

Must be repeated 5 times #143

le0327 opened this issue Sep 4, 2024 · 0 comments

Comments

@le0327
Copy link

le0327 commented Sep 4, 2024

I am using a translation app so my English may be poor. Sorry about that.

I am using the following code in unity, but the following behavior occurs when selecting a file. My hope is that this behavior will be eliminated and that the file selection can be done only once per button press.

〜Behavior
(1) A file selection dialog opens and a file is selected (the same applies when canceling).
(2) The dialog opens again and does not close until you select the image four times or press cancel.

〜Execution environment
Unity latest version
Windows11

〜Cord

using System.IO;
using UnityEngine;
using UnityEngine.UI;
using SFB; // Namespace for StandaloneFileBrowser

public class CardManager : MonoBehaviour
{
public Button extraButton;
public GameObject cardPrefab;
public InputField nameInputField;
public Transform scrollViewContent; // The parent object for cards in the ScrollView

private string imagePath;
private string cardName;

void Start()
{
    // Call OpenFileBrowser when ExtraButton is clicked
    extraButton.onClick.AddListener(OpenFileBrowser);
}

void OpenFileBrowser()
{
    var extensions = new[] {
        new ExtensionFilter("Image Files", "png", "jpg", "jpeg")
    };

    // Open the file panel and allow only one file to be selected
    string[] paths = StandaloneFileBrowser.OpenFilePanel("Select Image", "", extensions, false);

    // Check if a path was selected and it's not empty
    if (paths != null && paths.Length > 0 && !string.IsNullOrEmpty(paths[0]))
    {
        imagePath = paths[0];
        CreateCard();
    }
    else
    {
        Debug.LogWarning("No image was selected.");
    }
}

void CreateCard()
{
    if (string.IsNullOrEmpty(imagePath))
        return;

    // Instantiate a new card
    GameObject newCard = Instantiate(cardPrefab, scrollViewContent);

    // Load the selected image as a texture
    Texture2D texture = LoadTexture(imagePath);

    // Apply the texture to the card's Image component
    Image cardImage = newCard.GetComponent<Image>();
    cardImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));

    // Get the card name from the input field
    cardName = nameInputField.text;

    // Save the card information
    SaveCardInfo(imagePath, cardName);

    // Clear the path to prevent re-selection until the button is pressed again
    imagePath = null;
}

Texture2D LoadTexture(string path)
{
    byte[] fileData = File.ReadAllBytes(path);
    Texture2D texture = new Texture2D(2, 2);
    texture.LoadImage(fileData);
    return texture;
}

void SaveCardInfo(string imagePath, string cardName)
{
    string saveDataPath = Path.Combine(Application.persistentDataPath, "cardData.txt");

    // Append the card information to a file for saving
    File.AppendAllText(saveDataPath, $"{cardName},{imagePath}\n");
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant