Dev/Unity3D

Unity3D Inspector object 선택후 일괄 처리

임챙 2011. 7. 2. 01:24
아래 코드는 Texture 들을 선택해서 일괄적으로 포멧을 변경하는 코드입니다.
응용하면 다른 타입도 가능합니다.
아래 코드는 Assets/Editor/CusomMenu.cs 에 생성하셔야 합니다.
using UnityEditor;
using UnityEngine;
using System.Collections;

public class CustomMenu : ScriptableObject
{
	// ----------------------------------------------------------------------------
	[MenuItem ("CustomMenu/Texture Format")]
	static void ChangeTexture() 
	{
		SeletedChangeTexture();
	}
	
	static void SeletedChangeTexture()
	{
		Object[] textures = GetSelectedTextures();
		Selection.objects = new Object[0];
		
		int count = 0;
		foreach(Texture2D texture in textures)
		{			
			count++;
			string path = AssetDatabase.GetAssetPath(texture);
			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
			//texture.wrapMode = TextureWrapMode.Clamp;
			textureImporter.textureType = TextureImporterType.Advanced;
			textureImporter.mipmapEnabled = false;
			textureImporter.npotScale = TextureImporterNPOTScale.None;
			textureImporter.maxTextureSize = 4096;
			textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
			AssetDatabase.ImportAsset(path);
			
			Debug.Log(string.Format("[{0}/{1}] ChangeTexture progress : {2}", count, textures.Length, path));
		}
	}
	
    static Object[] GetSelectedTextures()
    {
        return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
    }	
}