批量转换Texture为Sprite

    //批量将贴图Texture转换为精灵Sprite
    //在project视图,选中多张Texture,在菜单栏Tool/ConvertTextureToSprite,点击按钮
    public class TextureConvertToSprite
    {
        [MenuItem("Tool/ConvertTextureToSprite")]
        static void ConvertToSprite()
        {
            //获取所有被选中的物体
            System.Object[] selection = (System.Object[])Selection.objects;
            //合法性处理
            if (selection.Length == 0)
            {
                return;
            }
            //批量导入贴图
            foreach (System.Object obj in selection)
            {
                //取得每一张贴图
                Texture texture = obj as Texture;
                //获得贴图路径
                string localpath = AssetDatabase.GetAssetPath(texture);
                //贴图导入
                TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(localpath);
                //设置贴图类型
                importer.textureType = TextureImporterType.Sprite;
                //导入项目资源
                AssetDatabase.ImportAsset(localpath);
            }
            //刷新项目资源
            AssetDatabase.Refresh();
        }
    }

猜你喜欢

转载自blog.csdn.net/itsxwz/article/details/81273436