当前位置:   article > 正文

[Unity3D]图片格式自定义MaxSize_unityeditor 代码切换图片maxsize

unityeditor 代码切换图片maxsize

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

由于 NPOT 纹理大小不能很好地与压缩算法配合使用,因此该技术主要用于微调精灵图集中打包的精灵的最大大小。
请添加图片描述

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();
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/125548
推荐阅读
相关标签
  

闽ICP备14008679号