简体   繁体   English

将图像加载为Texture2D

[英]Loading an image as Texture2D

In my program, I want to save some screenshots and load them later on to compute somethings. 在我的程序中,我想保存一些屏幕截图,并稍后加载它们以计算某些内容。 I created a methode to compute the image names: 我创建了一个方法来计算图像名称:

static public string generatePhotoName (string cameraName, float time)
{
    return "G:/Data/unity/cameraDemo/" + 
        DataController.measurmentPath + 
        "/photos" + "/" + 
        cameraName + "/" + 
        time.ToString () + "_" + cameraName + ".png";
}

This worked fine for saving, but when I try to load an image, File.Exists (filePath) returns false . 保存效果很好,但是当我尝试加载图像时, File.Exists (filePath)返回false

But when I hardcoded the filepath, loading works fine too: 但是,当我对文件路径进行硬编码时,加载也可以正常工作:

static public string generatePhotoName (string cameraName, float time)
{
    return "G:/Data/unity/cameraDemo/demo/photos/Camera/test.png";
}

It even works with "real" image names(ie 3.827817_Camera.png). 它甚至可以使用“真实”图像名称(即3.827817_Camera.png)。

Using Path.Combine(...) and changing "/" to "\\" did not change anything... 使用Path.Combine(...)并将“ /”更改为“ \\”并没有改变任何内容。

// edit: this is my load methode //编辑:这是我的加载方法

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: some more code // edit2:更多代码

This is how I call the methode Texture2D photo = DataController.loadPhotoToTexture(photoData.getFileName ()); 这就是我所说的方法Texture2D photo = DataController.loadPhotoToTexture(photoData.getFileName ());

And this is my class PhotoData 这是我的课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);
    }
}

The problem was, that I tried to save and load the screenshots in one Update() -call. 问题是,我试图在一个Update() call中保存并加载屏幕截图。

I fixed it by changing it to right-click to take and save a screenshot and left-click to load the screenshot. 我通过将其更改为右键单击来获取并保存屏幕截图,然后单击鼠标左键来加载屏幕截图来修复它。

If you want to store images to a certain file on disk, not inside your game folder structure, do this: create a folder, and store its location (filepath). 如果要将图像存储到磁盘上的某个文件中,而不是存储在游戏文件夹结构中,请执行以下操作:创建一个文件夹,并存储其位置(文件路径)。 For example: 例如:

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
}

You could also simply create a folder, with a relative path, which will be in the same folder as your executable, by changing the screenshotDirectory to screenshotDirectory = @"screenshots\\"+cameraName+@"\\"; 您也可以通过将screenshotDirectory更改为screenshotDirectory = @"screenshots\\"+cameraName+@"\\"; ,来简单地创建一个具有相对路径的文件夹,该文件夹将与您的可执行文件位于同一文件夹中screenshotDirectory = @"screenshots\\"+cameraName+@"\\";

Edit: You seem to load the texture correctly. 编辑:您似乎正确加载纹理。 Are you assigning it to the material's main texture? 您是否将其分配给材料的主要纹理? Where is your code encountering the problem? 您的代码在哪里遇到问题? For example if you're running this script on the same object that you want the texture to be applied: 例如,如果要在要应用纹理的同一对象上运行此脚本:

this.GetComponent<Renderer>().material.mainTexture = loadedTexture;

Also, when you want to load images, it's best that you use the Resources folder, which uses forwards slashes, not . 另外,当您要加载图像时,最好使用Resources文件夹,该文件夹使用正斜杠而不是。 Store everything that you might want to load on runtime there, and use Resources.Load(). 将您可能要在运行时加载的所有内容存储在那里,并使用Resources.Load()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM