How to delete assets runtime ?

Hello,

I am looking to delete assets runtime.

Example,
I have an image saved in Assets/Sprite/image.png. That was created runtime with Application.CaptureScreenShot().
Now I want to delete it runtime with code. I don’t want to delete it from editor window. Just delete it, when user pressed the space button.

How can i do this ? Any solution ? or any alternatives ?

Thanks.

see:

I have already tried this, but it doesn’t work. Can you please post an example.
I have tried some other methods also, but none of them worked for me.

What do you mean it does not work? Do you have some error messages?

No.
It will not show anything. No errors & do not delete the file. If I put a debug line after AssetDataBade.DeleteAsset(), that line will be printed, but asset won’t we deleted.

My machine is iMac with Mac OS X 10.11.3 with Unity 5.3.4. Does that make any difference ?

DeleteAsset returns a boolean, what is the value?

Just tried it & it’s false.

false means there is a problem deleting the file. Is your path correct? You can check with File.Exists.
It could also be some file permission issues.

        string path = "Assets/Screenshot.png";
        Application.CaptureScreenshot (path);
        print ("captured");
//      System.IO.File.Delete(path);
//      FileUtil.DeleteFileOrDirectory (path);
        print (AssetDatabase.DeleteAsset (path));
        AssetDatabase.DeleteAsset (path);
        print ("deleted");

Is this correct ?

I would do that this way, to know what is going on:

        string path = "Assets/Screenshot.png";
        Application.CaptureScreenshot(path);
        print("captured");

        if( System.IO.File.Exists(path))
        {
            if( AssetDatabase.DeleteAsset(path) )
                print("deleted");
            else
                Debug.LogError(string.Format("Can not deleted '{0}'. Unknown error.", path));

        }
        else
        {
            Debug.LogError( string.Format("Can not deleted '{0}'. File does not exist.", path) );
        }

Also there could be a path mismatch on mobile device. Also the screenshot migh not exist immediately after calling the function, check the doc:

Tried your code, But sometimes it shows both errors.
Then I put a button and calling those deleting part on click of button, but still it do not work. Though, If I capture screenshot, then minimize the Unity(or open any other window except Unity) & then go back to Unity, Unity imports new screenshot & then I click the button. It will work. But it simply do not work runtime.

That’s logically impossible.

Oh! You might need to AssetDatabase.Refresh() to force Unity to import the screenshot.

AssetDataBase.Refresh() works but it slows down the performance.

There is not much to do about that.

You could also bypass the AssetDataBase and delete your file directly using System.IO.File.Delete.

Before posting on forum, I tried this method also. But it was not working with Application.CaptureScreenshot()
Now I create screenshot with Texture2D.ReadPixel() & then encode to PNG.
Now, I can delete it very easily with System.IO.File.Delete.But it is not working with Application.CaptureScreenshot().

The assetdatabase does not exist at runtime, it’s an editor construct for editor scripting. Then again, the assets folder also does not exist at runtime, so your question doesn’t make a lot of sense.

What are you trying to do? At runtime you just use regular System.IO methods.

Oh! @bhavinpanara22 Do you mean by “runtime”, you want want to delete from a build?
As @Kiwasi says, a lot of editor stuffs (everything within the UnityEditor namespace) are not available from build.

I am trying to take screenshot of a gameplay & use it for something & delete it runtime. So, it will not take more memory.
Any suggestions, how can I do this ?

Runtime means in Play Mode from the build. As I have told, I am taking screenshot, use the screenshot for something & delete it from the project.