//纹理导入之前调用,针对入到的纹理进行设置 public void OnPreprocessTexture0() { //string dirName = System.IO.Path.GetDirectoryName(assetPath); //string folderStr = System.IO.Path.GetFileName(dirName); Debug.Log("导入Texture图片前处理"); TextureImporter textureImporter = (TextureImporter)assetImporter; Debug.Log(textureImporter.assetPath); string FileName = System.IO.Path.GetFileName(assetPath); string[] FileNameArray = FileName.Split(new string[] { "_" }, System.StringSplitOptions.RemoveEmptyEntries); if (FileNameArray.Length >= 3) { Debug.Log("符合命名规范图片"); textureImporter.mipmapEnabled = false;//关闭mipmap //图片类型 textureImporter.textureType = TextureImporterType.Default; textureImporter.spriteImportMode = SpriteImportMode.Single; //图片PackingTag textureImporter.spritePackingTag = FileNameArray[0]; //图片尺寸 if (FileNameArray[1] == "bg") { Debug.Log("名称:" + textureImporter.GetDefaultPlatformTextureSettings().name); TextureImporterPlatformSettings TempTexture = new TextureImporterPlatformSettings(); TempTexture.overridden = true; TempTexture.name = "PC"; TempTexture.maxTextureSize = 512; TempTexture.format = TextureImporterFormat.ETC_RGB4; textureImporter.SetPlatformTextureSettings(TempTexture); } else if (FileNameArray[1] == "btn") { textureImporter.maxTextureSize = 256; } else if (FileNameArray[1] == "icon") { textureImporter.maxTextureSize = 64; } else { TextureImporterPlatformSettings TempTexture = new TextureImporterPlatformSettings(); TempTexture.overridden = true; TempTexture.name = "Standalone"; TempTexture.maxTextureSize = 2048; TempTexture.format = TextureImporterFormat.DXT5Crunched; // TempTexture.crunchedCompression = true; TempTexture.compressionQuality = 50; textureImporter.SetPlatformTextureSettings(TempTexture); } Debug.Log("名称:" + textureImporter.GetDefaultPlatformTextureSettings().name); } else { Debug.Log("不是指定文件"); } }
/// <summary> /// 打开新的纹理 /// </summary> /// <param name="texture">纹理</param> /// <param name="anchor">锚点</param> public void OpenTexture(Texture2D texture, Vector2 anchor) { Value = texture; Path = AssetDatabase.GetAssetPath(Value); FullPath = Application.dataPath + Path.Substring(6); Format = Path.GetFileFormat(); Anchor = anchor; Importer = AssetImporter.GetAtPath(Path) as TextureImporter; Settings = new TextureImporterSettings(); Importer.ReadTextureSettings(Settings); PlatformSettings = Importer.GetDefaultPlatformTextureSettings(); //开启纹理的可读、可写模式 Utility.SetReadableEnable(Importer); if (PaintValue != null) { UObject.DestroyImmediate(PaintValue); PaintValue = null; } PaintValue = UObject.Instantiate(Value); //关闭纹理的可读、可写模式 Utility.SetReadableDisabled(Importer); }
public static void _SetIOSAndAndroidRGB4(TextureImporter textureImporter, TextureCompressionQuality compressionQuality) { int cq = (int)compressionQuality; TextureImporterPlatformSettings platformTextureSettings; platformTextureSettings = textureImporter.GetPlatformTextureSettings(Android); platformTextureSettings.format = TextureImporterFormat.ETC_RGB4; platformTextureSettings.compressionQuality = cq; platformTextureSettings.overridden = true; textureImporter.SetPlatformTextureSettings(platformTextureSettings); platformTextureSettings = textureImporter.GetPlatformTextureSettings(iPhone); platformTextureSettings.format = TextureImporterFormat.PVRTC_RGB4; platformTextureSettings.compressionQuality = cq; platformTextureSettings.overridden = true; textureImporter.SetPlatformTextureSettings(platformTextureSettings); platformTextureSettings = textureImporter.GetDefaultPlatformTextureSettings(); platformTextureSettings.format = TextureImporterFormat.RGB24; platformTextureSettings.overridden = true; textureImporter.SetPlatformTextureSettings(platformTextureSettings); textureImporter.alphaSource = TextureImporterAlphaSource.None; textureImporter.mipmapEnabled = false; }
/// <summary> /// Sets the asset texture format. /// </summary> /// <returns><c>true</c>, if set format was set, <c>false</c> otherwise.</returns> /// <param name="path">Path.</param> /// <param name="format">Format.</param> public static bool AssetSetFormat(string path, TextureImporterFormat format) { if (string.IsNullOrEmpty(path)) { return(false); } TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter; if (ti == null) { return(false); } TextureImporterPlatformSettings tips = ti.GetDefaultPlatformTextureSettings(); tips.format = format; tips.overridden = true; ti.SetPlatformTextureSettings(tips); SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport); return(true); }
/// <summary> /// Sets the asset texture format. /// </summary> /// <returns><c>true</c>, if set format was set, <c>false</c> otherwise.</returns> /// <param name="path">Path.</param> /// <param name="format">Format.</param> public static bool AssetSetFormat(string path, TextureImporterFormat format) { if (string.IsNullOrEmpty(path)) { return(false); } TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter; if (ti == null) { return(false); } #if UNITY_5_5_OR_NEWER TextureImporterPlatformSettings platformSettings = ti.GetDefaultPlatformTextureSettings(); platformSettings.format = format; ti.SetPlatformTextureSettings(platformSettings); #else TextureImporterSettings settings = new TextureImporterSettings(); ti.ReadTextureSettings(settings); settings.textureFormat = format; ti.SetTextureSettings(settings); #endif SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport); return(true); }
public static Texture2D GetIconAtPath(string path) { if (s_icons.ContainsKey(path)) { return(s_icons[path]); } else { TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(path); importer.textureType = TextureImporterType.GUI; importer.alphaIsTransparency = true; importer.alphaSource = TextureImporterAlphaSource.FromInput; importer.npotScale = TextureImporterNPOTScale.None; importer.mipmapEnabled = false; importer.wrapMode = TextureWrapMode.Clamp; var settings = importer.GetDefaultPlatformTextureSettings(); settings.compressionQuality = 0; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); Texture2D tex = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D; s_icons.Add(path, tex); return(GetIconAtPath(path)); } }
void SetTextureSettings(Texture2D texture) { if (texture == null) { return; } string assetPath = AssetDatabase.GetAssetPath(texture); TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; if (textureImporter != null) { textureImporter.sRGBTexture = false; //textureImporter.isReadable = true; Only needed for VATPlayer that uses CPU to play animations textureImporter.mipmapEnabled = false; textureImporter.filterMode = FilterMode.Bilinear; textureImporter.wrapMode = TextureWrapMode.Clamp; textureImporter.npotScale = TextureImporterNPOTScale.None; textureImporter.textureCompression = TextureImporterCompression.Uncompressed; TextureImporterPlatformSettings textureImporterPlatformSettings = textureImporter.GetDefaultPlatformTextureSettings(); textureImporterPlatformSettings.format = TextureImporterFormat.RGBA32; textureImporter.SetPlatformTextureSettings(textureImporterPlatformSettings); AssetDatabase.ImportAsset(assetPath); AssetDatabase.Refresh(); } }
Texture2D SaveTexture(string path, Texture2D tex, bool overwrite = false) { if (overwrite || !System.IO.File.Exists(path)) { #if UNITY_2019_1_OR_NEWER var bytes = tex.EncodeToTGA(); path = path.Replace(".png", ".tga"); #else var bytes = tex.EncodeToPNG(); #endif System.IO.File.WriteAllBytes(path, bytes); AssetDatabase.Refresh(); AssetImporter ai = AssetImporter.GetAtPath(path); TextureImporter ti = ai as TextureImporter; ti.sRGBTexture = false; ti.textureCompression = TextureImporterCompression.Uncompressed; var ftm = ti.GetDefaultPlatformTextureSettings(); ftm.format = TextureImporterFormat.RGBA32; ti.SetPlatformTextureSettings(ftm); ti.mipmapEnabled = true; ti.isReadable = true; ti.filterMode = FilterMode.Bilinear; ti.wrapMode = TextureWrapMode.Clamp; ti.SaveAndReimport(); } return(AssetDatabase.LoadAssetAtPath <Texture2D>(path)); }
private void ReportTexture(StringBuilder sb, TextureImporter importer) { int w, h; var type = importer.textureType; int maxSize = importer.maxTextureSize; #if UNITY_5_5_OR_NEWER var defaultTextureSetting = importer.GetDefaultPlatformTextureSettings(); var format = defaultTextureSetting.format; if (!importer.GetPlatformTextureSettings(this.platform, out maxSize, out format)) { maxSize = importer.maxTextureSize; format = defaultTextureSetting.format; } #else var format = importer.textureFormat; if (!importer.GetPlatformTextureSettings(this.platform, out maxSize, out format)) { maxSize = importer.maxTextureSize; format = importer.textureFormat; } #endif var tex = GetTextureSize(importer, out w, out h) as Texture2D; sb.Append("{"); if (!AssetsReporterUtils.IsVisibleInWebBrowserImage(importer.assetPath) && tex != null) { string preview = AssetsReporterUtils.GetWebVisibleTexturePreview(importer, tex, true); AssetsReporterUtils.AddJsonObject(sb, "preview", preview).Append(","); } AssetsReporterUtils.AddJsonObject(sb, "path", importer.assetPath.ToString()).Append(","); AssetsReporterUtils.AddJsonObject(sb, "textureType", type.ToString()).Append(","); AssetsReporterUtils.AddJsonObject(sb, "isReadable", importer.isReadable).Append(","); AssetsReporterUtils.AddJsonObject(sb, "wrapMode", importer.wrapMode.ToString()).Append(","); AssetsReporterUtils.AddJsonObject(sb, "mipmapEnabled", importer.mipmapEnabled).Append(","); AssetsReporterUtils.AddJsonObject(sb, "width", w).Append(","); AssetsReporterUtils.AddJsonObject(sb, "height", h).Append(","); AssetsReporterUtils.AddJsonObject(sb, "maxSize", maxSize).Append(","); AssetsReporterUtils.AddJsonObject(sb, "textureFormat", format.ToString()).Append(","); if (string.IsNullOrEmpty(importer.spritePackingTag)) { AssetsReporterUtils.AddJsonObject(sb, "isPow2", IsPow2Size(w, h)).Append(","); } else { AssetsReporterUtils.AddJsonObject(sb, "isPow2", true).Append(","); } AssetsReporterUtils.AddJsonObject(sb, "spritePackingTag", importer.spritePackingTag); sb.Append("}"); if (tex != null) { Resources.UnloadAsset(tex); tex = null; } AssetsReporterUtils.AddCountDictionary(this.spriteTagSet, importer.spritePackingTag); AssetsReporterUtils.AddCountDictionary(this.textureFormatSet, format); AssetsReporterUtils.AddCountDictionary(this.textureTypeSet, type); }
void ConvertTextureToFormat(Texture2D texture, TextureImporterFormat format) { string assetPath = AssetDatabase.GetAssetPath(texture); if (assetPath != "") { // make sure the source texture is npot and readable, and uncompressed TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(assetPath); #if UNITY_5_5_OR_NEWER var defaultTextureSettings = importer.GetDefaultPlatformTextureSettings(); if (defaultTextureSettings.format != format) { defaultTextureSettings.format = format; importer.SetPlatformTextureSettings(defaultTextureSettings); } #else if (importer.textureFormat != format) { importer.textureFormat = format; } #endif AssetDatabase.ImportAsset(assetPath); } }
/// <summary> /// 读取原纹理设置 /// </summary> private void ReadSettings() { Importer = AssetImporter.GetAtPath(Path) as TextureImporter; Settings = new TextureImporterSettings(); Importer.ReadTextureSettings(Settings); PlatformSettings = Importer.GetDefaultPlatformTextureSettings(); }
static void LoadAsset() { Texture2D t = AssetDatabase.LoadAssetAtPath("Assets/circle.png", typeof(Texture2D)) as Texture2D;//导入设置配种 string path = AssetDatabase.GetAssetPath(t); TextureImporter import = TextureImporter.GetAtPath(path) as TextureImporter; import.textureType = TextureImporterType.Sprite; import.spritePackingTag = "importAsset"; TextureImporterPlatformSettings pset = import.GetDefaultPlatformTextureSettings(); pset.overridden = true; pset.maxTextureSize = 2048; pset.textureCompression = TextureImporterCompression.CompressedHQ; pset.format = TextureImporterFormat.RGB16; import.SetPlatformTextureSettings(pset); TextureImporterPlatformSettings _pset = import.GetPlatformTextureSettings("PC"); _pset.maxTextureSize = 4096; _pset.textureCompression = TextureImporterCompression.CompressedHQ; import.SetPlatformTextureSettings(_pset); AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);//重新导入 }
public static bool ConfigureSpriteTextureImporter(string assetPath) { // make sure the source texture is npot and readable, and uncompressed TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(assetPath); if (importer.mipmapEnabled || #if UNITY_5_5_OR_NEWER importer.textureType != TextureImporterType.Default || importer.textureCompression != TextureImporterCompression.Uncompressed || importer.GetDefaultPlatformTextureSettings().format != TextureImporterFormat.RGBA32 || #else importer.textureType != TextureImporterType.Advanced || importer.textureFormat != TextureImporterFormat.AutomaticTruecolor || #endif importer.npotScale != TextureImporterNPOTScale.None || !importer.isReadable || // !importer.alphaIsTransparency || importer.maxTextureSize < 4096 || importer.wrapMode != TextureWrapMode.Clamp ) { #if UNITY_5_5_OR_NEWER importer.textureType = TextureImporterType.Default; importer.textureCompression = TextureImporterCompression.Uncompressed; var defaultPlatform = importer.GetDefaultPlatformTextureSettings(); defaultPlatform.format = TextureImporterFormat.RGBA32; importer.SetPlatformTextureSettings(defaultPlatform); #else importer.textureType = TextureImporterType.Advanced; importer.textureFormat = TextureImporterFormat.AutomaticTruecolor; #endif importer.npotScale = TextureImporterNPOTScale.None; importer.wrapMode = TextureWrapMode.Clamp; importer.isReadable = true; importer.mipmapEnabled = false; importer.maxTextureSize = 4096; // importer.alphaIsTransparency = true; AssetDatabase.ImportAsset(assetPath); return(true); } return(false); }
void SaveSingleChunkHeightAndNormalMap(int countX, int countZ, int pixelCountX, int pixelCountZ, int sumCount, string path) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string heightNormalName = mapName + "_Chunk_" + sumCount + "_HeightNormalMap.png"; Texture2D heightNormalChunkMap = new Texture2D(pixelCountX + 1, pixelCountZ + 1, TextureFormat.RGBA32, true); Vector2 RG; for (int j = countZ * pixelCountZ; j <= (countZ + 1) * pixelCountZ; j++) { for (int i = countX * pixelCountX; i <= (countX + 1) * pixelCountX; i++) { if (i >= heightmapWidth || j >= heightmapHeight) { int mini = Mathf.Min(i, heightmapWidth - 1); int minj = Mathf.Min(j, heightmapHeight - 1); RG = EncodeHeight(heights[mini, minj]); heightNormalChunkMap.SetPixel(i - countX * pixelCountX, j - countZ * pixelCountZ, new Color(RG.x, RG.y, normals[mini, minj].x, normals[mini, minj].z)); } else { RG = EncodeHeight(heights[i, j]); heightNormalChunkMap.SetPixel(i - countX * pixelCountX, j - countZ * pixelCountZ, new Color(RG.x, RG.y, normals[i, j].x, normals[i, j].z)); } } } heightNormalChunkMap.Apply(); byte[] rawData = heightNormalChunkMap.EncodeToPNG(); File.WriteAllBytes(path + "/" + heightNormalName, rawData); AssetDatabase.Refresh(); //设置图片导入信息 TextureImporter heightNormalTexImporter = AssetImporter.GetAtPath(path.Replace(Application.dataPath, "Assets") + "/" + heightNormalName) as TextureImporter; heightNormalTexImporter.wrapMode = TextureWrapMode.Clamp; heightNormalTexImporter.npotScale = TextureImporterNPOTScale.None; TextureImporterPlatformSettings heightNormalTexSetting = heightNormalTexImporter.GetDefaultPlatformTextureSettings(); heightNormalTexSetting.format = TextureImporterFormat.RGBA32; heightNormalTexSetting.resizeAlgorithm = TextureResizeAlgorithm.Mitchell; heightNormalTexImporter.SetPlatformTextureSettings(heightNormalTexSetting); heightNormalTexImporter.SaveAndReimport(); //保存材质 //if(isSaveGPUMesh) //{ // string materialPath = path.Replace(Application.dataPath, "Assets"); // SaveTerrainMaterial(materialPath, mapName + "_Chunk_" + sumCount, heightNormalChunkMap); //} }
void OnGUI() { GUILayout.Space(10); EditorGUILayout.BeginHorizontal(); bool pathCorrect = !string.IsNullOrEmpty(_path) && Directory.Exists(_path); if (!pathCorrect) { _path = GetDefaultSavingPath(); } if (GUILayout.Button("Change", GUILayout.Width(150))) { _path = EditorUtility.OpenFolderPanel("Select folder:", _path, ""); PlayerPrefs.SetString(PathKey, _path); } EditorGUILayout.LabelField($"Saving directory: {_path}"); EditorGUILayout.EndHorizontal(); GUILayout.Space(10); GUILayout.Label("Golly string:", EditorStyles.boldLabel); ScrollPos = EditorGUILayout.BeginScrollView(ScrollPos, GUILayout.Height(150)); _gollyString = EditorGUILayout.TextArea(_gollyString, GUILayout.ExpandHeight(true)); EditorGUILayout.EndScrollView(); GUILayout.Space(10); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("WorldSize:", GUILayout.Width(150)); _worldSize.x = EditorGUILayout.IntField(_worldSize.x, GUILayout.Width(150)); _worldSize.y = EditorGUILayout.IntField(_worldSize.y, GUILayout.Width(150)); GUILayout.FlexibleSpace(); EditorGUILayout.LabelField("Pivot:", GUILayout.Width(150)); _pivot.x = EditorGUILayout.FloatField(_pivot.x, GUILayout.Width(150)); _pivot.y = EditorGUILayout.FloatField(_pivot.y, GUILayout.Width(150)); EditorGUILayout.EndHorizontal(); GUILayout.Space(10); _pngName = EditorGUILayout.TextField("TextureName:", _pngName); if (GUILayout.Button("Bake", GUILayout.Width(140))) { var sizeInDemandedAreas = (int2)math.ceil(_worldSize / new float2(16, 3)); _worldSize = sizeInDemandedAreas * new int2(16, 3); var texture = GenerateTextureByGollyString(_worldSize, _gollyString); byte[] _bytes = texture.EncodeToPNG(); var fullPath = $"{_path}/{_pngName}.png"; System.IO.File.WriteAllBytes(fullPath, _bytes); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); var shortPath = fullPath.Substring(fullPath.IndexOf("Assets/")); TextureImporter importer = AssetImporter.GetAtPath(shortPath) as TextureImporter; if (importer != null) { importer.textureType = TextureImporterType.Default; importer.isReadable = true; importer.mipmapEnabled = false; var settings = importer.GetDefaultPlatformTextureSettings(); settings.format = TextureImporterFormat.RGBA32; settings.textureCompression = TextureImporterCompression.Uncompressed; importer.SetPlatformTextureSettings(settings); AssetDatabase.ImportAsset(shortPath, ImportAssetOptions.ForceUpdate); } } }
void OnPreprocessTexture() { Object asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D)); if (asset) { return; // set defaults for new textures only } TextureImporter importer = assetImporter as TextureImporter; importer.textureType = TextureImporterType.Sprite; importer.spritePackingTag = "tile"; importer.spritePixelsPerUnit = 1; importer.mipmapEnabled = false; importer.filterMode = FilterMode.Point; importer.textureCompression = TextureImporterCompression.Uncompressed; TextureImporterPlatformSettings defaultSettings = importer.GetDefaultPlatformTextureSettings(); defaultSettings.maxTextureSize = 2048; defaultSettings.compressionQuality = 0; defaultSettings.overridden = true; defaultSettings.crunchedCompression = false; defaultSettings.format = TextureImporterFormat.RGBA32; importer.SetPlatformTextureSettings(defaultSettings); // setting pivot based on texture size object[] args = new object[2] { 0, 0 }; MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke(importer, args); // set to center of top left corner tile // float step = 8; // int width = (int)args[0]; // int height = (int)args[1]; TextureImporterSettings settings = new TextureImporterSettings(); importer.ReadTextureSettings(settings); settings.spriteAlignment = (int)SpriteAlignment.Center; settings.spriteMode = 1; settings.alphaIsTransparency = true; importer.SetTextureSettings(settings); // importer.spritePivot = new Vector2((1.0f / width) * step, 1.0f - ((1.0f / height) * step)); // importer.spritePivot = new Vector2((1.0f / width) * step, 0.0f); // importer.spritePivot = new Vector2(0.25f, 0.25f); // Debug.Log(args[0]+" "+args[1]); /* */ }
private void OverwriteImportSettings(TextureImporter target, TextureImporter reference, string tagName) { target.textureType = reference.textureType; var targetSetting = new TextureImporterSettings(); var referenceSetting = new TextureImporterSettings(); target.ReadTextureSettings(targetSetting); reference.ReadTextureSettings(referenceSetting); //以下都是被锁定的图片设置 targetSetting.aniso = referenceSetting.aniso; targetSetting.compressionQuality = referenceSetting.compressionQuality; targetSetting.spriteGenerateFallbackPhysicsShape = referenceSetting.spriteGenerateFallbackPhysicsShape; targetSetting.spritePixelsPerUnit = referenceSetting.spritePixelsPerUnit; targetSetting.npotScale = referenceSetting.npotScale; targetSetting.readable = referenceSetting.readable; targetSetting.streamingMipmapsPriority = referenceSetting.streamingMipmapsPriority; targetSetting.mipmapEnabled = referenceSetting.mipmapEnabled; target.SetTextureSettings(targetSetting); target.androidETC2FallbackOverride = reference.androidETC2FallbackOverride; target.anisoLevel = reference.anisoLevel; target.compressionQuality = reference.compressionQuality; target.crunchedCompression = reference.crunchedCompression; target.isReadable = reference.isReadable; target.npotScale = reference.npotScale; target.streamingMipmapsPriority = reference.streamingMipmapsPriority; target.streamingMipmaps = reference.streamingMipmaps; target.textureCompression = reference.textureCompression; target.mipmapEnabled = reference.mipmapEnabled; if (m_overwritePackingTag) { if (!string.IsNullOrEmpty(tagName)) { target.spritePackingTag = tagName; } else { target.spritePackingTag = reference.spritePackingTag; } } var defaultPlatformSetting = reference.GetDefaultPlatformTextureSettings(); target.SetPlatformTextureSettings(defaultPlatformSetting); foreach (var g in NodeGUIUtility.SupportedBuildTargetGroups) { var platformName = BuildTargetUtility.TargetToAssetBundlePlatformName(g, BuildTargetUtility.PlatformNameType.TextureImporter); var impSet = reference.GetPlatformTextureSettings(platformName); target.SetPlatformTextureSettings(impSet); } }
static Dictionary <string, object> GetTextureImporterValues(TextureImporter importer) { var dict = EditorCommonUtils.GetValues(importer); EditorCommonUtils.GetValues(importer.GetDefaultPlatformTextureSettings(), dict, "Default"); EditorCommonUtils.GetValues(importer.GetPlatformTextureSettings("Standalone"), dict, "Standalone"); EditorCommonUtils.GetValues(importer.GetPlatformTextureSettings("Android"), dict, "Android"); EditorCommonUtils.GetValues(importer.GetPlatformTextureSettings("iPhone"), dict, "iPhone"); return(dict); }
public void AddTexture(string stringPath, TextureImporter ti) { var tiAnd = ti.GetPlatformTextureSettings("Android"); var tiIos = ti.GetPlatformTextureSettings("iPhone"); foreach (var group in groups) { if (group.android.overridden == tiAnd.overridden && group.iphone.overridden == tiIos.overridden) { if (tiAnd.overridden && tiIos.overridden) { if (@group.android.textureCompression != tiAnd.textureCompression || @group.android.format != tiAnd.format || @group.iphone.textureCompression != tiIos.textureCompression || @group.iphone.format != tiIos.format) { continue; } } else if (tiAnd.overridden) { if (@group.android.textureCompression != tiAnd.textureCompression || @group.android.format != tiAnd.format) { continue; } } else if (tiIos.overridden) { if (@group.iphone.textureCompression != tiIos.textureCompression || @group.iphone.format != tiIos.format) { continue; } } else if (@group.normal.textureCompression != ti.textureCompression || group.hasAlpha != ti.DoesSourceTextureHaveAlpha()) { continue; } group.textures.Add(stringPath); return; } } var newAtlas = new Atlas(ti.DoesSourceTextureHaveAlpha()); newAtlas.textures.Add(stringPath); newAtlas.iphone = tiIos; newAtlas.android = tiAnd; newAtlas.normal = ti.GetDefaultPlatformTextureSettings(); groups.Add(newAtlas); }
bool VerifySnow() { #if __MICROSPLAT_SNOW__ for (int i = 0; i < rawTerrains.Count; ++i) { Terrain t = rawTerrains [i]; MicroSplatTerrain mst = t.GetComponent <MicroSplatTerrain> (); if (mst != null) { var tex = mst.snowMaskOverride; if (tex != null) { AssetImporter ai = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(tex)); TextureImporter ti = ai as TextureImporter; if (ti == null || !ti.isReadable) { EditorGUILayout.HelpBox("Snow Mask texture is not read/write", MessageType.Error); if (GUILayout.Button("Fix it!")) { ti.isReadable = true; ti.SaveAndReimport(); } return(false); } bool isLinear = ti.sRGBTexture == false; bool isRGB32 = ti.textureCompression == TextureImporterCompression.Uncompressed && ti.GetDefaultPlatformTextureSettings().format == TextureImporterFormat.RGBA32; if (isRGB32 == false || isLinear == false || ti.wrapMode == TextureWrapMode.Repeat) { EditorGUILayout.HelpBox("Snow Mask is not in the correct format (Uncompressed, linear, clamp, RGBA32)", MessageType.Error); if (GUILayout.Button("Fix it!")) { ti.sRGBTexture = false; ti.textureCompression = TextureImporterCompression.Uncompressed; var ftm = ti.GetDefaultPlatformTextureSettings(); ftm.format = TextureImporterFormat.RGBA32; ti.SetPlatformTextureSettings(ftm); ti.mipmapEnabled = true; ti.wrapMode = TextureWrapMode.Clamp; ti.SaveAndReimport(); } return(false); } } } } #endif return(true); }
public TextureImporterPlatformSettings GetPlatformSettings(TextureImporter importer) { var platformSettings = new TextureImporterPlatformSettings(); importer.GetDefaultPlatformTextureSettings().CopyTo(platformSettings); platformSettings.overridden = true; platformSettings.name = Name; if (Size > 0) { platformSettings.maxTextureSize = Size; } platformSettings.format = Format; return(platformSettings); }
void OnPreprocessTexture() { TextureImporter textureImporter = (TextureImporter)assetImporter; if (textureImporter.assetPath.StartsWith(terrainSpritesPath)) { textureImporter.textureType = TextureImporterType.Sprite; textureImporter.filterMode = FilterMode.Point; TextureImporterPlatformSettings textureSettings = textureImporter.GetDefaultPlatformTextureSettings(); textureSettings.format = TextureImporterFormat.RGBA32; textureSettings.textureCompression = TextureImporterCompression.Uncompressed; textureImporter.SetPlatformTextureSettings(textureSettings); } }
void OnPreprocessTexture() { //First checks if the asset is going into any folder labeled "Textures", otherwise it will not run the rest of the code at all. if (assetPath.Contains("Textures")) { //Settings are obtained from the Texture Configuration class to allow for easy access to the already selected choices. //Texture Importer must save and Reimport for the changes to take effect, otherwise the changes will not apply to the file. TextureImporter textureImporter = (TextureImporter)assetImporter; TextureImporterPlatformSettings texturesettings = textureImporter.GetDefaultPlatformTextureSettings(); texturesettings.maxTextureSize = TextureConfigurationFile.textureMaxSizeSetting; texturesettings.name = TextureConfigurationFile.platformOverrideSetting; texturesettings.overridden = true; textureImporter.anisoLevel = TextureConfigurationFile.textureAnisoLevelSetting; textureImporter.SetPlatformTextureSettings(texturesettings); textureImporter.SaveAndReimport(); } }
public void WriteToFile(string fullPathToFile, bool allowOverwriting) { if (File.Exists(fullPathToFile) && !allowOverwriting) { throw new System.AccessViolationException("Tried to write PaletteGroup but file already exists. " + "\nFile Path: " + fullPathToFile); } Texture2D paletteGroupAsTexture = CreateAsTexture(); try { byte[] outTextureData = paletteGroupAsTexture.EncodeToPNG(); File.WriteAllBytes(fullPathToFile, outTextureData); } catch (System.Exception e) { throw new System.IO.IOException("Encountered IO exception during PaletteGroup write: " + e.Message); } // Force refresh so that we can set its Import settings immediately AssetDatabase.ImportAsset(fullPathToFile); // Assign correct settings to the file TextureImporter textureImporter = AssetImporter.GetAtPath(fullPathToFile) as TextureImporter; if (textureImporter == null) { throw new System.NullReferenceException("Failed to import file at specified path: " + fullPathToFile); } var platformSettings = textureImporter.GetDefaultPlatformTextureSettings(); platformSettings.resizeAlgorithm = TextureResizeAlgorithm.Bilinear; platformSettings.format = TextureImporterFormat.RGBA32; textureImporter.SetPlatformTextureSettings(platformSettings); textureImporter.textureType = TextureImporterType.Default; textureImporter.spriteImportMode = SpriteImportMode.None; textureImporter.filterMode = FilterMode.Point; //textureImporter.textureFormat = TextureImporterFormat.RGBA32; textureImporter.alphaIsTransparency = false; textureImporter.mipmapEnabled = false; textureImporter.npotScale = TextureImporterNPOTScale.None; textureImporter.maxTextureSize = 256; // Force Unity to see the file and use the new import settings AssetDatabase.ImportAsset(fullPathToFile, ImportAssetOptions.ForceUpdate); }
private static void _SetTrueColorFormat(TextureImporter textureImporter) { textureImporter.ClearPlatformTextureSettings(Android); textureImporter.ClearPlatformTextureSettings(iPhone); TextureImporterPlatformSettings platformTextureSettings; platformTextureSettings = textureImporter.GetDefaultPlatformTextureSettings(); platformTextureSettings.format = TextureImporterFormat.RGBA32; platformTextureSettings.overridden = true; textureImporter.SetPlatformTextureSettings(platformTextureSettings); textureImporter.alphaSource = TextureImporterAlphaSource.FromInput; textureImporter.maxTextureSize = 2048; textureImporter.generateCubemap = TextureImporterGenerateCubemap.None; textureImporter.npotScale = TextureImporterNPOTScale.None; textureImporter.mipmapEnabled = false; }
static Texture2D SaveSpriteToEditorPath(Texture2D sp, string path) { string dir = Path.GetDirectoryName(path); Directory.CreateDirectory(dir); File.WriteAllBytes(path, sp.EncodeToPNG()); AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter; importer.textureType = TextureImporterType.Default; importer.textureShape = TextureImporterShape.Texture2D; importer.alphaIsTransparency = true; importer.npotScale = TextureImporterNPOTScale.None; importer.isReadable = false; importer.mipmapEnabled = false; var settingsDefault = importer.GetDefaultPlatformTextureSettings(); settingsDefault.textureCompression = TextureImporterCompression.Uncompressed; settingsDefault.maxTextureSize = 2048; settingsDefault.format = TextureImporterFormat.RGBA32; var settingsAndroid = importer.GetPlatformTextureSettings("Android"); settingsAndroid.overridden = true; settingsAndroid.maxTextureSize = settingsDefault.maxTextureSize; settingsAndroid.format = TextureImporterFormat.ASTC_RGBA_8x8; importer.SetPlatformTextureSettings(settingsAndroid); var settingsiOS = importer.GetPlatformTextureSettings("iPhone"); settingsiOS.overridden = true; settingsiOS.maxTextureSize = settingsDefault.maxTextureSize; settingsiOS.format = TextureImporterFormat.ASTC_RGBA_8x8; importer.SetPlatformTextureSettings(settingsiOS); EditorUtility.SetDirty(importer); importer.SaveAndReimport(); return(AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D); }
// Apply settings Method which searches and adds all texture files in an array, and for each file, adds the settings, overrides it for the selected platform // and imports the newly applied asset again, therefore applying all the settings properly. static void ApplySettings(int maxSize, int anisoLevel, bool alphaSplitting, TextureImporterCompression compressionType, string platform) { Object[] textures = GetSelectedTextures(); Selection.objects = new Object[0]; foreach (Texture texture in textures) { string path = AssetDatabase.GetAssetPath(texture); TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; TextureImporterPlatformSettings texturesettings = textureImporter.GetDefaultPlatformTextureSettings(); texturesettings.maxTextureSize = maxSize; texturesettings.name = platform; texturesettings.overridden = true; texturesettings.textureCompression = compressionType; texturesettings.allowsAlphaSplitting = alphaSplitting; textureImporter.anisoLevel = anisoLevel; textureImporter.SetPlatformTextureSettings(texturesettings); AssetDatabase.ImportAsset(path); } }
private void CreatAndSetBlendTex(Material mt) { string blendTexFolder = "Assets/BrushTerrain/BlendTex/"; Texture2D blendTexAs = new Texture2D(256, 256, TextureFormat.ARGB32, true); Color[] col = new Color[256 * 256]; for (int i = 0; i < col.Length; i++) { col[i] = new Color(0, 0, 0, 0); } blendTexAs.SetPixels(col); string path = blendTexFolder + blendTexAs.GetHashCode() + ".png"; byte[] bytes = blendTexAs.EncodeToPNG(); File.WriteAllBytes(path, bytes); AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); //==AssetDatabase.Refresh(); //AssetDatabase.Refresh(); TextureImporter imt = TextureImporter.GetAtPath(path) as TextureImporter; imt.isReadable = true; #if UNITY_5_5_OR_NEWER TextureImporterPlatformSettings tips = imt.GetDefaultPlatformTextureSettings(); tips.textureCompression = TextureImporterCompression.Uncompressed; tips.format = TextureImporterFormat.RGBA32; tips.overridden = true; imt.SetPlatformTextureSettings(tips); #else imt.textureFormat = TextureImporterFormat.RGBA32; #endif imt.wrapMode = TextureWrapMode.Clamp; imt.anisoLevel = 9; imt.mipmapEnabled = false; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); Texture2D blendTex = AssetDatabase.LoadAssetAtPath <Texture2D>(path); mt.SetTexture("_BlendTex", blendTex); }
void OnPostprocessTexture(Texture2D texture) { string path = "Assets/Art/Picture"; int pos = path.LastIndexOf("Assets"); if (pos != -1) { path = path.Substring(pos); } if (assetPath.Contains(path)) { string spritrName = ""; spritrName = assetPath.Replace("\\", "/"); int _position = spritrName.LastIndexOf(@"/"); spritrName = spritrName.Substring(0, _position); spritrName = spritrName.Replace("/", "_"); spritrName.ToUpper(); TextureImporter textureImporter = assetImporter as TextureImporter; if (textureImporter.userData != GetVersion().ToString()) { textureImporter.textureType = TextureImporterType.Sprite; textureImporter.spriteImportMode = UnityEditor.SpriteImportMode.Single; textureImporter.spritePackingTag = spritrName; textureImporter.mipmapEnabled = false; textureImporter.spritePixelsPerUnit = 100; textureImporter.userData = GetVersion().ToString(); textureImporter.alphaIsTransparency = textureImporter.DoesSourceTextureHaveAlpha(); TextureImporterPlatformSettings setting = textureImporter.GetDefaultPlatformTextureSettings(); setting.maxTextureSize = 1024; textureImporter.SetPlatformTextureSettings(setting); textureImporter.SaveAndReimport(); Debug.LogWarningFormat("[{0}]-->auto fix sprite and Named: {1}", assetPath, spritrName); } else { textureImporter.spritePackingTag = null; } EditorUtility.SetDirty(textureImporter); } }
private void ProcessTexture(Texture m) { try { AssetDatabase.StartAssetEditing(); string assetPath = AssetDatabase.GetAssetPath(m); TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(assetPath); TextureImporterSettings settings = new TextureImporterSettings(); importer.ReadTextureSettings(settings); TextureImporterPlatformSettings platformSettings = importer.GetDefaultPlatformTextureSettings(); platformSettings.maxTextureSize = TextureSize; importer.SetPlatformTextureSettings(platformSettings); importer.SaveAndReimport(); } finally { AssetDatabase.StopAssetEditing(); AssetDatabase.SaveAssets(); } }