CG游麟网官方站
查看: 10003|回复: 23

[教程集] 转 unity3d 批量修改Texture属性 两种方式加载贴图

[复制链接]

21

主题

27

帖子

1745

积分

名动江湖

Rank: 6Rank: 6

麒麟币
1717
任务币
0
威望
0
贡献
9
主题
21
在线时间
4 小时
发表于 2016-1-14 17:44:47 | 显示全部楼层 |阅读模式
unity3d 批量修改Texture属性 两种方式加载贴图
批量修改Texture属性两种方式加载贴图,通过Resources.LoadAll  Directoy   
获取项目路径:Application.dataPath;


using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

public class ModifyTextureType : AssetPostprocessor
{

    private static string PATH = "E:\\tcb\\program\\tcb\\tcbclient\\";

    /// <summary>
    /// 批量修改
    /// </summary>
    [MenuItem("Custom/ModifyTexture/Texture Import Settings")]
    private static void Init()
    {
        LoopSetTexture2();
    }

    /// <summary>
    /// 获取贴图设置
    /// </summary>
    public static TextureImporter GetTextureSettings(string path)
    {
        TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

        //Texture Type
        textureImporter.textureType = TextureImporterType.Advanced;

        //Non power of2
        textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;

        //PlatformTextureSettings
        textureImporter.SetPlatformTextureSettings("iPhone", 1024, TextureImporterFormat.PVRTC_RGBA4);
        textureImporter.SetPlatformTextureSettings("Android", 1024, TextureImporterFormat.ETC2_RGBA8);

        return textureImporter;
    }

    /// <summary>
    /// 循环设置选择的贴图
    /// </summary>
    private static void LoopSetTexture()
    {
        Object[] textures = GetSelectedTextures();
        foreach (Texture2D texture in textures)
        {
            //获取资源路径
            string path = AssetDatabase.GetAssetPath(texture);
            TextureImporter texImporter = GetTextureSettings(path);
            TextureImporterSettings tis = new TextureImporterSettings();
            texImporter.ReadTextureSettings(tis);
            texImporter.SetTextureSettings(tis);
            AssetDatabase.ImportAsset(path);
        }
    }

    /// <summary>
    /// 获取Resources下的贴图
    /// </summary>
    /// <returns></returns>
    private static Object[] GetSelectedTextures()
    {
        Object[] textureAll;
        var textures = Resources.LoadAll("", typeof(Texture2D));
        int countAll = textures.Length;
        textureAll = new Object[countAll];
        for (int i = 0; i < countAll; i++)
        {
            textureAll = textures as Object;
        }

        return textureAll;
    }

    private static void LoopSetTexture2()
    {
        string[] fileInfo = GetTexturePath();
        int length = fileInfo.Length;

        for (int i = 0; i < length; i++)
        {
            //获取资源路径
            string path = fileInfo;
            TextureImporter texImporter = GetTextureSettings(path);
            TextureImporterSettings tis = new TextureImporterSettings();
            texImporter.ReadTextureSettings(tis);
            texImporter.SetTextureSettings(tis);
            AssetDatabase.ImportAsset(path);
        }

    }

    private static string[] GetTexturePath()
    {
        //jpg
        ArrayList jpgList = GetResourcesPath("*.jpg");
        int jpgLength = jpgList.Count;
        //png
        ArrayList pngList = GetResourcesPath("*.png");
        int pngLength = pngList.Count;
        //tga
        ArrayList tgaList = GetResourcesPath("*.tga");
        int tgaLength = tgaList.Count;

        string[] filePath = new string[jpgLength + pngLength + tgaLength];

        for (int i = 0; i < jpgLength; i++)
        {
            filePath = jpgList.ToString();
        }

        for (int i = 0; i < pngLength; i++)
        {
            filePath[i + jpgLength] = pngList.ToString();
        }

        for (int i = 0; i < tgaLength; i++)
        {
            filePath[i + jpgLength + pngLength] = tgaList.ToString();
        }

        return filePath;
    }

    /// <summary>
    /// 获取指定后掇后的文件路径
    /// </summary>
    /// <param name="fileType"></param>
    /// <returns></returns>
    private static ArrayList GetResourcesPath(string fileType)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(PATH + "Assets\\Resources");

        ArrayList filePath = new ArrayList();
        foreach (FileInfo fi in directoryInfo.GetFiles(fileType, SearchOption.AllDirectories))
        {
            string path = fi.DirectoryName + "\\" + fi.Name;
            path = path.Remove(0, PATH.Length);
            path = path.Replace("\\", "/");
            filePath.Add(path);
        }

        return filePath;
    }
}


楼主热帖
回复

使用道具 举报

3

主题

1365

帖子

1395

积分

名动江湖

Rank: 6Rank: 6

麒麟币
6
任务币
85
威望
0
贡献
0
主题
3
在线时间
40 小时
发表于 2016-1-14 22:31:53 | 显示全部楼层
感谢楼主分享
回复 支持 反对

使用道具 举报

0

主题

1万

帖子

3万

积分

垂名青史

Rank: 16Rank: 16Rank: 16Rank: 16

麒麟币
21764
任务币
359
威望
2
贡献
131
主题
0
在线时间
754 小时

忠实会员

发表于 2016-1-14 23:55:08 | 显示全部楼层
感謝分享這麼好的資源!
回复 支持 反对

使用道具 举报

0

主题

508

帖子

1023

积分

名动江湖

Rank: 6Rank: 6

麒麟币
503
任务币
0
威望
0
贡献
0
主题
0
在线时间
42 小时
发表于 2016-1-15 08:36:29 | 显示全部楼层
好东西  支持~
回复 支持 反对

使用道具 举报

4

主题

981

帖子

1万

积分

名扬四海

Rank: 11Rank: 11Rank: 11Rank: 11

麒麟币
9135
任务币
360
威望
0
贡献
0
主题
4
在线时间
294 小时

最佳新人忠实会员

发表于 2016-1-15 10:07:50 | 显示全部楼层
感謝分享  支持~
回复 支持 反对

使用道具 举报

2

主题

340

帖子

5439

积分

誉满一方

Rank: 8Rank: 8

麒麟币
5069
任务币
85
威望
0
贡献
0
主题
2
在线时间
103 小时
发表于 2016-1-15 10:23:48 | 显示全部楼层
感谢楼主分享
回复 支持 反对

使用道具 举报

1

主题

239

帖子

1263

积分

名动江湖

Rank: 6Rank: 6

麒麟币
1018
任务币
0
威望
0
贡献
0
主题
1
在线时间
19 小时
发表于 2016-1-15 10:58:41 | 显示全部楼层
感谢楼主分享
回复 支持 反对

使用道具 举报

0

主题

317

帖子

1424

积分

名动江湖

Rank: 6Rank: 6

麒麟币
1095
任务币
85
威望
0
贡献
0
主题
0
在线时间
62 小时
发表于 2016-1-15 11:03:32 | 显示全部楼层
非常不错,谢谢楼主
回复 支持 反对

使用道具 举报

5

主题

3586

帖子

5844

积分

誉满一方

Rank: 8Rank: 8

麒麟币
2258
任务币
50
威望
0
贡献
2
主题
5
在线时间
353 小时

最佳新人

发表于 2016-1-15 11:51:53 | 显示全部楼层
这个帖子不错,大家快来顶起来!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

在线客服
客服QQ:
47413829
新QQ群:
418757022
在线时间:周一至周五
9:00-22:00 Email:
47413829@qq.com
举报:网盘资源失效
在线客服
快速回复 返回顶部 返回列表