如何在运行时刷新 Unity 资源文件夹以提取最新文件(没有 AssetDatabase.Refresh())?
How can I refresh the Unity resources folder during runtime in order to pull the latest files (Without AssetDatabase.Refresh())?
我正在 Unity 中为 Android 创建一个游戏,我在运行时捕获屏幕截图并在稍后的同一场景中显示它们。但是,当我尝试这个时,图片似乎是过时的屏幕截图(来自上一个会话)并且它们只会在运行时更新,如果我手动刷新资源文件夹,我无法在 运行 on Android.
我试过其他加载图像的方法,例如 WWW 和从 ByteArray 读取,但也没有用。我还使用了 AssetDatabase.Refresh() ,它有效但无法在 Android.
上使用
这是我的屏幕截图代码(按要求工作):
IEnumerator CaptureIt()
{
string fileName = "/Screenshot" + screenshotCounter + ".png";
string pathToSave = "Assets/Resources/Screenshots/" + fileName;
ScreenCapture.CaptureScreenshot(pathToSave);
screenshotCounter++;
yield return new WaitForEndOfFrame();
}
这是我将图像显示为精灵的代码:
public void loadImage(int roundNum)
{
loadedSprite = Resources.Load<Sprite>("Screenshots/Screenshot" + roundNum);
imageDisplay.sprite = loadedSprite;
}
正如我所说,我希望这会加载截取的屏幕截图的最新实例,但这只有在资源文件夹已刷新时才会发生。
我会尝试为此使用 StreamingAssets
文件夹,但我非常怀疑它是否适用于 Android(我不认为你可以在运行时在那里写)。否则你可以切换到使用 Application.persistentDataPath
.
不过,在这两种情况下,您都需要使用另一种方式来加载精灵。比如使用 WWW 或文件 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();
}
显示代码:
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;
}
希望对您有所帮助。
我正在 Unity 中为 Android 创建一个游戏,我在运行时捕获屏幕截图并在稍后的同一场景中显示它们。但是,当我尝试这个时,图片似乎是过时的屏幕截图(来自上一个会话)并且它们只会在运行时更新,如果我手动刷新资源文件夹,我无法在 运行 on Android.
我试过其他加载图像的方法,例如 WWW 和从 ByteArray 读取,但也没有用。我还使用了 AssetDatabase.Refresh() ,它有效但无法在 Android.
上使用这是我的屏幕截图代码(按要求工作):
IEnumerator CaptureIt()
{
string fileName = "/Screenshot" + screenshotCounter + ".png";
string pathToSave = "Assets/Resources/Screenshots/" + fileName;
ScreenCapture.CaptureScreenshot(pathToSave);
screenshotCounter++;
yield return new WaitForEndOfFrame();
}
这是我将图像显示为精灵的代码:
public void loadImage(int roundNum)
{
loadedSprite = Resources.Load<Sprite>("Screenshots/Screenshot" + roundNum);
imageDisplay.sprite = loadedSprite;
}
正如我所说,我希望这会加载截取的屏幕截图的最新实例,但这只有在资源文件夹已刷新时才会发生。
我会尝试为此使用 StreamingAssets
文件夹,但我非常怀疑它是否适用于 Android(我不认为你可以在运行时在那里写)。否则你可以切换到使用 Application.persistentDataPath
.
不过,在这两种情况下,您都需要使用另一种方式来加载精灵。比如使用 WWW 或文件 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();
}
显示代码:
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;
}
希望对您有所帮助。