public static void Build(tk2dSpriteCollection data)
        {
            if (data.linkedSpriteCollections.Count > 0 && !data.disableTrimming)
            {
                return;
            }

            string errors     = "";
            int    errorCount = 0;
            string root       = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(data)) + "/Linked";

            foreach (tk2dLinkedSpriteCollection link in data.linkedSpriteCollections)
            {
                if (link.spriteCollection == null)
                {
                    if (!System.IO.Directory.Exists(root))
                    {
                        System.IO.Directory.CreateDirectory(root);
                    }
                    link.spriteCollection = tk2dSpriteCollectionEditor.CreateSpriteCollection(root, data.name + link.name);
                }

                tk2dEditor.SpriteCollectionEditor.SpriteCollectionProxy proxy = new tk2dEditor.SpriteCollectionEditor.SpriteCollectionProxy(data, false);
                proxy.CopyBuiltFromSource(link.spriteCollection);
                proxy.linkedSpriteCollections.Clear();                 // stop recursion
                string thisErrors = "";

                foreach (tk2dSpriteCollectionDefinition tp in proxy.textureParams)
                {
                    if (tp.texture != null)
                    {
                        Texture2D repl = FindReplacementTexture(tp.texture, link.name);
                        if (repl == null)
                        {
                            thisErrors += string.Format("Unable to find replacement for texture '{0}' for link '{1}'\n", tp.texture.name, link.name);
                            ++errorCount;
                        }
                        tp.texture = repl;
                    }
                }

                if (thisErrors.Length == 0)
                {
                    proxy.CopyToTarget(link.spriteCollection);
                    link.spriteCollection.linkParent = data;
                    EditorUtility.SetDirty(link.spriteCollection);

                    tk2dSpriteCollectionBuilder.Rebuild(link.spriteCollection);
                }
                else
                {
                    errors += thisErrors;
                }
            }

            if (errors.Length > 0)
            {
                Debug.LogError("There were " + errorCount.ToString() + " errors building the sprite collection\n" + errors);
            }
        }
        public static void Build(tk2dSpriteCollection data)
        {
            if (data.linkedSpriteCollections.Count > 0 && !data.disableTrimming) {
                return;
            }

            string errors = "";
            int errorCount = 0;
            string root = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(data)) + "/Linked";
            foreach (tk2dLinkedSpriteCollection link in data.linkedSpriteCollections) {
                if (link.spriteCollection == null) {
                    if (!System.IO.Directory.Exists(root)) {
                        System.IO.Directory.CreateDirectory(root);
                    }
                    link.spriteCollection = tk2dSpriteCollectionEditor.CreateSpriteCollection(root, data.name + link.name);
                }

                tk2dEditor.SpriteCollectionEditor.SpriteCollectionProxy proxy = new tk2dEditor.SpriteCollectionEditor.SpriteCollectionProxy(data, false);
                proxy.CopyBuiltFromSource(link.spriteCollection);
                proxy.linkedSpriteCollections.Clear(); // stop recursion
                string thisErrors = "";

                foreach (tk2dSpriteCollectionDefinition tp in proxy.textureParams) {
                    if (tp.texture != null) {
                        Texture2D repl = FindReplacementTexture(tp.texture, link.name);
                        if (repl == null) {
                            thisErrors += string.Format("Unable to find replacement for texture '{0}' for link '{1}'\n", tp.texture.name, link.name);
                            ++errorCount;
                        }
                        tp.texture = repl;
                    }
                }

                if (thisErrors.Length == 0) {
                    proxy.CopyToTarget(link.spriteCollection);
                    link.spriteCollection.linkParent = data;
                    EditorUtility.SetDirty(link.spriteCollection);

                    tk2dSpriteCollectionBuilder.Rebuild(link.spriteCollection);
                }
                else {
                    errors += thisErrors;
                }
            }

            if (errors.Length > 0) {
                Debug.LogError("There were " + errorCount.ToString() + " errors building the sprite collection\n" + errors);
            }
        }
        // Update target platforms
        public static void UpdatePlatformSpriteCollection(tk2dSpriteCollection source, tk2dSpriteCollection target, string dataPath, bool root, float scale, string platformName)
        {
            tk2dEditor.SpriteCollectionEditor.SpriteCollectionProxy proxy = new tk2dEditor.SpriteCollectionEditor.SpriteCollectionProxy(source);

            // Restore old sprite collection
            proxy.spriteCollection = target.spriteCollection;

            proxy.atlasTextures = target.atlasTextures;
            proxy.atlasMaterials = target.atlasMaterials;
            proxy.altMaterials = target.altMaterials;

            // This must always be zero, as children cannot have nested platforms.
            // That would open the door to a lot of unnecessary insanity
            proxy.platforms = new List<tk2dSpriteCollectionPlatform>();

            // Update atlas sizes
            proxy.atlasWidth = (int)(proxy.atlasWidth * scale);
            proxy.atlasHeight = (int)(proxy.atlasHeight * scale);
            proxy.maxTextureSize = (int)(proxy.maxTextureSize * scale);
            proxy.forcedTextureWidth = (int)(proxy.forcedTextureWidth * scale);
            proxy.forcedTextureHeight = (int)(proxy.forcedTextureHeight * scale);

            if (!proxy.useTk2dCamera)
                proxy.targetOrthoSize *= 1.0f;
            proxy.globalScale = 1.0f / scale;

            // Don't bother changing stuff on the root object
            // The root object is the one that the sprite collection is defined on initially
            if (!root)
            {
                // Update textures
                foreach (tk2dSpriteCollectionDefinition param in proxy.textureParams)
                {
                    if (param.texture == null) continue;

                    string path = AssetDatabase.GetAssetPath(param.texture);
                    string platformTexture = FindAssetForPlatform(platformName, path, textureExtensions);

                    if (platformTexture.Length == 0)
                    {
                        LogNotFoundError(platformName, param.texture.name, "texture");
                    }
                    else
                    {
                        Texture2D tex = AssetDatabase.LoadAssetAtPath(platformTexture, typeof(Texture2D)) as Texture2D;
                        if (tex == null)
                        {
                            Debug.LogError("Unable to load platform specific texture '" + platformTexture + "'");
                        }
                        else
                        {
                            param.texture = tex;
                        }
                    }

                    // Handle spritesheets. Odd coordinates could cause issues
                    if (param.extractRegion)
                    {
                        param.regionX = (int)(param.regionX * scale);
                        param.regionY = (int)(param.regionY * scale);
                        param.regionW = (int)(param.regionW * scale);
                        param.regionH = (int)(param.regionH * scale);
                    }

                    if (param.anchor == tk2dSpriteCollectionDefinition.Anchor.Custom)
                    {
                        param.anchorX = (int)(param.anchorX * scale);
                        param.anchorY = (int)(param.anchorY * scale);
                    }

                    if (param.customSpriteGeometry)
                    {
                        foreach (tk2dSpriteColliderIsland geom in param.geometryIslands)
                        {
                            for (int p = 0; p < geom.points.Length; ++p)
                                geom.points[p] *= scale;
                        }
                    }

                    if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)
                    {
                        foreach (tk2dSpriteColliderIsland geom in param.polyColliderIslands)
                        {
                            for (int p = 0; p < geom.points.Length; ++p)
                                geom.points[p] *= scale;
                        }
                    }
                    else if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom)
                    {
                        param.boxColliderMax *= scale;
                        param.boxColliderMin *= scale;
                    }
                }
            }

            // We ALWAYS duplicate fonts
            if (target.fonts == null) target.fonts = new tk2dSpriteCollectionFont[0];
            for (int i = 0; i < proxy.fonts.Count; ++i)
            {
                tk2dSpriteCollectionFont font = proxy.fonts[i];
                if (!font.InUse || font.texture == null || font.data == null || font.editorData == null || font.bmFont == null) continue; // not valid for some reason or other
                bool needFontData = true;
                bool needFontEditorData = true;
                bool hasCorrespondingData = i < target.fonts.Length && target.fonts[i] != null;
                if (hasCorrespondingData)
                {
                    tk2dSpriteCollectionFont targetFont = target.fonts[i];
                    if (targetFont.data != null) { font.data = targetFont.data; needFontData = false; }
                    if (targetFont.editorData != null) { font.editorData = targetFont.editorData; needFontEditorData = false; }
                }

                string bmFontPath = AssetDatabase.GetAssetPath(font.bmFont);
                string texturePath = AssetDatabase.GetAssetPath(font.texture);

                if (!root)
                {
                    // find platform specific versions
                    bmFontPath = FindAssetForPlatform(platformName, bmFontPath, fontExtensions);
                    texturePath = FindAssetForPlatform(platformName, texturePath, textureExtensions);
                    if (bmFontPath.Length != 0 && texturePath.Length == 0)
                    {
                        // try to find a texture
                        tk2dEditor.Font.Info fontInfo = tk2dEditor.Font.Builder.ParseBMFont(bmFontPath);
                        if (fontInfo != null)
                            texturePath = System.IO.Path.GetDirectoryName(bmFontPath).Replace('\\', '/') + "/" + System.IO.Path.GetFileName(fontInfo.texturePaths[0]);
                    }

                    if (bmFontPath.Length == 0) LogNotFoundError(platformName, font.bmFont.name, "font");
                    if (texturePath.Length == 0) LogNotFoundError(platformName, font.texture.name, "texture");
                    if (bmFontPath.Length == 0 || texturePath.Length == 0) continue; // not found

                    // load the assets
                    font.bmFont = AssetDatabase.LoadAssetAtPath(bmFontPath, typeof(UnityEngine.Object));
                    font.texture = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D;
                }

                string targetDir = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(target));

                // create necessary assets
                if (needFontData)
                {
                    string srcPath = AssetDatabase.GetAssetPath(font.data);
                    string destPath = AssetDatabase.GenerateUniqueAssetPath(GetCopyAtTargetPath(platformName, targetDir, srcPath));
                    AssetDatabase.CopyAsset(srcPath, destPath);
                    AssetDatabase.Refresh();
                    font.data = AssetDatabase.LoadAssetAtPath(destPath, typeof(tk2dFontData)) as tk2dFontData;
                }
                if (needFontEditorData)
                {
                    string srcPath = AssetDatabase.GetAssetPath(font.editorData);
                    string destPath = AssetDatabase.GenerateUniqueAssetPath(GetCopyAtTargetPath(platformName, targetDir, srcPath));
                    AssetDatabase.CopyAsset(srcPath, destPath);
                    AssetDatabase.Refresh();
                    font.editorData = AssetDatabase.LoadAssetAtPath(destPath, typeof(tk2dFont)) as tk2dFont;
                }

                if (font.editorData.bmFont != font.bmFont ||
                    font.editorData.texture != font.texture ||
                    font.editorData.data != font.data)
                {
                    font.editorData.bmFont = font.bmFont;
                    font.editorData.texture = font.texture;
                    font.editorData.data = font.data;
                    EditorUtility.SetDirty(font.editorData);
                }
            }

            proxy.CopyToTarget(target);
        }
Esempio n. 4
0
        // Update target platforms

        public static void UpdatePlatformSpriteCollection(tk2dSpriteCollection source, tk2dSpriteCollection target, string dataPath, bool root, float scale, string platformName)

        {
            tk2dEditor.SpriteCollectionEditor.SpriteCollectionProxy proxy = new tk2dEditor.SpriteCollectionEditor.SpriteCollectionProxy(source);



            // Restore old sprite collection

            proxy.spriteCollection = target.spriteCollection;



            proxy.atlasTextures = target.atlasTextures;

            proxy.atlasMaterials = target.atlasMaterials;

            proxy.altMaterials = target.altMaterials;



            // This must always be zero, as children cannot have nested platforms.

            // That would open the door to a lot of unnecessary insanity

            proxy.platforms = new List <tk2dSpriteCollectionPlatform>();



            // Update atlas sizes

            proxy.atlasWidth = (int)(proxy.atlasWidth * scale);

            proxy.atlasHeight = (int)(proxy.atlasHeight * scale);

            proxy.maxTextureSize = (int)(proxy.maxTextureSize * scale);

            proxy.forcedTextureWidth = (int)(proxy.forcedTextureWidth * scale);

            proxy.forcedTextureHeight = (int)(proxy.forcedTextureHeight * scale);



            if (proxy.padAmount > 0)
            {
                proxy.padAmount = Mathf.Max((int)(proxy.padAmount * scale), 1);                 // min 1 pixel padding
            }



            proxy.globalScale = 1.0f / scale;



            // Don't bother changing stuff on the root object

            // The root object is the one that the sprite collection is defined on initially

            if (!root)

            {
                // Update textures

                foreach (tk2dSpriteCollectionDefinition param in proxy.textureParams)

                {
                    if (param.texture == null)
                    {
                        continue;
                    }



                    string path = AssetDatabase.GetAssetPath(param.texture);

                    string platformTexture = FindAssetForPlatform(platformName, path, textureExtensions);



                    if (platformTexture.Length == 0)

                    {
                        LogNotFoundError(platformName, param.texture.name, "texture");
                    }

                    else

                    {
                        Texture2D tex = AssetDatabase.LoadAssetAtPath(platformTexture, typeof(Texture2D)) as Texture2D;

                        if (tex == null)

                        {
                            Debug.LogError("Unable to load platform specific texture '" + platformTexture + "'");
                        }

                        else

                        {
                            param.texture = tex;
                        }
                    }



                    // Handle spritesheets. Odd coordinates could cause issues

                    if (param.extractRegion)

                    {
                        param.regionX = (int)(param.regionX * scale);

                        param.regionY = (int)(param.regionY * scale);

                        param.regionW = (int)(param.regionW * scale);

                        param.regionH = (int)(param.regionH * scale);
                    }



                    if (param.anchor == tk2dSpriteCollectionDefinition.Anchor.Custom)

                    {
                        param.anchorX = (int)(param.anchorX * scale);

                        param.anchorY = (int)(param.anchorY * scale);
                    }



                    if (param.customSpriteGeometry)

                    {
                        foreach (tk2dSpriteColliderIsland geom in param.geometryIslands)

                        {
                            for (int p = 0; p < geom.points.Length; ++p)
                            {
                                geom.points[p] *= scale;
                            }
                        }
                    }

                    else if (param.dice)

                    {
                        param.diceUnitX = (int)(param.diceUnitX * scale);

                        param.diceUnitY = (int)(param.diceUnitY * scale);
                    }



                    if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.Polygon)

                    {
                        foreach (tk2dSpriteColliderIsland geom in param.polyColliderIslands)

                        {
                            for (int p = 0; p < geom.points.Length; ++p)
                            {
                                geom.points[p] *= scale;
                            }
                        }
                    }

                    else if (param.colliderType == tk2dSpriteCollectionDefinition.ColliderType.BoxCustom)

                    {
                        param.boxColliderMax *= scale;

                        param.boxColliderMin *= scale;
                    }



                    for (int i = 0; i < param.attachPoints.Count; ++i)
                    {
                        param.attachPoints[i].position = param.attachPoints[i].position * scale;
                    }
                }
            }



            // We ALWAYS duplicate fonts

            if (target.fonts == null)
            {
                target.fonts = new tk2dSpriteCollectionFont[0];
            }

            for (int i = 0; i < proxy.fonts.Count; ++i)

            {
                tk2dSpriteCollectionFont font = proxy.fonts[i];

                if (!font.InUse || font.texture == null || font.data == null || font.editorData == null || font.bmFont == null)
                {
                    continue;                                                                                                                             // not valid for some reason or other
                }
                bool needFontData = true;

                bool needFontEditorData = true;

                bool hasCorrespondingData = i < target.fonts.Length && target.fonts[i] != null;

                if (hasCorrespondingData)

                {
                    tk2dSpriteCollectionFont targetFont = target.fonts[i];

                    if (targetFont.data != null)
                    {
                        font.data = targetFont.data; needFontData = false;
                    }

                    if (targetFont.editorData != null)
                    {
                        font.editorData = targetFont.editorData; needFontEditorData = false;
                    }
                }



                string bmFontPath = AssetDatabase.GetAssetPath(font.bmFont);

                string texturePath = AssetDatabase.GetAssetPath(font.texture);



                if (!root)

                {
                    // find platform specific versions

                    bmFontPath = FindAssetForPlatform(platformName, bmFontPath, fontExtensions);

                    texturePath = FindAssetForPlatform(platformName, texturePath, textureExtensions);

                    if (bmFontPath.Length != 0 && texturePath.Length == 0)

                    {
                        // try to find a texture

                        tk2dEditor.Font.Info fontInfo = tk2dEditor.Font.Builder.ParseBMFont(bmFontPath);

                        if (fontInfo != null)
                        {
                            texturePath = System.IO.Path.GetDirectoryName(bmFontPath).Replace('\\', '/') + "/" + System.IO.Path.GetFileName(fontInfo.texturePaths[0]);
                        }
                    }



                    if (bmFontPath.Length == 0)
                    {
                        LogNotFoundError(platformName, font.bmFont.name, "font");
                    }

                    if (texturePath.Length == 0)
                    {
                        LogNotFoundError(platformName, font.texture.name, "texture");
                    }

                    if (bmFontPath.Length == 0 || texturePath.Length == 0)
                    {
                        continue;                                                                        // not found
                    }
                    // load the assets

                    font.bmFont = AssetDatabase.LoadAssetAtPath(bmFontPath, typeof(UnityEngine.TextAsset)) as TextAsset;

                    font.texture = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D;
                }



                string targetDir = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(target));



                // create necessary assets

                if (needFontData)

                {
                    string srcPath = AssetDatabase.GetAssetPath(font.data);

                    string destPath = AssetDatabase.GenerateUniqueAssetPath(GetCopyAtTargetPath(platformName, targetDir, srcPath));

                    AssetDatabase.CopyAsset(srcPath, destPath);

                    AssetDatabase.Refresh();

                    font.data = AssetDatabase.LoadAssetAtPath(destPath, typeof(tk2dFontData)) as tk2dFontData;
                }

                if (needFontEditorData)

                {
                    string srcPath = AssetDatabase.GetAssetPath(font.editorData);

                    string destPath = AssetDatabase.GenerateUniqueAssetPath(GetCopyAtTargetPath(platformName, targetDir, srcPath));

                    AssetDatabase.CopyAsset(srcPath, destPath);

                    AssetDatabase.Refresh();

                    font.editorData = AssetDatabase.LoadAssetAtPath(destPath, typeof(tk2dFont)) as tk2dFont;
                }



                if (font.editorData.bmFont != font.bmFont ||

                    font.editorData.texture != font.texture ||

                    font.editorData.data != font.data)

                {
                    font.editorData.bmFont = font.bmFont;

                    font.editorData.texture = font.texture;

                    font.editorData.data = font.data;

                    tk2dUtil.SetDirty(font.editorData);
                }
            }



            proxy.CopyToTarget(target);
        }