• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Foxsocks79 · Jun 08, 2017 at 03:46 PM · c#2d spritesenum

Question about using Enums and Dictionary for Sprites

I'm making a 2d game where armor is comprised of multiple sprites (up to 20).

I've thought of 2 ideas for how to manage this in my armor class.

1) Just have variables for each Sprite in the class eg

 public class Armor {
 ...
     public Sprite chest;
     public Sprite legLeft;
     public Sprite legRight;
     public Sprite armLeft;
     public Sprite armRight;
 ...
 }

2) Create Enums for my various armor slots and in my armor class just have a dictionary of those enums and sprites - There will be a "loader" method that will load the sprites from their asset bundle into the dictionary):

  public class Armor {
 ...
     public enum ArmorSlot
     {
        chest,
        legLeft,
        legRight,
        armLeft,
        armRight
    }
 
     public Dictionary<ArmorSlot, Sprite> armorSprites;
 ..
 }

At this stage I'm leaning to the second approach as I won't be using the unity editor to configure which sprites belong to the armors (armors are being deserialized from a json file and sprites loaded from an asset bundle via naming conventions)

Can anyone see any problems with the second approach? Any suggestions appreciated.

Thanks

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jmgek · Jun 08, 2017 at 07:27 PM 0
Share

Why are you not just creating a list to contain the sprites?

 List<Sprite> chest; 

It seems you're doing an extra step with your dictionary, because you still have to populate it.

avatar image Foxsocks79 jmgek · Jun 08, 2017 at 08:53 PM 0
Share

Reading your comment, I do agree that with you, I am doing an unnecessary step populating the dictionary.

I don't need a list of Sprites for the Chest - there will only be one chest sprite at a time.

I am using skeletal animation so the armor in its entirety is made from several sprites. (Chest, Legs, Arms)

I think the first approach, for its simplicity, is probably what i need to do in this case.

Thanks for you input

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hexagonius · Jun 08, 2017 at 07:52 PM

I'd go for the first approach. The enum would just be an unnecessary overhead for identification, what the variable names already do. If you had different armors, that's a different story, you'd have multiple chests you'd need to differentiate, but in your case everything is already unique.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jmgek · Jun 08, 2017 at 08:04 PM 0
Share

Even if he does go with the first approach he cant assign more than one sprite to each armor class. Why I suggested just using a List of sprites.

avatar image Foxsocks79 · Jun 08, 2017 at 09:00 PM 0
Share

I think you're right, the variable names will be enough - the enums and dictionary add an unnecessary layer of complexity to the class.

I do have multiple armors, but they are just separate instances of the class - there are other properties apart from Sprites that differ.

Thanks for your help

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problems with enums and switches (C#) 2 Answers

Restricting enum options in inspector when using a propertydrawer. 0 Answers

C# Enum for Animation Issue 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges