public Config(string configPath)
        {
            //Debug.Log("Config file read : " + configPath);
            var data = AssetDatabase.LoadAssetAtPath <TextAsset>(configPath).text;
            var json = new JSONObject(data);

            // 설정파일을 읽는다.
            outputPath = json[c_jsonfield_outputPath].str;
            var pair = json[c_jsonfield_targetDimension].list;

            outputWidth  = (int)pair[0].n;
            outputHeight = (int)pair[1].n;

            // 설정파일과 같은 경로에 있는 텍스쳐들을 검색
            string dir, filename;

            FSNUtils.StripPathAndName(configPath, out dir, out filename);
            var textureGUIDs = AssetDatabase.FindAssets("t:Texture2D", new string[] { dir });
            var texcount     = textureGUIDs.Length;
            var texturePaths = new string[texcount];

            for (int i = 0; i < texcount; i++)
            {
                texturePaths[i] = AssetDatabase.GUIDToAssetPath(textureGUIDs[i]);
            }
            spritePathList = texturePaths;
        }
예제 #2
0
    /// <summary>
    /// 타겟 경로가 유효하도록, 필요할 경우 폴더를 생성한다.
    /// </summary>
    /// <param name="targetpath"></param>
    public static void MakeTargetDirectory(string targetdir)
    {
        if (!string.IsNullOrEmpty(targetdir) && !AssetDatabase.IsValidFolder(targetdir))
        {
            string parent, current;
            FSNUtils.StripPathAndName(targetdir, out parent, out current);

            MakeTargetDirectory(parent);
            if (!string.IsNullOrEmpty(current))
            {
                AssetDatabase.CreateFolder(parent, current);
                //Debug.LogFormat("making dir : {0}, {1}", parent, current);
            }
        }
    }
    /// <summary>
    /// 조합 이미지 하나를 빌드한다.
    /// </summary>
    /// <param name="config"></param>
    /// <returns></returns>
    static bool BuildCombinedImage(Config config)
    {
        var packer = new FSNSpritePacker();

        packer.outputPath = config.outputPath;
        var paths = config.spritePathList;
        var count = paths.Length;

        for (int i = 0; i < count; i++)                                             // 각 텍스쳐 경로에 대해 실행
        {
            EnsureSourceTextureImportSetting(paths[i]);                             // 소스 텍스쳐 임포트 세팅 맞추기

            var    origpath = paths[i];
            string path, name;
            FSNUtils.StripPathAndName(origpath, out path, out name);

            var texture = AssetDatabase.LoadAssetAtPath <Texture2D>(origpath);
            var data    = new FSNSpritePackerData(texture, FSNUtils.RemoveFileExt(name));
            packer.PushData(data);                                                                                      // 팩커에 텍스쳐 하나씩 밀어넣기
        }

        return(packer.Pack(config.outputWidth, config.outputHeight));
    }
예제 #4
0
    /// <summary>
    /// 특정 경로에 있는 텍스쳐들을 검색해서 premultiplied alpha를 적용하여 빌드한다.
    /// </summary>
    /// <param name="searchPath"></param>
    public static void ConvertWithinPath(string searchPath, string destPath)
    {
        var guids     = AssetDatabase.FindAssets("t:Texture2D", new string[] { searchPath });
        var count     = guids.Length;
        var processed = 0;

        for (var i = 0; i < count; i++)
        {
            var    path = AssetDatabase.GUIDToAssetPath(guids[i]);
            string dir, name;
            FSNUtils.StripPathAndName(path, out dir, out name);                                      // 텍스쳐 경로, 이름 분리
            var subdir = searchPath.Length == dir.Length? "" : dir.Substring(searchPath.Length + 1); // 검색 경로의 하위 경로를 뽑아낸다.

            var importer = AssetImporter.GetAtPath(path) as TextureImporter;                         // 텍스쳐 타입을 제대로 설정한다.
            importer.npotScale     = TextureImporterNPOTScale.None;
            importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
            importer.isReadable    = true;
            importer.SaveAndReimport();


            var completeDestPath = destPath;
            if (subdir.Length > 0 && subdir[0] != '/')
            {
                subdir = "/" + subdir;
            }
            completeDestPath += subdir;

            //Debug.Log("completeDestPath : " + completeDestPath);

            Convert(path, name, completeDestPath);
            processed++;
        }

        AssetDatabase.Refresh();

        Debug.LogFormat("Premultiplied Alpha 텍스쳐 생성 완료. 총 {0} 개 처리했습니다.", processed);
    }
    protected override void OnSuccess(int width, int height, Output[] output)
    {
        var outtex = new Texture2D(width, height, TextureFormat.ARGB32, false);

        var pixels  = outtex.GetPixels32();                                                     // 텍스쳐를 0으로 채우기
        var pixcnt  = pixels.Length;
        var zeroCol = new Color32(0, 0, 0, 0);

        for (int i = 0; i < pixcnt; i++)
        {
            pixels[i] = zeroCol;
        }
        outtex.SetPixels32(pixels);

        int count = output.Length;

        for (int i = 0; i < count; i++)                                                         // 텍스쳐를 조립한다.
        {
            var entry   = output[i];
            var texture = entry.data.texture;
            outtex.SetPixels32(entry.xMin, entry.yMin, texture.width, texture.height, texture.GetPixels32());
        }

        //var textureOutAssetPath	= "Resources/" + outputPath + ".png";							// 실제 출력할 텍스쳐 경로 (asset)
        var    textureOutAssetPath = outputPath + ".png";
        var    textureOutRealPath = Application.dataPath + "/" + textureOutAssetPath;                                   // 실제 출력할 텍스쳐 경로 (절대경로)
        string outPath, outName;

        FSNUtils.StripPathAndName(textureOutAssetPath, out outPath, out outName);
        FSNEditorUtils.MakeTargetDirectory("Assets/" + outPath);                                        // 타겟 디렉토리 확보
        System.IO.File.WriteAllBytes(textureOutRealPath, outtex.EncodeToPNG());                         // 텍스쳐를 파일로 기록한다.
        AssetDatabase.Refresh();

        var importer = AssetImporter.GetAtPath("Assets/" + textureOutAssetPath) as TextureImporter;
        var settings = new TextureImporterSettings();

        importer.ReadTextureSettings(settings);
        settings.ApplyTextureType(TextureImporterType.Sprite, false);
        settings.spriteMeshType = SpriteMeshType.FullRect;
        settings.spriteExtrude  = 0;
        settings.rgbm           = TextureImporterRGBMMode.Off;
        importer.SetTextureSettings(settings);

        importer.textureType      = TextureImporterType.Advanced;                                          // 출력한 텍스쳐의 임포트 옵션 설정
        importer.anisoLevel       = 0;
        importer.filterMode       = FilterMode.Bilinear;
        importer.mipmapEnabled    = false;
        importer.npotScale        = TextureImporterNPOTScale.None;
        importer.isReadable       = true;
        importer.spriteImportMode = SpriteImportMode.Single;                    // 어떤 경우, Single로 바꾼 다음 다시 Multi로 바꿔야한다.
        importer.textureFormat    = TextureImporterFormat.AutomaticTruecolor;
        importer.linearTexture    = true;

        var spritesheet = new List <SpriteMetaData>();                                                          // 스프라이트 시트 생성 (각 스프라이트 영역 구분)

        for (int i = 0; i < count; i++)
        {
            var entry = output[i];
            spritesheet.Add(new SpriteMetaData()
            {
                pivot     = new Vector2(0.5f, 0.5f),
                alignment = 0,
                name      = entry.data.spriteName,
                rect      = new Rect(entry.xMin, entry.yMin, entry.data.width, entry.data.height)
            });
        }
        importer.spriteImportMode = SpriteImportMode.Multiple;
        importer.spritesheet      = spritesheet.ToArray();

        importer.SaveAndReimport();
    }