public ApplyTextureType ( TextureImporterType type ) : void | ||
type | TextureImporterType | |
return | void |
/// <summary> /// 把纹理放到编辑器中去 /// </summary> /// <param name="ps"></param> private static Texture2D AssembleTexture(ParticleSystem ps, Dictionary <int, object> resource) { Texture2D tex2D = null; Texture3D tex = resource[ps.TexId] as Texture3D; if (tex.IsATF) { } else { //实例化一个Texture2D,宽和高设置可以是任意的,因为当使用LoadImage方法会对Texture2D的宽和高会做相应的调整 //Texture2D tex2D = new Texture2D(1,1); //tex2D.LoadImage(tex.Data); var fileName = string.Empty; //if (!File.Exists(SceneFileCopy.GetAbsoluteTextureDir() + tex.Name)) { fileName = tex.Name; ps.UnityResourceParam.Texture2DPath = SceneFileCopy.GetRelativeTextureDir(ps.RootFileName) + fileName; } //else //{ // fileName = tex.Name.Substring(0, UnityEngine.Mathf.Max(0, tex.Name.Length - 4)) + "_" + Guid.NewGuid().ToString() + tex.Name.Substring(tex.Name.Length - 4, 4); // ps.UnityResourceParam.Texture2DPath = SceneFileCopy.GetRelativeTextureDir() + fileName; //} ps.UnityResourceParam.Texture2DPath = SceneFileCopy.GetRelativeTextureDir(ps.RootFileName) + fileName; SaveFile(SceneFileCopy.GetAbsoluteTextureDir(ps.RootFileName) + fileName, tex.Data); tex2D = UnityEditor.AssetDatabase.LoadAssetAtPath(ps.UnityResourceParam.Texture2DPath, typeof(Texture2D)) as Texture2D; UnityEditor.TextureImporter textureImporter = UnityEditor.AssetImporter.GetAtPath(ps.UnityResourceParam.Texture2DPath) as UnityEditor.TextureImporter; UnityEditor.TextureImporterSettings settings = new UnityEditor.TextureImporterSettings(); textureImporter.ReadTextureSettings(settings); settings.ApplyTextureType(UnityEditor.TextureImporterType.Default); textureImporter.SetTextureSettings(settings); textureImporter.textureType = UnityEditor.TextureImporterType.Default; //使用透明度 textureImporter.alphaIsTransparency = true; textureImporter.isReadable = true; textureImporter.filterMode = (UnityEngine.FilterMode)tex.FilterMode; textureImporter.wrapMode = (UnityEngine.TextureWrapMode)tex.WrapMode; textureImporter.mipmapEnabled = tex.MipMode > 0; UnityEditor.AssetDatabase.ImportAsset(ps.UnityResourceParam.Texture2DPath); } UnityEditor.AssetDatabase.Refresh(); return(tex2D); }
private Sprite GetSpriteAtPath(string path, File file, ref bool success) { var importer = TextureImporter.GetAtPath (path) as TextureImporter; if (importer != null) { //If no TextureImporter exists, there's no texture to be found if ((importer.textureType != TextureImporterType.Sprite || importer.spritePivot.x != file.pivot_x || importer.spritePivot.y != file.pivot_y) && !InvalidImporters.Contains (importer)) { if (success) success = false; //If the texture type isn't Sprite, or the pivot isn't set properly, var settings = new TextureImporterSettings (); //set the texture type and pivot importer.ReadTextureSettings (settings); //and make success false so the process can abort settings.ApplyTextureType (TextureImporterType.Sprite, true); //after all the textures have been processed settings.spriteAlignment = (int)SpriteAlignment.Custom; settings.spritePivot = new Vector2 (file.pivot_x, file.pivot_y); importer.SetTextureSettings (settings); importer.SaveAndReimport (); InvalidImporters.Add (importer); } } else Debug.LogErrorFormat ("Error: No Sprite was found at {0}", path); return (Sprite)AssetDatabase.LoadAssetAtPath (path, typeof(Sprite)); }
private void ProgressCoroutine() { FileInfo file = allFiles[allFiles.Count - 1]; allFiles.RemoveAt(allFiles.Count - 1); ++progressCurrent; if (file == null) { return; } if (!file.Exists) { return; } if (file.FullName.Contains("Plugins")) { return; } //png图片格式幂指数判断 string fileExtension = file.Extension.ToLower(); if ((fileExtension == ".png" || fileExtension == ".jpg" || fileExtension == ".tga" || fileExtension == ".psd") && checkType == CheckType.TEXTURE) { string filename = Application.dataPath + "/put.png"; file.CopyTo(filename, true); try { AssetDatabase.Refresh(ImportAssetOptions.ForceUncompressedImport); UnityEngine.Object ob = AssetDatabase.LoadMainAssetAtPath(filename.Substring(filename.IndexOf("Assets"))); Texture2D tex = (Texture2D)ob; TextureImporter texImporter = (TextureImporter)TextureImporter.GetAtPath(filename.Substring(filename.IndexOf("Assets"))); TextureImporterSettings s = new TextureImporterSettings(); s.ApplyTextureType(TextureImporterType.Advanced, true); s.mipmapEnabled = false; s.maxTextureSize = 4096; s.readable = true; s.textureFormat = TextureImporterFormat.AutomaticTruecolor; texImporter.SetTextureSettings(s); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tex)); processTexture(tex, file); } catch (System.Exception exception) { Debug.LogError(exception.Message); Debug.LogError(file.FullName); } finally { System.IO.File.Delete(filename); } } //FBX mesh文件顶点和三角型面输出 if (fileExtension == ".fbx" && (checkType == CheckType.VERTEX_AND_FACE || checkType == CheckType.TANGENT || checkType == CheckType.BONE_AMOUNT)) { if (checkType == CheckType.VERTEX_AND_FACE) { string filename = Application.dataPath + "/put.FBX"; file.CopyTo(filename, true); AssetDatabase.Refresh(ImportAssetOptions.ForceUncompressedImport); UnityEngine.Object obmodel = AssetDatabase.LoadMainAssetAtPath(filename.Substring(filename.IndexOf("Assets"))); GameObject objFBX = (GameObject)obmodel; processModel(objFBX, file); System.IO.File.Delete(filename); } if (checkType == CheckType.TANGENT) { processModelTangent(file); } if (checkType == CheckType.BONE_AMOUNT) { processModelBone(file); } } if (fileExtension == ".prefab") { if (checkType == CheckType.BONE_QUALITY) { processBoneQuality(file); } } }
// ---------------------------------------------------------------------------- static void SelectedChangeTextureType(TextureImporterType newType) { Object[] textures = GetSelectedTextures(); Selection.objects = new Object[0]; foreach (Texture2D texture in textures) { string path = AssetDatabase.GetAssetPath(texture); //Debug.Log("path: " + path); TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; textureImporter.textureType = newType; AssetDatabase.ImportAsset(path); TextureImporterSettings tis = new TextureImporterSettings(); textureImporter.ReadTextureSettings(tis); tis.ApplyTextureType(newType, true); } }