Upload icon feature
This commit is contained in:
@@ -4388,6 +4388,8 @@ MonoBehaviour:
|
|||||||
selectButton: {fileID: 1745561408}
|
selectButton: {fileID: 1745561408}
|
||||||
birdNameInput: {fileID: 382778012}
|
birdNameInput: {fileID: 382778012}
|
||||||
birdPriceInput: {fileID: 210372877}
|
birdPriceInput: {fileID: 210372877}
|
||||||
|
birdImage: {fileID: 2023785527}
|
||||||
|
birdData:
|
||||||
--- !u!1 &1708620717
|
--- !u!1 &1708620717
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@@ -12,29 +12,39 @@ public class IconMarketplaceUploadIcon : MonoBehaviour
|
|||||||
public IconMarketplaceManager marketplaceManager;
|
public IconMarketplaceManager marketplaceManager;
|
||||||
public TMP_Text statusText;
|
public TMP_Text statusText;
|
||||||
public Button backButton;
|
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()
|
void Awake()
|
||||||
{
|
{
|
||||||
backButton.onClick.AddListener(() => marketplaceManager.SwitchPanel(0));
|
backButton.onClick.AddListener(() => marketplaceManager.SwitchPanel(0));
|
||||||
OpenFilePicker();
|
uploadButton.onClick.AddListener(() =>
|
||||||
}
|
|
||||||
|
|
||||||
void OpenFilePicker()
|
|
||||||
{
|
{
|
||||||
#if UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX || UNITY_EDITOR
|
if (birdNameInput.text.Trim() == string.Empty)
|
||||||
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]);
|
AccountHandler.UpdateStatusText(statusText, "Bird name can't be empty", Color.red);
|
||||||
}
|
}
|
||||||
#endif
|
else if (birdPriceInput.text.Trim() == string.Empty)
|
||||||
}
|
|
||||||
|
|
||||||
async void UploadFile(string path)
|
|
||||||
{
|
{
|
||||||
|
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;
|
byte[] fileContent;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -44,17 +54,38 @@ public class IconMarketplaceUploadIcon : MonoBehaviour
|
|||||||
AccountHandler.UpdateStatusText(statusText, "File size exceeds 1 MB limit", Color.red);
|
AccountHandler.UpdateStatusText(statusText, "File size exceeds 1 MB limit", Color.red);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
birdData = Convert.ToBase64String(fileContent);
|
||||||
|
ImageUtil.RenderFromBase64(birdData, birdImage);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
AccountHandler.UpdateStatusText(statusText, "Failed to read file: " + e.Message, Color.red);
|
AccountHandler.UpdateStatusText(statusText, "Failed to read file: " + e.Message, Color.red);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
string OpenFilePicker()
|
||||||
|
{
|
||||||
|
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]))
|
||||||
|
{
|
||||||
|
return paths[0];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async void UploadIcon()
|
||||||
|
{
|
||||||
EncryptedWWWForm dataForm = new();
|
EncryptedWWWForm dataForm = new();
|
||||||
dataForm.AddField("token", BazookaManager.Instance.GetAccountSession());
|
dataForm.AddField("token", BazookaManager.Instance.GetAccountSession());
|
||||||
dataForm.AddField("username", BazookaManager.Instance.GetAccountName());
|
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());
|
using UnityWebRequest request = UnityWebRequest.Post(SensitiveInfo.SERVER_DATABASE_PREFIX + "uploadMarketplaceIcon.php", dataForm.GetWWWForm());
|
||||||
request.SetRequestHeader("Requester", "BerryDashClient");
|
request.SetRequestHeader("Requester", "BerryDashClient");
|
||||||
request.SetRequestHeader("ClientVersion", Application.version);
|
request.SetRequestHeader("ClientVersion", Application.version);
|
||||||
@@ -91,6 +122,10 @@ public class IconMarketplaceUploadIcon : MonoBehaviour
|
|||||||
var jsonResponse = JObject.Parse(response);
|
var jsonResponse = JObject.Parse(response);
|
||||||
if ((bool)jsonResponse["success"])
|
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);
|
AccountHandler.UpdateStatusText(statusText, "Icon uploaded successfully!", Color.green);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user