thirdweb-dev / unity-sdk

Best in class web3 SDK for Unity games
https://portal.thirdweb.com/unity
Apache License 2.0
122 stars 68 forks source link

Is Minting Successful? #70

Closed alibk1 closed 1 year ago

alibk1 commented 1 year ago

In my project, we are creating random photos and download them to user's phone to sell them as NFT. The code below allows users to select the downloaded photo and upload it to ipfs server and then gets its url. Also I want to mint the NFT to user's wallet. But when i run the Mint function the page in attached photo is showing. I dont want to send any NFT or Coin to any walllet from logged walllet. I just want to make the user own the selected photo as NFT.

`using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using Thirdweb; using UnityEngine; using NativeGalleryNamespace; using UnityEngine.UI; using TMPro; using NFTStorage;

public class NFTTransfer : MonoBehaviour { public ThirdwebManager sdk; public NFTStorageClient client; public Contract contract; public TMP_Text text; public TMP_Text text2; public GameObject buttonSelect; public GameObject buttonSend; string url;

void Start()
{
    contract = sdk.SDK.GetContract("0x55d398326f99059ff775485246999027b3197955");
}

void Update()
{
    text2.text = url;
    if (sdk.SDK.wallet != null) 
    {
        buttonSelect.SetActive(true);
    }
    if (client.parsedResponse != null) 
    {
        if (client.parsedResponse.value.cid != "")
        {
            buttonSend.SetActive(true);
            buttonSelect.SetActive(false);
            url = "https://" + client.parsedResponse.value.cid + ".ipfs.nftstorage.link";
        }
    }
}

public void SelectPhoto() 
{
    NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
    {
        Debug.Log("Image path: " + path);
        if (path != null)
        {
            // Create Texture from selected image
            Texture2D texture = NativeGallery.LoadImageAtPath(path);
            if (texture == null)
            {
                Debug.Log("Couldn't load texture from " + path);
                return;
            }

            //// Assign texture to a temporary quad and destroy it after 5 seconds
            //GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
            //quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f;
            //quad.transform.forward = Camera.main.transform.forward;
            //quad.transform.localScale = new Vector3(3f, texture.height / (float)texture.width *3, 3f);

            //Material material = quad.GetComponent<Renderer>().material;
            //if (!material.shader.isSupported) // happens when Standard shader is not included in the build
            //    material.shader = Shader.Find("Legacy Shaders/Diffuse");

            //material.mainTexture = texture;

            text.text = path;
            // If a procedural texture is not destroyed manually, 
            // it will only be freed after a scene change
        }
    });

    client.UploadDataFromStringHttpClient(text.text);

}

public void SendPhoto() 
{
    SendPhotoAsync();
}

public async Task SendPhotoAsync()
{
    var data = await contract.ERC721.Mint(new NFTMetadata()
        {
            name = "A NFT", // Name of the NFT
            image = url, // An image URL or IPFS URI
                                    // Any other valid metadata properties
        }
    );
}

} `

What is the problem?

WhatsApp Image 2023-05-22 at 17 18 04

0xFirekeeper commented 1 year ago

When you mint an NFT you are triggering a transaction, so this is expected behavior. If you don't want the user to have to sign transactions, you can pay for the gas yourself by setting Openzeppelin Defender settings in the ThirdwebManager, hover over the fields to see tooltips. If that is not what you intend to do, and just want to store an unminted NFT locally, you can create a new NFT() and store it for later use, but either way, for it to show up in the contract, you'll need to mint it.

Sidenote: I recommend using a testnet when testing this stuff, it seems like you're on Ethereum Mainnet.