ListNFTWebGL.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections;
  3. using Models;
  4. using Newtonsoft.Json;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. using UnityEngine.UI;
  8. using Web3Unity.Scripts.Library.ETHEREUEM.Connect;
  9. #if UNITY_WEBGL
  10. public class ListNFTWebGL : MonoBehaviour
  11. {
  12. private string chain = "ethereum";
  13. private string network = "goerli";
  14. private string _itemPrice = "0.001";
  15. private string _tokenType = "";
  16. private string _itemID = "";
  17. private string account;
  18. public Renderer textureObject;
  19. public Text description;
  20. public Text tokenURI;
  21. public Text contractAddr;
  22. public Text isApproved;
  23. public InputField itemPrice;
  24. public Text playerAccount;
  25. public void Awake()
  26. {
  27. account = PlayerPrefs.GetString("Account");
  28. description.text = "";
  29. tokenURI.text = "";
  30. isApproved.text = "";
  31. contractAddr.text = "";
  32. }
  33. // Start is called before the first frame update
  34. private async void Start()
  35. {
  36. playerAccount.text = account;
  37. var response = await EVM.GetMintedNFT(chain, network, account);
  38. if (response[1].uri.StartsWith("ipfs://"))
  39. response[1].uri = response[1].uri.Replace("ipfs://", "https://ipfs.chainsafe.io/ipfs/");
  40. var webRequest = UnityWebRequest.Get(response[1].uri);
  41. await webRequest.SendWebRequest();
  42. var data =
  43. JsonConvert.DeserializeObject<RootGetNFT>(
  44. System.Text.Encoding.UTF8.GetString(webRequest.downloadHandler.data));
  45. description.text = data.description;
  46. // parse json to get image uri
  47. var imageUri = data.image;
  48. if (imageUri.StartsWith("ipfs://"))
  49. {
  50. imageUri = imageUri.Replace("ipfs://", "https://ipfs.chainsafe.io/ipfs/");
  51. StartCoroutine(DownloadImage(imageUri));
  52. }
  53. tokenURI.text = response[1].uri;
  54. Debug.Log(response[1].uri);
  55. contractAddr.text = response[1].nftContract;
  56. isApproved.text = response[1].isApproved.ToString();
  57. _itemID = response[1].id;
  58. _itemPrice = itemPrice.text;
  59. _tokenType = response[1].tokenType;
  60. }
  61. // ReSharper disable Unity.PerformanceAnalysis
  62. private IEnumerator DownloadImage(string MediaUrl)
  63. {
  64. var request = UnityWebRequestTexture.GetTexture(MediaUrl);
  65. yield return request.SendWebRequest();
  66. if (request.result == UnityWebRequest.Result.ProtocolError)
  67. {
  68. Debug.Log(request.error);
  69. }
  70. else
  71. {
  72. var webTexture = ((DownloadHandlerTexture) request.downloadHandler).texture as Texture2D;
  73. var webSprite = SpriteFromTexture2D(webTexture);
  74. textureObject.GetComponent<Image>().sprite = webSprite;
  75. }
  76. }
  77. private Sprite SpriteFromTexture2D(Texture2D texture)
  78. {
  79. return Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f),
  80. 100.0f);
  81. }
  82. public async void ListItem()
  83. {
  84. var eth = float.Parse(_itemPrice);
  85. float decimals = 1000000000000000000; // 18 decimals
  86. var wei = eth * decimals;
  87. var response =
  88. await EVM.CreateListNftTransaction(chain, network, account, _itemID, Convert.ToDecimal(wei).ToString(),
  89. _tokenType);
  90. var value = Convert.ToInt32(response.tx.value.hex, 16);
  91. try
  92. {
  93. var responseNft = await Web3GL.SendTransactionData(response.tx.to, value.ToString(),
  94. response.tx.gasPrice, response.tx.gasLimit, response.tx.data);
  95. if (responseNft == null) Debug.Log("Empty Response Object:");
  96. }
  97. catch (Exception e)
  98. {
  99. Debug.Log("Revoked Transaction" + e);
  100. }
  101. }
  102. }
  103. #endif