将图像加载为 Texture2D
Loading an image as Texture2D
在我的程序中,我想保存一些屏幕截图并稍后加载它们以计算一些东西。我创建了一个方法来计算图像名称:
static public string generatePhotoName (string cameraName, float time)
{
return "G:/Data/unity/cameraDemo/" +
DataController.measurmentPath +
"/photos" + "/" +
cameraName + "/" +
time.ToString () + "_" + cameraName + ".png";
}
保存时效果很好,但是当我尝试加载图像时,File.Exists (filePath)
returns false
.
但是当我对文件路径进行硬编码时,加载也能正常工作:
static public string generatePhotoName (string cameraName, float time)
{
return "G:/Data/unity/cameraDemo/demo/photos/Camera/test.png";
}
它甚至可以使用 "real" 图片名称(即 3.827817_Camera.png)。
使用 Path.Combine(...)
并将“/”更改为“\”并没有改变任何东西...
// 编辑:这是我的加载方法
static public Texture2D loadPhotoToTexture (string filePath)
{
Texture2D tex = null;
byte[] fileData;
if (File.Exists (filePath)) {
fileData = File.ReadAllBytes (filePath);
tex = new Texture2D (2, 2);
tex.LoadImage (fileData); //..this will auto-resize the texture dimensions.
} else {
Debug.Log (filePath + " does not exist");
}
return tex;
}`
// edit2:更多代码
这就是我调用方法的方式
Texture2D photo = DataController.loadPhotoToTexture(photoData.getFileName ());
这是我的 class PhotoData
public class PhotoData : BaseData
{
private string _cameraName;
public string cameraName {
get { return _cameraName; }
set { _cameraName = value; }
}
private float _time;
public float time {
get { return _time; }
set { _time = value; }
}
public PhotoData ()
{
}
public PhotoData (string cameraName, float time)
{
_cameraName = cameraName;
_time = time;
}
public string getFileName ()
{
return PlayerController.generatePhotoName (_cameraName, _time);
}
}
如果您想将图像存储到磁盘上的某个文件中,而不是在您的游戏文件夹结构中,请执行以下操作:创建一个文件夹,并存储其位置(文件路径)。例如:
string screenshotFileName = time.ToString () + "_" + cameraName + ".png"
string screenshotDirectory;
screenshotDirectory = @"C:\someFolder\anotherFolder\";
try { Directory.CreateDirectory(directory); }
catch (Exception e)
{ //you should catch each exception separately
if (e is DirectoryNotFoundException || e is UnauthorizedAccessException)
Debug.LogError("OMG PANIC");
}
FileInfo filepath = new FileInfo(screenshotDirectory+screenshotFileName);
if(filepath.Exists)
{
//load the file
}
您也可以通过将 screenshotDirectory 更改为
screenshotDirectory = @"screenshots\"+cameraName+@"\";
编辑:
您似乎正确加载了纹理。您是否将其分配给 material 的主纹理?您的代码在哪里遇到问题?例如,如果您 运行 此脚本位于您希望应用纹理的同一对象上:
this.GetComponent<Renderer>().material.mainTexture = loadedTexture;
另外,当你想加载图片时,最好使用 Resources 文件夹,它使用正斜杠,而不是 .将您可能想要在运行时加载的所有内容存储在那里,然后使用 Resources.Load().
问题是,我试图在一次 Update()
调用中保存和加载屏幕截图。
我通过将其更改为右键单击以截取并保存屏幕截图并左键单击以加载屏幕截图来修复它。
在我的程序中,我想保存一些屏幕截图并稍后加载它们以计算一些东西。我创建了一个方法来计算图像名称:
static public string generatePhotoName (string cameraName, float time)
{
return "G:/Data/unity/cameraDemo/" +
DataController.measurmentPath +
"/photos" + "/" +
cameraName + "/" +
time.ToString () + "_" + cameraName + ".png";
}
保存时效果很好,但是当我尝试加载图像时,File.Exists (filePath)
returns false
.
但是当我对文件路径进行硬编码时,加载也能正常工作:
static public string generatePhotoName (string cameraName, float time)
{
return "G:/Data/unity/cameraDemo/demo/photos/Camera/test.png";
}
它甚至可以使用 "real" 图片名称(即 3.827817_Camera.png)。
使用 Path.Combine(...)
并将“/”更改为“\”并没有改变任何东西...
// 编辑:这是我的加载方法
static public Texture2D loadPhotoToTexture (string filePath)
{
Texture2D tex = null;
byte[] fileData;
if (File.Exists (filePath)) {
fileData = File.ReadAllBytes (filePath);
tex = new Texture2D (2, 2);
tex.LoadImage (fileData); //..this will auto-resize the texture dimensions.
} else {
Debug.Log (filePath + " does not exist");
}
return tex;
}`
// edit2:更多代码
这就是我调用方法的方式
Texture2D photo = DataController.loadPhotoToTexture(photoData.getFileName ());
这是我的 class PhotoData
public class PhotoData : BaseData
{
private string _cameraName;
public string cameraName {
get { return _cameraName; }
set { _cameraName = value; }
}
private float _time;
public float time {
get { return _time; }
set { _time = value; }
}
public PhotoData ()
{
}
public PhotoData (string cameraName, float time)
{
_cameraName = cameraName;
_time = time;
}
public string getFileName ()
{
return PlayerController.generatePhotoName (_cameraName, _time);
}
}
如果您想将图像存储到磁盘上的某个文件中,而不是在您的游戏文件夹结构中,请执行以下操作:创建一个文件夹,并存储其位置(文件路径)。例如:
string screenshotFileName = time.ToString () + "_" + cameraName + ".png"
string screenshotDirectory;
screenshotDirectory = @"C:\someFolder\anotherFolder\";
try { Directory.CreateDirectory(directory); }
catch (Exception e)
{ //you should catch each exception separately
if (e is DirectoryNotFoundException || e is UnauthorizedAccessException)
Debug.LogError("OMG PANIC");
}
FileInfo filepath = new FileInfo(screenshotDirectory+screenshotFileName);
if(filepath.Exists)
{
//load the file
}
您也可以通过将 screenshotDirectory 更改为
screenshotDirectory = @"screenshots\"+cameraName+@"\";
编辑: 您似乎正确加载了纹理。您是否将其分配给 material 的主纹理?您的代码在哪里遇到问题?例如,如果您 运行 此脚本位于您希望应用纹理的同一对象上:
this.GetComponent<Renderer>().material.mainTexture = loadedTexture;
另外,当你想加载图片时,最好使用 Resources 文件夹,它使用正斜杠,而不是 .将您可能想要在运行时加载的所有内容存储在那里,然后使用 Resources.Load().
问题是,我试图在一次 Update()
调用中保存和加载屏幕截图。
我通过将其更改为右键单击以截取并保存屏幕截图并左键单击以加载屏幕截图来修复它。