231 lines
8.6 KiB
C#
231 lines
8.6 KiB
C#
![]() |
// ***********************************************************************
|
|||
|
// Company : Kunpo
|
|||
|
// Author : KimCh
|
|||
|
// Created :
|
|||
|
//
|
|||
|
// Last Modified By : KimCh
|
|||
|
// Last Modified On :
|
|||
|
// ***********************************************************************
|
|||
|
namespace G
|
|||
|
{
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using UnityEditor;
|
|||
|
using UnityEditor.U2D;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.U2D;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// UI图片导入解析类
|
|||
|
/// 在指定文件下,创建文件夹,会自动生成与新创建文件夹同名图集
|
|||
|
/// 在指定文件夹下导入图片资源时,对其进行解析。符合条件,自动添加进与文件夹同名的图集;不符合条件,不允许导入
|
|||
|
/// </summary>
|
|||
|
public class TexturePostprocessor : AssetPostprocessor
|
|||
|
{
|
|||
|
#region Method
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
void OnPreprocessAsset()
|
|||
|
{
|
|||
|
var ext = Path.GetExtension(assetPath);
|
|||
|
|
|||
|
if (ext == ".spriteatlas")
|
|||
|
{
|
|||
|
var spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(assetPath);
|
|||
|
if (!spriteAtlas)
|
|||
|
return;
|
|||
|
var settings = spriteAtlas.GetPlatformSettings("DefaultTexturePlatform");
|
|||
|
if (settings == null)
|
|||
|
return;
|
|||
|
settings.name = "iPhone";
|
|||
|
SetOverriddenFormat(settings, settings.textureCompression, true);
|
|||
|
spriteAtlas.SetPlatformSettings(settings);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 图片导入
|
|||
|
/// </summary>
|
|||
|
void OnPreprocessTexture()
|
|||
|
{
|
|||
|
var ti = assetImporter as TextureImporter;
|
|||
|
|
|||
|
var dir = Path.GetDirectoryName(assetPath);
|
|||
|
if (dir.StartsWith(@"Assets\Arts\Textures\Weapon"))
|
|||
|
{
|
|||
|
SetSpriteFormat(ti, 1024);
|
|||
|
}
|
|||
|
else if (dir.StartsWith(@"Assets\Arts\Models\Weapon"))
|
|||
|
{
|
|||
|
SetTextureFormat(ti, 512);
|
|||
|
}
|
|||
|
else if (dir.StartsWith(@"Assets\Arts\Spines"))
|
|||
|
{
|
|||
|
SetTextureFormat2(ti, 1024);
|
|||
|
}
|
|||
|
else if (dir.StartsWith(@"Assets\Arts\Textures\Chapter"))
|
|||
|
{
|
|||
|
SetAtlasSpriteFormat(ti, 0);
|
|||
|
}
|
|||
|
else if (dir.Contains("UI"))
|
|||
|
{
|
|||
|
SetAtlasSpriteFormat(ti, 0);
|
|||
|
}
|
|||
|
else if (dir.Contains("VFX"))
|
|||
|
{
|
|||
|
var folder = Path.GetFileName(dir);
|
|||
|
var xIndex = folder.LastIndexOf('x');
|
|||
|
if (xIndex != -1 && int.TryParse(folder.Substring(xIndex + 1), out int size))
|
|||
|
{
|
|||
|
ti.maxTextureSize = Mathf.Min(size, ti.maxTextureSize);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ti.maxTextureSize = 256;
|
|||
|
}
|
|||
|
//ti.npotScale = TextureImporterNPOTScale.None;
|
|||
|
ti.mipmapEnabled = false;
|
|||
|
ti.isReadable = false;
|
|||
|
ti.textureCompression = TextureImporterCompression.Compressed;
|
|||
|
}
|
|||
|
|
|||
|
SetCompression(ti);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="ti"></param>
|
|||
|
/// <param name="maxSize"></param>
|
|||
|
static void SetSpriteFormat(TextureImporter ti, int maxSize, bool fullRect = false)
|
|||
|
{
|
|||
|
if (maxSize > 0 && ti.maxTextureSize > maxSize)
|
|||
|
ti.maxTextureSize = maxSize;
|
|||
|
ti.mipmapEnabled = false;
|
|||
|
//ti.isReadable = false;
|
|||
|
ti.npotScale = TextureImporterNPOTScale.None;
|
|||
|
|
|||
|
if (ti.textureType != TextureImporterType.Sprite)
|
|||
|
{
|
|||
|
ti.textureType = TextureImporterType.Sprite;
|
|||
|
ti.textureCompression = TextureImporterCompression.Compressed;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
//设置sprite 九宫格图片的MashType
|
|||
|
if (fullRect)
|
|||
|
{
|
|||
|
var textureSettings = new TextureImporterSettings();
|
|||
|
ti.ReadTextureSettings(textureSettings);
|
|||
|
if (textureSettings.spriteMeshType != SpriteMeshType.FullRect)
|
|||
|
{
|
|||
|
textureSettings.spriteMeshType = SpriteMeshType.FullRect;
|
|||
|
ti.SetTextureSettings(textureSettings);
|
|||
|
ti.SaveAndReimport();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
static void SetAtlasSpriteFormat(TextureImporter ti, int maxSize, bool fullRect = false)
|
|||
|
{
|
|||
|
if (maxSize > 0 && ti.maxTextureSize > maxSize)
|
|||
|
ti.maxTextureSize = maxSize;
|
|||
|
ti.mipmapEnabled = false;
|
|||
|
//ti.isReadable = false;
|
|||
|
ti.npotScale = TextureImporterNPOTScale.None;
|
|||
|
|
|||
|
if (ti.textureType != TextureImporterType.Sprite)
|
|||
|
{
|
|||
|
ti.textureType = TextureImporterType.Sprite;
|
|||
|
ti.textureCompression = TextureImporterCompression.Uncompressed;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ti.textureCompression = TextureImporterCompression.Uncompressed;
|
|||
|
//设置sprite 九宫格图片的MashType
|
|||
|
if (fullRect)
|
|||
|
{
|
|||
|
var textureSettings = new TextureImporterSettings();
|
|||
|
ti.ReadTextureSettings(textureSettings);
|
|||
|
if (textureSettings.spriteMeshType != SpriteMeshType.FullRect)
|
|||
|
{
|
|||
|
textureSettings.spriteMeshType = SpriteMeshType.FullRect;
|
|||
|
ti.SetTextureSettings(textureSettings);
|
|||
|
ti.SaveAndReimport();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
static void SetTextureFormat(TextureImporter ti, int maxSize)
|
|||
|
{
|
|||
|
ti.maxTextureSize = maxSize;
|
|||
|
ti.mipmapEnabled = false;
|
|||
|
ti.isReadable = false;
|
|||
|
//ti.npotScale = TextureImporterNPOTScale.ToNearest;
|
|||
|
|
|||
|
ti.textureType = TextureImporterType.Default;
|
|||
|
ti.textureCompression = TextureImporterCompression.Compressed;
|
|||
|
}
|
|||
|
|
|||
|
static void SetTextureFormat2(TextureImporter ti, int maxSize)
|
|||
|
{
|
|||
|
ti.maxTextureSize = maxSize;
|
|||
|
ti.mipmapEnabled = false;
|
|||
|
ti.isReadable = false;
|
|||
|
ti.npotScale = TextureImporterNPOTScale.ToNearest;
|
|||
|
|
|||
|
ti.textureType = TextureImporterType.Default;
|
|||
|
ti.textureCompression = TextureImporterCompression.CompressedHQ;
|
|||
|
}
|
|||
|
|
|||
|
static void SetCompression(TextureImporter importer)
|
|||
|
{
|
|||
|
var defaultTS = importer.GetDefaultPlatformTextureSettings();
|
|||
|
defaultTS.name = "iPhone";
|
|||
|
//var iOSTS = importer.GetPlatformTextureSettings("iOS");
|
|||
|
//defaultTS.CopyTo(iOSTS);
|
|||
|
|
|||
|
SetOverriddenFormat(defaultTS, defaultTS.textureCompression, importer.alphaSource != TextureImporterAlphaSource.None);
|
|||
|
|
|||
|
importer.SetPlatformTextureSettings(defaultTS);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="settings"></param>
|
|||
|
/// <param name="alpha"></param>
|
|||
|
static void SetOverriddenFormat(TextureImporterPlatformSettings settings, TextureImporterCompression compression, bool alpha)
|
|||
|
{
|
|||
|
switch (settings.textureCompression)
|
|||
|
{
|
|||
|
case TextureImporterCompression.Compressed:
|
|||
|
settings.overridden = true;
|
|||
|
settings.format = alpha ? TextureImporterFormat.ASTC_6x6 : TextureImporterFormat.ASTC_6x6;
|
|||
|
settings.compressionQuality = (int)UnityEditor.TextureCompressionQuality.Normal;
|
|||
|
break;
|
|||
|
case TextureImporterCompression.CompressedHQ:
|
|||
|
settings.overridden = true;
|
|||
|
settings.format = alpha ? TextureImporterFormat.ASTC_4x4 : TextureImporterFormat.ASTC_4x4;
|
|||
|
settings.compressionQuality = (int)UnityEditor.TextureCompressionQuality.Best;
|
|||
|
break;
|
|||
|
case TextureImporterCompression.CompressedLQ:
|
|||
|
settings.overridden = true;
|
|||
|
settings.format = alpha ? TextureImporterFormat.ASTC_8x8 : TextureImporterFormat.ASTC_8x8;
|
|||
|
settings.compressionQuality = (int)UnityEditor.TextureCompressionQuality.Fast;
|
|||
|
break;
|
|||
|
case TextureImporterCompression.Uncompressed:
|
|||
|
settings.overridden = false;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|