Forum rules - please read before posting.

Textures and sprites

There are a few places where AC use a 2DTexture input... like bg image on menus, object/interaction icons, etc...

I'm not sure if this is already achievable... but, would it be possible to overload this input to be able to accept 2DTK and native 2D Unity's sprites too? That way, you can use an atlas texture instead of multiple images, use 2DTK multiresolution atlases, etc... improving memory use.

Thanks a lot!!

Comments

  • You can use atlas textures to make sprite animations with Unity's sprite editor.  Have you not tried that?

    This video shows one way to do this:

  • Hey thanks @Xidore, but in this case I wasn't referring to character animation but to the Texture2D inputs that are in the Managers... Like inventory objects icons, mouse icons, portraits (even animated) and any graphic showed on the menús... every input is a Texture2D, so when showing them on screen will create a separate draw call...

    For example, in the 2D demo... The game once it has started, has a drawcall count of 15. The inventory is always visible, but in the demo you only get the worn, so it can only go up to 16 draw calls for this. If you create 4 more objects (ie, using the textures from 2D Demo->Graphics->Textures) and check carry on start, you'll see 4 more objects on your inventory at the start, and 4 more draw calls.

    And so on...

    I always asked myself why do this, instead of using 2DTK sprites inputs (at the beginning of 2D support), or native Unity 2D sprites. This way you could have an atlas with all your inventory objects icons, active and not, and showing a full inventory would have a cost of 1 draw call, as you are using just one texture/material ^_^
  • edited February 2015
    With AC's support for menus made with Unity UI, you can use sprites in exactly this way.  If a Texture2D is sent to Unity UI (e.g. an Inventory icon), it is converted to a sprite automatically.
  • Thanks @ChrisIceBox, although I'm not sure if I understand that conversion, sorry :P

    Let's say, I have a texture with 100 inventory icons (my atlas). Then I create 100 native Unity sprites from that texture.

    Now, on AC's inventory manager... I want to add the "Mighty Sword" object, but when choosing the icon, it asks for a texture file... and I can choose the whole atlas, but not the Mighty Sword sprite.

    Surely there is something that I can't see :?
  • There isn't, but you make a good suggestion.  This wouldn't have been possible pre-Unity UI integration, but it's something I can look into now.
  • @ChrisIceBox any news on this? I am running into the same problem 5 years later :smile: I created a spritesheet from all my inventory items but there's no way i can choose an image from the spritesheet in the Inventory settings.

  • edited December 2020

    There's a couple of issues at play:

    1. An inventory cursor would still need to be a texture, even if it were displayed as a sprite in the menu
    2. Replacing sprites for textures would prevent use of the animation features of using item textures

    However, it should be possible to bypass these on a project-level through custom scripting. Try this:

    1. For each item slot in your Inventory UI prefab, make the root Image component invisible / zero size, and attach a second Image component as a child object, in the place that you want sprites to appear
    2. Attach the script below (InventorySprites.cs) as a component on your root
    3. Assign each of the new Imge components you made in 1) to the Inspector's "Sprite Images" array
    4. Assign your inventory item sprites in the component's "Item Sprites" array, with each index of the array matching its associated item's ID number (so your item with ID = 0 is first, = 1 is second, etc)
    5. Update the Menu Name / Inventory Box Name fields to suit your own needs

    That should cause sprites to display instead of textures, though you'll need a bit more to handle the use of sprites as a cursor. See how the above works for you and we'll move onto that once the above is working.

    InventorySprites.cs:

    using AC;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class InventorySprites : MonoBehaviour
    {
    
        public Image[] spriteImages = new Image[0];
        public Sprite[] itemSprites = new Sprite[0];
    
        public string menuName = "Inventory";
        public string inventoryBoxName = "InventoryBox";
    
        private MenuInventoryBox inventoryBox;
    
    
        private void OnEnable ()
        {
            EventManager.OnInventoryAdd += OnInventoryAdd;
            EventManager.OnInventoryRemove += OnInventoryRemove;
            EventManager.OnMenuTurnOn += OnMenuTurnOn;
    
            MenuElement element = PlayerMenus.GetElementWithName (menuName, inventoryBoxName);
            if (element) inventoryBox = element as MenuInventoryBox;
        }
    
    
        private void OnDisable ()
        {
            EventManager.OnInventoryAdd -= OnInventoryAdd;
            EventManager.OnInventoryRemove -= OnInventoryRemove;
            EventManager.OnMenuTurnOn -= OnMenuTurnOn;
        }
    
    
        private void OnInventoryAdd (InvItem invItem, int value) { UpdateInventorySprites (); }
        private void OnInventoryRemove (InvItem invItem, int value) { UpdateInventorySprites (); }
        private void OnMenuTurnOn (Menu menu, bool isInstant) { UpdateInventorySprites (); }
    
    
        private void UpdateInventorySprites ()
        {
            if (inventoryBox)
            {
                for (int i=0; i<spriteImages.Length; i++)
                {
                    InvItem itemSlot = inventoryBox.GetItem (i);
                    if (itemSlot && itemSlot.id < itemSprites.Length)
                    {
                        spriteImages[i].sprite = itemSprites[itemSlot.id];
                    }
                    else
                    {
                        spriteImages[i].sprite = null;
                    }
                }
            }
        }
    
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.