根据这些文章:how to change Texture Max Size with scripting 和Editor class "Texture Importer" + Apply import settings question,我意识到要更改纹理最大尺寸,您需要将纹理保存为资产......
在我的情况下,我有一个BIN文件,我通过网络发送并接收到(Application.persistentDataPath +" / Binaries /")路径,然后我检索BIN中的数据到纹理并将其保存为(Application.dataPath +" / textures / Weather /")的PNG文件。现在我有文件,所以我可以更改纹理最大尺寸。
List<Texture2D> SetupTextures(List<string> textureNames)
{
string fileName;
List<Texture2D> textures = new List<Texture2D>();
string[] subStrings = Regex.Split(texturesData, "Assets");
string assetDatabasePath = "Assets" + subStrings [1];
//print ("AAA: " + assetDatabasePath + textureNames[0]);
foreach(string name in textureNames)
{
fileName = assetDatabasePath + name;
#if UNITY_EDITOR
TextureImporter tImporter = AssetImporter.GetAtPath( fileName ) as TextureImporter;
if( tImporter != null ) {
tImporter.mipmapEnabled = true;
tImporter.isReadable = true;
tImporter.maxTextureSize = 256;
tImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
AssetDatabase.ImportAsset( fileName, ImportAssetOptions.ForceUpdate );
//print ("TextureImporter successfully settled.");
} else {
//print ("Faild to set TextureImporter!");
}
Texture2D texture =
(Texture2D) AssetDatabase.LoadAssetAtPath (fileName, typeof(Texture2D) );
if( texture != null ) {
//renderer.material.mainTexture = texture;
//renderer.material.SetTexture("_Texture2", texture);
textures.Add(texture);
//print ("Texture successfully added to list.");
} else {
//print ("Faild to add texture to list!");
}
#endif
}
return textures;
}
问题是AssetImporter.GetAtPath()在路径中没有看到新生成的PNG文件?但如果我重新聚焦到其他窗口,然后专注于统一,他们开始加载,AssetImporter.GetAtPath()获取所需的文件。
1)那么有没有改变焦点的方法呢?
2)或者我可以更改纹理最大尺寸而不将其保存到光盘吗?
答案 0 :(得分:0)
您需要做的是在将BIN-s转换为PNG-s之后但在致电SetupTextures
之前致电AssetDatabase.Refresh。也许你需要玩旗帜,但在我的应用程序中我使用了ImportAssetOptions.Default
并且没问题。
此外,我会在致电AssetDatabase.SaveAssets
后(或SetupTextures
结束时)致电SetupTextures
。只是为了确保Unity已将所有内容保存到磁盘。