Upload icon feature
This commit is contained in:
@@ -12,49 +12,80 @@ public class IconMarketplaceUploadIcon : MonoBehaviour
|
||||
public IconMarketplaceManager marketplaceManager;
|
||||
public TMP_Text statusText;
|
||||
public Button backButton;
|
||||
public Button uploadButton;
|
||||
public Button selectButton;
|
||||
public TMP_InputField birdNameInput;
|
||||
public TMP_InputField birdPriceInput;
|
||||
public Image birdImage;
|
||||
private string birdData = null;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
backButton.onClick.AddListener(() => marketplaceManager.SwitchPanel(0));
|
||||
OpenFilePicker();
|
||||
uploadButton.onClick.AddListener(() =>
|
||||
{
|
||||
if (birdNameInput.text.Trim() == string.Empty)
|
||||
{
|
||||
AccountHandler.UpdateStatusText(statusText, "Bird name can't be empty", Color.red);
|
||||
}
|
||||
else if (birdPriceInput.text.Trim() == string.Empty)
|
||||
{
|
||||
AccountHandler.UpdateStatusText(statusText, "Bird price can't be empty", Color.red);
|
||||
}
|
||||
else if (birdData == null)
|
||||
{
|
||||
AccountHandler.UpdateStatusText(statusText, "You must upload an image", Color.red);
|
||||
}
|
||||
else
|
||||
{
|
||||
UploadIcon();
|
||||
}
|
||||
});
|
||||
selectButton.onClick.AddListener(() =>
|
||||
{
|
||||
string path = OpenFilePicker();
|
||||
if (path == null) return;
|
||||
byte[] fileContent;
|
||||
try
|
||||
{
|
||||
fileContent = File.ReadAllBytes(path);
|
||||
if (fileContent.Length > 1024 * 1024)
|
||||
{
|
||||
AccountHandler.UpdateStatusText(statusText, "File size exceeds 1 MB limit", Color.red);
|
||||
return;
|
||||
}
|
||||
birdData = Convert.ToBase64String(fileContent);
|
||||
ImageUtil.RenderFromBase64(birdData, birdImage);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AccountHandler.UpdateStatusText(statusText, "Failed to read file: " + e.Message, Color.red);
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void OpenFilePicker()
|
||||
string OpenFilePicker()
|
||||
{
|
||||
#if UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_EDITOR
|
||||
var filters = new[] {
|
||||
new ExtensionFilter("Image Files", "png"),
|
||||
};
|
||||
var paths = StandaloneFileBrowser.OpenFilePanel("Select an icon file", "", filters, false);
|
||||
if (paths.Length > 0 && !string.IsNullOrEmpty(paths[0]))
|
||||
{
|
||||
UploadFile(paths[0]);
|
||||
return paths[0];
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
async void UploadFile(string path)
|
||||
async void UploadIcon()
|
||||
{
|
||||
byte[] fileContent;
|
||||
try
|
||||
{
|
||||
fileContent = File.ReadAllBytes(path);
|
||||
if (fileContent.Length > 1024 * 1024)
|
||||
{
|
||||
AccountHandler.UpdateStatusText(statusText, "File size exceeds 1 MB limit", Color.red);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AccountHandler.UpdateStatusText(statusText, "Failed to read file: " + e.Message, Color.red);
|
||||
return;
|
||||
}
|
||||
|
||||
EncryptedWWWForm dataForm = new();
|
||||
dataForm.AddField("token", BazookaManager.Instance.GetAccountSession());
|
||||
dataForm.AddField("username", BazookaManager.Instance.GetAccountName());
|
||||
dataForm.AddField("filecontent", Convert.ToBase64String(fileContent));
|
||||
dataForm.AddField("name", birdNameInput.text);
|
||||
dataForm.AddField("price", birdPriceInput.text);
|
||||
dataForm.AddField("filecontent", birdData);
|
||||
using UnityWebRequest request = UnityWebRequest.Post(SensitiveInfo.SERVER_DATABASE_PREFIX + "uploadMarketplaceIcon.php", dataForm.GetWWWForm());
|
||||
request.SetRequestHeader("Requester", "BerryDashClient");
|
||||
request.SetRequestHeader("ClientVersion", Application.version);
|
||||
@@ -91,6 +122,10 @@ public class IconMarketplaceUploadIcon : MonoBehaviour
|
||||
var jsonResponse = JObject.Parse(response);
|
||||
if ((bool)jsonResponse["success"])
|
||||
{
|
||||
birdImage.sprite = Resources.Load<Sprite>("Other/X");
|
||||
birdData = null;
|
||||
birdNameInput.text = "";
|
||||
birdPriceInput.text = "";
|
||||
AccountHandler.UpdateStatusText(statusText, "Icon uploaded successfully!", Color.green);
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user