File 如何在运行时刷新Unity resources文件夹以获取最新文件(没有AssetDatabase.refresh())?

File 如何在运行时刷新Unity resources文件夹以获取最新文件(没有AssetDatabase.refresh())?,file,unity3d,resources,loading,File,Unity3d,Resources,Loading,我正在为Android创建一个Unity游戏,在运行时捕获屏幕截图,稍后在同一场景中显示。然而,当我尝试此操作时,这些图片似乎是过时的屏幕截图(来自上一个会话),只有在运行时手动刷新资源文件夹时,它们才会更新,而在Android上运行时,我无法这样做 我尝试过其他加载图像的方法,比如WWW和从ByteArray读取图像,但也不起作用。我还使用了AssetDatabase.Refresh(),它可以正常工作,但无法在Android上使用 以下是我的屏幕截图代码(按要求工作): 以下是我将图像显示为

我正在为Android创建一个Unity游戏,在运行时捕获屏幕截图,稍后在同一场景中显示。然而,当我尝试此操作时,这些图片似乎是过时的屏幕截图(来自上一个会话),只有在运行时手动刷新资源文件夹时,它们才会更新,而在Android上运行时,我无法这样做

我尝试过其他加载图像的方法,比如WWW和从ByteArray读取图像,但也不起作用。我还使用了AssetDatabase.Refresh(),它可以正常工作,但无法在Android上使用

以下是我的屏幕截图代码(按要求工作):

以下是我将图像显示为精灵的代码:

public void loadImage(int roundNum)
    {
        loadedSprite = Resources.Load<Sprite>("Screenshots/Screenshot" + roundNum);
        imageDisplay.sprite = loadedSprite;
    }
public void loadImage(int-roundNum)
{
loadedSprite=Resources.Load(“屏幕截图/屏幕截图”+roundNum);
imageDisplay.sprite=加载的sprite;
}

正如我所说的,我希望这会加载截图的最新实例,但这只有在资源文件夹被刷新后才会发生。

我会尝试使用
StreamingAssets
文件夹来实现这一点,但我非常怀疑这是否在Android上也能起作用(我认为您无法在运行时编写)。否则,您可以切换到使用
Application.persistentDataPath


不过,在这两种情况下,您都需要使用另一种加载精灵的方式。比如使用WWW或File IO。

好的,下面是我如何解决这个问题的:

截图代码:

IEnumerator CaptureIt()
{
    string fileName = "Screenshot" + screenshotCounter + ".png";
#if UNITY_EDITOR
    //Path for Editor
    string pathToSave = "Assets/Resources/Screenshots/" + fileName;
#elif UNITY_ANDROID
    //Path for Android
    string pathToSave = fileName;
#endif
    ScreenCapture.CaptureScreenshot(pathToSave);
    screenshotCounter++;
    yield return new WaitForEndOfFrame();
}
Dsiplay代码:

public void loadImage(int roundNum)
{
#if !UNITY_EDITOR
    string url = Application.persistentDataPath +"/"+ "Screenshot" + roundNum + ".png";
    var bytes = File.ReadAllBytes( url );
    Texture2D texture = new Texture2D( 996, 2048 );
    texture.LoadImage( bytes );
    Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
    imageDisplay.sprite= sp ;
#else
    StartCoroutine(loadTex("Screenshots/Screenshot" + roundNum));
    Debug.Log("sping");
#endif

IEnumerator loadTex(string filePath)
{
    ResourceRequest resourceRequest = Resources.LoadAsync<Sprite>(filePath);
    while (!resourceRequest.isDone)
    {
        Debug.Log("yielding 0");
        yield return 0;
    }
    Sprite sp = resourceRequest.asset as Sprite;
    Debug.Log("yield return null");
    imageDisplay.sprite = sp;
    yield return null;
}
public void loadImage(int-roundNum)
{
#如果
字符串url=Application.persistentDataPath+“/”+“屏幕截图”+roundNum+“.png”;
var bytes=File.ReadAllBytes(url);
纹理2d纹理=新纹理2d(9962048);
加载图像(字节);
Sprite sp=Sprite.Create(纹理,新矩形(0,0,texture.width,texture.height),新向量2(0.5f,0.5f));
imageDisplay.sprite=sp;
#否则
Start例程(loadTex(“屏幕截图/屏幕截图”+roundNum));
Debug.Log(“sping”);
#恩迪夫
IEnumerator loadTex(字符串文件路径)
{
ResourceRequest ResourceRequest=Resources.LoadAsync(文件路径);
而(!resourceRequest.isDone)
{
Log(“产生0”);
收益率为0;
}
Sprite sp=resourceRequest.asset作为Sprite;
Log(“yield return null”);
imageDisplay.sprite=sp;
收益返回空;
}

我希望这能有所帮助。

谢谢你的建议,但它仍然只能在编辑器中工作。StreamingAssets或persistentDataPath?我尝试了两者,这是我的新代码:
byte[]bytes;bytes=System.IO.File.ReadAllBytes(Application.persistentDataPath+“Screenshot”+roundNum+“.png”);Texture2D textureToLoad=new Texture2D(9962048,TextureFormat.RGB24,false);textureToLoad.LoadImage(字节);Sprite sp=Sprite.Create(textureToLoad,new Rect(0,0,textureToLoad.width,textureToLoad.height),new Vector2(0.5f,0.5f));imageDisplay.Sprite=sp;
public void loadImage(int roundNum)
{
#if !UNITY_EDITOR
    string url = Application.persistentDataPath +"/"+ "Screenshot" + roundNum + ".png";
    var bytes = File.ReadAllBytes( url );
    Texture2D texture = new Texture2D( 996, 2048 );
    texture.LoadImage( bytes );
    Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
    imageDisplay.sprite= sp ;
#else
    StartCoroutine(loadTex("Screenshots/Screenshot" + roundNum));
    Debug.Log("sping");
#endif

IEnumerator loadTex(string filePath)
{
    ResourceRequest resourceRequest = Resources.LoadAsync<Sprite>(filePath);
    while (!resourceRequest.isDone)
    {
        Debug.Log("yielding 0");
        yield return 0;
    }
    Sprite sp = resourceRequest.asset as Sprite;
    Debug.Log("yield return null");
    imageDisplay.sprite = sp;
    yield return null;
}