我使用下面的代碼將 png 轉換為按鈕上的精靈,它在運行時作業正常。但是當我關閉專案并重新啟動 png 時仍然沒有轉換為 sprite。
public void creatimage(){
string pngpath = Application.dataPath $"/Screenshot{index}.png";
byte[] filedata;
filedata = System.IO.File.ReadAllBytes(pngpath);
Texture2D text = new Texture2D(2, 2);
text.LoadImage(filedata);
Rect rec = new Rect(0, 0, text.width, text.height);
Sprite sp = Sprite.Create(text, rec, new Vector2(0, 0), 0.1f);
child.GetComponent<Image>().sprite = sp;
text.Apply();
}
我需要永遠將 png 轉換為 sprite,我該怎么做?

uj5u.com熱心網友回復:
您可以使用 UnityEditor 將 png 轉換為精靈,然后使用
TextureImporter 將 png 轉換為 sprite
TextureImporter importer = AssetImporter.GetAtPath(filename) as TextureImporter;
importer.textureType = TextureImporterType.Sprite;
EditorUtility.SetDirty(importer);
importer.SaveAndReimport();
uj5u.com熱心網友回復:
好吧,我很確定您只能將紋理加載/保存到磁盤。可以按如下方式保存紋理:
byte[] itemBGBytes = text.EncodeToPNG();
File.WriteAllBytes(pngpath, itemBGBytes);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/438291.html
標籤:unity3d
