[Unity3D]图片格式自定义MaxSize

如何适用
只需在“项目”窗口中创建一个名为“编辑器”的文件夹,然后在其中添加此脚本即可。然后,打开Window-Custom Max Size Setter,选择一个或多个纹理并调整其最大尺寸值。

由于 NPOT 纹理大小不能很好地与压缩算法配合使用,因此该技术主要用于微调精灵图集中打包的精灵的最大大小。
[Unity3D]图片格式自定义MaxSize_第1张图片

using UnityEditor;
using UnityEngine;

public class CustomMaxSizeSetter : EditorWindow
{
	private const int MINIMUM_MAX_SIZE = 32;
	private const int MAXIMUM_MAX_SIZE = 2048;

	private int initialMaxSize = -1;
	private int currentMaxSize = -1;

	private Texture2D[] selection;
	private string selectionLabel;

	[MenuItem( "Window/Custom Max Size Setter" )]
	private static void Init()
	{
		CustomMaxSizeSetter window = GetWindow<CustomMaxSizeSetter>();
		window.minSize = new Vector2( 250f, 85f );
		window.titleContent = new GUIContent( "Custom Max Size" );
		window.Show();
	}

	private void OnEnable()
	{
		OnSelectionChange();
	}

	private void OnGUI()
	{
		GUILayout.Label( selectionLabel, EditorStyles.boldLabel );

		EditorGUI.BeginDisabledGroup( selection == null || selection.Length == 0 );

		EditorGUI.showMixedValue = currentMaxSize < 0;
		EditorGUI.BeginChangeCheck();
		int maxSize = EditorGUILayout.IntSlider( currentMaxSize, MINIMUM_MAX_SIZE, MAXIMUM_MAX_SIZE );
		if( EditorGUI.EndChangeCheck() ) // Otherwise, IntSlider clamps value from -1 to MINIMUM_MAX_SIZE with no user input
			currentMaxSize = maxSize;
		EditorGUI.showMixedValue = false;

		EditorGUILayout.Space();

		EditorGUI.BeginDisabledGroup( currentMaxSize < 0 || initialMaxSize == currentMaxSize );

		if( GUILayout.Button( "Apply" ) )
		{
			AssetDatabase.StartAssetEditing(); // Apart from batching the reimport operations, this also ensures OnProjectChange isn't called in the middle of this for-loop
			try
			{
				for( int i = 0; i < selection.Length; i++ )
					SetMaxSizeOfTexture( selection[i], currentMaxSize );
			}
			finally
			{
				AssetDatabase.StopAssetEditing();
			}
		}

		EditorGUI.EndDisabledGroup();
		EditorGUI.EndDisabledGroup();
	}

	private void OnProjectChange() // Texture Max Size might be changed from the Inspector and etc.
	{
		OnSelectionChange();
	}

	private void OnSelectionChange()
	{
		selection = Selection.GetFiltered<Texture2D>( SelectionMode.Assets );
		if( selection == null || selection.Length == 0 )
		{
			selectionLabel = "Max Size of \"Nothing Selected\":";
			initialMaxSize = currentMaxSize = -1;
		}
		else if( selection.Length == 1 )
		{
			selectionLabel = "Max Size of \"" + selection[0].name + "\":";
			initialMaxSize = currentMaxSize = GetMaxSizeOfTexture( selection[0] );
		}
		else
		{
			selectionLabel = "Max Size of \"" + selection[0].name + "\" and " + ( selection.Length - 1 ) + " more:";
			initialMaxSize = currentMaxSize = GetMaxSizeOfTexture( selection[0] );
			for( int i = 1; i < selection.Length; i++ )
			{
				int maxSize = GetMaxSizeOfTexture( selection[i] );
				if( maxSize != initialMaxSize )
				{
					initialMaxSize = currentMaxSize = -1;
					break;
				}
			}
		}

		Repaint();
	}

	private int GetMaxSizeOfTexture( Texture2D texture )
	{
		TextureImporter textureImporter = (TextureImporter) AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( texture ) );
		return textureImporter.maxTextureSize;
	}

	private void SetMaxSizeOfTexture( Texture2D texture, int maxSize )
	{
		TextureImporter textureImporter = (TextureImporter) AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( texture ) );
		textureImporter.maxTextureSize = maxSize;
		textureImporter.npotScale = TextureImporterNPOTScale.None;
		textureImporter.SaveAndReimport();
	}
}

你可能感兴趣的:(1024程序员节,unity)