簡體   English   中英

Unity3D-嘗試將“ .byte”文件用作對象紋理時出現問題

[英]Unity3D - Issue when trying to apply “.byte” file as an object texture

我正在創建一個C#腳本,該腳本在程序啟動時將導入的紋理應用於平面(游戲對象)。 為此,我將用戶選擇的圖像復制到資產文件夾。 然后將其更改為.byte擴展名,這是因為我正在使用TextAsset讀取和設置對象紋理。

現在我認為失敗的是紋理加載,這是完整的腳本代碼:

using UnityEngine;
using System.Collections;
using System.IO;

public class setplant : MonoBehaviour {
    GameObject plant;
    void Start () {
        plant = GameObject.Find("plant");
        string currentdirectory = System.IO.Directory.GetCurrentDirectory();
        string pathconfig = (Path.GetFullPath(Path.Combine(currentdirectory, "..\\Pick_And_Build\\Launcher\\Launcher\\bin\\Debug\\tempfiles\\"))+"instanciateprogram.dat");
        StreamReader reader = File.OpenText(pathconfig);
        string img = reader.ReadLine();
        reader.Close();
        string textureresourcepath = Directory.GetCurrentDirectory() + "\\Assets\\" + Path.GetFileNameWithoutExtension(img) + ".byte";
        if(!File.Exists(textureresourcepath))
            File.Copy(img, textureresourcepath);
        TextAsset imageasset = UnityEditor.AssetDatabase.LoadAssetAtPath(Path.GetFileName(textureresourcepath), typeof(TextAsset)) as TextAsset;
        Texture2D tex = new Texture2D(2, 2);
        tex.LoadImage(imageasset.bytes);
        plant.GetComponent<Renderer>().material.mainTexture = tex;
    }
    void Update () {
    }
}

注意: 映像已復制到Assets文件夾,因此目錄系統正在運行。

此輸出僅是具有默認材質的平面,而不是基於圖像紋理的材質。 有小費嗎?

這是一個愚蠢的錯誤擴展問題。 不是.byte ...“ .bytes”是正確的。 無論如何,感謝您為我提供幫助的人們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM