Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question: Same object using different sprites

Discussion in 'Scripting' started by Langdelliooo, Oct 9, 2018.

  1. Langdelliooo

    Langdelliooo

    Joined:
    Sep 20, 2018
    Posts:
    17
    hello,

    I have an XML file that acts as an item database, it allows me to create items using the following;
    ItemID
    ItemName
    ItemDescription
    ItemSprite

    I then have a sprite manager in which I attach all sprites and whatever description I have assigned to itemsprite in my XML file will load that sprite for that given item. However in an attempt to create a bit more diversity I would like my the same item to have 2 sprites which are randomly assigned. For example item: bread has sprite RedBread and BlueBread. The item characteristics are still the exact same just using a different sprite.

    What would be the best way to achieve this outcome?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    In your factory where you build your object out and determine the sprite to use from the xml. If there's more than 1 available sprite, randomly select one of the available options using UnityEngine.Random class.
     
  3. Langdelliooo

    Langdelliooo

    Joined:
    Sep 20, 2018
    Posts:
    17
    So currently my XML looks a little like this;

    <itemID>01</itemID>
    <itemName>Sword</itemName>
    <itemSprite>Sword_01</itemSprite>

    <itemID>02</itemID>
    <itemName>Shield</itemName>
    <itemSprite>Shield_01</itemSprite>

    <itemID>03</itemID>
    <itemName>Cake</itemName>
    <itemSprite>Cake_01</itemSprite>

    On my sprite editor I use a public array which holds my sprites;

    Sword_01
    Sword_02
    Shield_01
    Shield_02
    Cake_01
    Cake_02

    Does this mean that I would need to add in another itemsprite line into the XML and subsequent script to use the random function?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    The format of you xml is up to you.

    XML supports multiple nodes of the same name, so you could just say:
    Code (csharp):
    1. <item>
    2.     <itemID>01</itemID>
    3.     <itemName>Sword</itemName>
    4.     <itemSprite>Sword_01</itemSprite>
    5.     <itemSprite>Sword_02</itemSprite>
    6. </item>
    7. <item>
    8.     <itemID>02</itemID>
    9.     <itemName>Shield</itemName>
    10.     <itemSprite>Shield_01</itemSprite>
    11. </item>
    In the same respect, you could come up with a different format that's more descriptive:
    Code (csharp):
    1.  
    2. <item>
    3.     <itemID>01</itemID>
    4.     <itemName>Sword</itemName>
    5.     <sprites>
    6.         <itemSprite>Sword_01</itemSprite>
    7.         <itemSprite>Sword_02</itemSprite>
    8.     </sprites>
    9. </item>
    10. <item>
    11.     <itemID>02</itemID>
    12.     <itemName>Shield</itemName>
    13.     <sprites>
    14.         <itemSprite>Shield_01</itemSprite>
    15.     </sprites>
    16. </item>
    17.  
    That's up to you though... you have to parse this, do it however you prefer!
     
  5. Langdelliooo

    Langdelliooo

    Joined:
    Sep 20, 2018
    Posts:
    17
    That is brilliant, I can’t thank you enough for your time!