You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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");
}
}
The text was updated successfully, but these errors were encountered:
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
}
The text was updated successfully, but these errors were encountered: