Beispiel #1
0
        public static int AddToAmmonomicon(tk2dSpriteDefinition spriteDefinition, Material overrideMaterial)
        {
            int m_spriteDefinition = AddSpriteToCollection(spriteDefinition, ammonomiconCollection);

            ammonomiconCollection.GetSpriteDefinition(spriteDefinition.name).material = overrideMaterial;
            return(AddSpriteToCollection(spriteDefinition, ammonomiconCollection));
        }
Beispiel #2
0
        private static void SetupHand(AssetBundle assetBundle, out GameObject handPrefab, tk2dSpriteCollectionData collection)
        {
            var spriteDefinition = collection.GetSpriteDefinition("Western_Bros_Hand");

            // change the sprite definition so the sprite is centered, so it can be flipped without offsets
            spriteDefinition.boundsDataCenter          = Vector3.zero;
            spriteDefinition.untrimmedBoundsDataCenter = Vector3.zero;

            float val = 0.2f;

            spriteDefinition.boundsDataExtents          = new Vector3(val * 2, val * 2, 0);
            spriteDefinition.untrimmedBoundsDataExtents = new Vector3(val * 2, val * 2, 0);

            spriteDefinition.position0 = new Vector3(-val, -val, 0);
            spriteDefinition.position1 = new Vector3(val, -val, 0);
            spriteDefinition.position2 = new Vector3(-val, val, 0);
            spriteDefinition.position3 = new Vector3(val, val, 0);

            handPrefab = assetBundle.LoadAsset <GameObject>("WestBroHandObject");

            var sprite = SpriteSerializer.AddSpriteToObject(handPrefab, ExpandCustomEnemyDatabase.WestBrosCollection, spriteDefinition.name);

            handPrefab.AddComponent <PlayerHandController>();
        }
Beispiel #3
0
 public tk2dSpriteDefinition GetSpriteDefinition(string name)
 {
     return(CollectionData.GetSpriteDefinition(name));
 }
Beispiel #4
0
        public static void HandleSprites(tk2dSpriteCollectionData sprites)
        {
            if (sprites == null)
            {
                return;
            }
            string path = "sprites/" + sprites.spriteCollectionName;

            Texture2D     replacement;
            AssetMetadata metadata;

            Texture mainTexture = sprites.materials?.Length != 0 ? sprites.materials[0]?.mainTexture : null;
            string  atlasName   = mainTexture?.name;

            if (mainTexture != null && (atlasName == null || atlasName.Length == 0 || atlasName[0] != '~'))
            {
                if (TextureMap.TryGetValue(path, out replacement))
                {
                }
                else if (TryGetMapped(path, out metadata))
                {
                    TextureMap[path] = replacement = Resources.Load <Texture2D>(path);
                }
                else
                {
                    foreach (KeyValuePair <string, AssetMetadata> mapping in Map)
                    {
                        if (!mapping.Value.HasData)
                        {
                            continue;
                        }
                        string resourcePath = mapping.Key;
                        if (!resourcePath.StartsWithInvariant("sprites/@"))
                        {
                            continue;
                        }
                        string spriteName = resourcePath.Substring(9);
                        if (sprites.spriteCollectionName.Contains(spriteName))
                        {
                            string copyPath = Path.Combine(ResourcesDirectory, ("DUMP" + path).Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + ".png");
                            if (mapping.Value.Container == AssetMetadata.ContainerType.Filesystem && !File.Exists(copyPath))
                            {
                                Directory.GetParent(copyPath).Create();
                                File.Copy(mapping.Value.File, copyPath);
                            }
                            TextureMap[path] = replacement = Resources.Load <Texture2D>(resourcePath);
                            break;
                        }
                    }
                }

                if (replacement != null)
                {
                    // Full atlas texture replacement.
                    replacement.name = '~' + atlasName;
                    for (int i = 0; i < sprites.materials.Length; i++)
                    {
                        if (sprites.materials[i]?.mainTexture == null)
                        {
                            continue;
                        }
                        sprites.materials[i].mainTexture = replacement;
                    }
                }
            }

            if (DumpSprites)
            {
                Dump.DumpSpriteCollection(sprites);
            }
            if (DumpSpritesMetadata)
            {
                Dump.DumpSpriteCollectionMetadata(sprites);
            }

            List <tk2dSpriteDefinition> list = null;

            foreach (KeyValuePair <string, AssetMetadata> mapping in Map)
            {
                string assetPath = mapping.Key;
                if (assetPath.Length <= path.Length + 1)
                {
                    continue;
                }
                if (!assetPath.StartsWithInvariant(path) || mapping.Value.AssetType != t_Texture2D)
                {
                    continue;
                }

                string name = assetPath.Substring(path.Length + 1);
                tk2dSpriteDefinition frame = sprites.GetSpriteDefinition(name);

                if (frame != null && frame.materialInst != null)
                {
                    Texture2D origTex = (Texture2D)frame.materialInst.mainTexture;
                    if (Packer.IsPageTexture(origTex))
                    {
                        continue;
                    }
                }

                if (!TextureMap.TryGetValue(assetPath, out replacement))
                {
                    replacement = TextureMap[assetPath] = Resources.Load <Texture2D>(assetPath);
                }
                if (replacement == null)
                {
                    continue;
                }

                if (frame == null && name[0] == '@')
                {
                    name = name.Substring(1);
                    for (int i = 0; i < sprites.spriteDefinitions.Length; i++)
                    {
                        tk2dSpriteDefinition frame_ = sprites.spriteDefinitions[i];
                        if (frame_.Valid && frame_.name.Contains(name))
                        {
                            frame = frame_;
                            name  = frame_.name;
                            break;
                        }
                    }
                    if (frame == null)
                    {
                        continue;
                    }
                }

                if (frame != null)
                {
                    // Replace old sprite.
                    frame.ReplaceTexture(replacement);
                }
                else
                {
                    // Add new sprite.
                    if (list == null)
                    {
                        list = new List <tk2dSpriteDefinition>(sprites.spriteDefinitions?.Length ?? 32);
                        if (sprites.spriteDefinitions != null)
                        {
                            list.AddRange(sprites.spriteDefinitions);
                        }
                    }
                    frame          = new tk2dSpriteDefinition();
                    frame.name     = name;
                    frame.material = sprites.materials[0];
                    frame.ReplaceTexture(replacement);

                    AssetSpriteData frameData    = new AssetSpriteData();
                    AssetMetadata   jsonMetadata = GetMapped(assetPath + ".json");
                    if (jsonMetadata != null)
                    {
                        frameData = JSONHelper.ReadJSON <AssetSpriteData>(jsonMetadata.Stream);
                    }

                    frame.normals  = new Vector3[0];
                    frame.tangents = new Vector4[0];
                    frame.indices  = new int[] { 0, 3, 1, 2, 3, 0 };

                    // TODO figure out this black magic
                    const float pixelScale = 0.0625f;
                    float       w          = replacement.width * pixelScale;
                    float       h          = replacement.height * pixelScale;
                    frame.position0         = new Vector3(0f, 0f, 0f);
                    frame.position1         = new Vector3(w, 0f, 0f);
                    frame.position2         = new Vector3(0f, h, 0f);
                    frame.position3         = new Vector3(w, h, 0f);
                    frame.boundsDataCenter  = frame.untrimmedBoundsDataCenter = new Vector3(w / 2f, h / 2f, 0f);
                    frame.boundsDataExtents = frame.untrimmedBoundsDataExtents = new Vector3(w, h, 0f);

                    sprites.SetAttachPoints(list.Count, frameData.attachPoints);

                    list.Add(frame);
                }
            }
            if (list != null)
            {
                sprites.spriteDefinitions = list.ToArray();
                ReflectionHelper.SetValue(f_tk2dSpriteCollectionData_spriteNameLookupDict, sprites, null);
            }

            if (sprites.hasPlatformData)
            {
                sprites.inst.Handle();
            }
        }
Beispiel #5
0
    public Attachment NewAttachment(Skin skin, AttachmentType type, String name)
    {
        if (type != AttachmentType.region)
        {
            throw new Exception("Unknown attachment type: " + type);
        }

        // Strip folder names.
        int index = name.LastIndexOfAny(new char[] { '/', '\\' });

        if (index != -1)
        {
            name = name.Substring(index + 1);
        }

        tk2dSpriteDefinition attachmentParameters = sprites.GetSpriteDefinition(name);

        if (attachmentParameters == null)
        {
            throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")");
        }
        if (attachmentParameters.complexGeometry)
        {
            throw new NotImplementedException("Complex geometry is not supported: " + name + " (" + type + ")");
        }
        if (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW)
        {
            throw new NotImplementedException("Only 2D Toolkit atlases are supported: " + name + " (" + type + ")");
        }

        Vector2 minTexCoords = Vector2.one;
        Vector2 maxTexCoords = Vector2.zero;

        for (int i = 0; i < attachmentParameters.uvs.Length; ++i)
        {
            Vector2 uv = attachmentParameters.uvs[i];
            minTexCoords = Vector2.Min(minTexCoords, uv);
            maxTexCoords = Vector2.Max(maxTexCoords, uv);
        }

        Texture texture = attachmentParameters.material.mainTexture;
        int     width   = (int)(Mathf.Abs(maxTexCoords.x - minTexCoords.x) * texture.width);
        int     height  = (int)(Mathf.Abs(maxTexCoords.y - minTexCoords.y) * texture.height);

        bool rotated = (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.Tk2d);

        if (rotated)
        {
            float temp = minTexCoords.x;
            minTexCoords.x = maxTexCoords.x;
            maxTexCoords.x = temp;
        }

        RegionAttachment attachment = new RegionAttachment(name);

        attachment.SetUVs(
            minTexCoords.x,
            maxTexCoords.y,
            maxTexCoords.x,
            minTexCoords.y,
            rotated
            );

        // TODO - Set attachment.RegionOffsetX/Y. What units does attachmentParameters.untrimmedBoundsData use?!
        attachment.RegionWidth          = width;
        attachment.RegionHeight         = height;
        attachment.RegionOriginalWidth  = width;
        attachment.RegionOriginalHeight = height;

        return(attachment);
    }
Beispiel #6
0
    public Attachment NewAttachment(Skin skin, AttachmentType type, String name)
    {
        if (type != AttachmentType.region)
        {
            throw new Exception("Unknown attachment type: " + type);
        }

        // Strip folder names.
        int index = name.LastIndexOfAny(new char[] { '/', '\\' });

        if (index != -1)
        {
            name = name.Substring(index + 1);
        }

        tk2dSpriteDefinition def = sprites.GetSpriteDefinition(name);

        if (def == null)
        {
            throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")");
        }
        if (def.complexGeometry)
        {
            throw new NotImplementedException("Complex geometry is not supported: " + name + " (" + type + ")");
        }
        if (def.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW)
        {
            throw new NotImplementedException("Only 2D Toolkit atlases are supported: " + name + " (" + type + ")");
        }

        RegionAttachment attachment = new RegionAttachment(name);

        Vector2 minTexCoords = Vector2.one;
        Vector2 maxTexCoords = Vector2.zero;

        for (int i = 0; i < def.uvs.Length; ++i)
        {
            Vector2 uv = def.uvs[i];
            minTexCoords = Vector2.Min(minTexCoords, uv);
            maxTexCoords = Vector2.Max(maxTexCoords, uv);
        }
        bool rotated = def.flipped == tk2dSpriteDefinition.FlipMode.Tk2d;

        if (rotated)
        {
            float temp = minTexCoords.x;
            minTexCoords.x = maxTexCoords.x;
            maxTexCoords.x = temp;
        }
        attachment.SetUVs(
            minTexCoords.x,
            maxTexCoords.y,
            maxTexCoords.x,
            minTexCoords.y,
            rotated
            );

        attachment.RegionOriginalWidth  = (int)(def.untrimmedBoundsData[1].x / def.texelSize.x);
        attachment.RegionOriginalHeight = (int)(def.untrimmedBoundsData[1].y / def.texelSize.y);

        attachment.RegionWidth  = (int)(def.boundsData[1].x / def.texelSize.x);
        attachment.RegionHeight = (int)(def.boundsData[1].y / def.texelSize.y);

        float x0 = def.untrimmedBoundsData[0].x - def.untrimmedBoundsData[1].x / 2;
        float x1 = def.boundsData[0].x - def.boundsData[1].x / 2;

        attachment.RegionOffsetX = (int)((x1 - x0) / def.texelSize.x);

        float y0 = def.untrimmedBoundsData[0].y - def.untrimmedBoundsData[1].y / 2;
        float y1 = def.boundsData[0].y - def.boundsData[1].y / 2;

        attachment.RegionOffsetY = (int)((y1 - y0) / def.texelSize.y);

        attachment.RendererObject = def.material;

        return(attachment);
    }