Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 DaUr3 · Jul 26, 2018 at 02:08 PM · gameobjectsdictionarymanageritem

Item Registry System

I am trying to make an item registry system so I can access Item Prefabs and spawn them really easy but I've run into a couple roadblocks.

With this system, I am trying not to have to rely on Resources.Load() unless absolutely necessary. Thus I am currently trying to implement it with an empty object on screen that acts as a singleton that stores all Item Prefabs so I don't have to do anything.

However, there are two roadblocks I have run into. Firstly, if I use a dictionary to store items so I can key them by their item IDs, I can't just drag in prefixes really easily and will be forced to use Resources.Load(). Secondly, if I use an array to store items, keying them by ItemID gets A LOT harder.

Is there another method to do this without having to rely on Resources.Load()? Or is there a much simpler way other people are doing it and I am just overthinking things?

Comment
Add comment · Show 3
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 Harinezumi · Jul 26, 2018 at 02:29 PM 1
Share

You could use an ScriptableObject class that represents an Item resource, having an ID and a reference to a prefab. Then you create an Item asset for each of your Items, and add to the array the assets. Finally, internally you use a Dictionary with key bing the ID defined by the asset, and the value the prefab.
Does this make sense?

avatar image hexagonius Harinezumi · Jul 26, 2018 at 02:36 PM 0
Share

totally correct. Serialized containers without the actual need of being in a scene should always be SerializedObjects.

avatar image DaUr3 Harinezumi · Jul 27, 2018 at 02:49 AM 1
Share

$$anonymous$$y Items are already Scriptable Objects, but apparently I never thought of storing a prefab with them!

2 Replies

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

Answer by madks13 · Jul 26, 2018 at 02:27 PM

You do know you can make your own enumerable/dictionary? You just have to implement the corresponding interfaces. You can change the behaviour so that it takes a GameObject and sets the key automatically. You should define your own comparer methods though, since it will be hard comparing prefabs quickly with default comparers.

Edit : @DaUr3 i just want to add this small thing : as mentionned in the comments, using ints as keys is the most efficient way of looking up items by key. That said, Unity seems to return InstanceID when GetHash is called, and it's an int. So you might want to use those as keys.

Comment
Add comment · Show 1 · 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 DaUr3 · Jul 27, 2018 at 11:32 AM 0
Share

Gotchu, Thanks fam.

avatar image
1

Answer by ericford · Jul 26, 2018 at 02:56 PM

Rather than using a dictionary, you can use an array of structs to create a fake dictionary that you can modify in the inspector. I quickly wrote up some code that I think achieves what you are looking for:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public struct ItemInfo
 {
     public string itemID;
     public GameObject itemPrefab;
 }
 
 public class TestDict : MonoBehaviour 
 {
     [SerializeField] private ItemInfo[] items = new ItemInfo[0];
 
     // Loops through the array of items, looking for a matching ID 
     public GameObject GetItem(string id)
     {
         foreach (ItemInfo item in items)
         {
             // If matching ID found, return that item's prefab
             if (item.itemID == id)
                 return item.itemPrefab;
         }
 
         // No matching ID, returning null
         return null;
     }
 }
Comment
Add comment · Show 4 · 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 madks13 · Jul 26, 2018 at 03:03 PM 1
Share

The reason i suggested a dictionnary is because you can change the behaviour so it accepts prefabs and gets their id directly by itself. Also, string comparison is way slower so the performance takes a huge hit (magnitude order of 1000 with only 1000 items). Also, dictionnaries are optimised for item search by key, contrary to arrays. OP said it's a system, so it might be used for large quantites of items.

avatar image Harinezumi madks13 · Jul 26, 2018 at 03:26 PM 0
Share

I completely agree, but I wanted to mention a nice trick: if the item IDs are ints, you can use them as keys into an array (or List), and there is no faster "dictionary" than an array with int keys. Of course, then the IDs should be small, non-negative numbers, and any discontinuity in the range of IDs is a waste of memory (although small, just the size of a "pointer").
So this is another reason why not to use string keys.

avatar image ericford Harinezumi · Jul 26, 2018 at 03:36 PM 0
Share

Would using an enum as the key be a smart idea? I'm just thinking usability-wise. If he needs to access this "dictionary" from multiple places in the game, he's going to need to keep referring back to the array/list to see what int is associated to each prefab. If he uses an enum ins$$anonymous$$d as the key, it would save him a lot of time and prevent mistakes.

Show more comments

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.

Follow this Question

Answers Answers and Comments

93 People are following this question.

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

Related Questions

Can't access singleton's dictionary from another singleton 0 Answers

Creating an "Importance Manager" to destroy older GameObjects from a list to make way for new GameObjects. 1 Answer

Dictionary with GameObject as key is updating values under other keys 1 Answer

How can I detect multiple triggers on same gameobject at once? 3 Answers

extra GameObjects Spawning 2 Answers


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