• 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 /
  • Help Room /
avatar image
1
Question by borro56 · Sep 28, 2016 at 07:16 PM · prefabsscriptable object

Scriptable Object References to Prefabs

We are using Scriptable Objects as our game database. We let the Game Designer combine them to configure the game. In a case we need a scriptable object to have a reference to a prefab, i.e. a mission asset that contains a list of enemy prefabs to instantiate. Later, we have a World Asset that contains a list of missions, so we can drag all the missions to this world asset. Finally, we have a mission selection screen that loads dynamically simply iterating over the mission array of the world asset and creating a button per each mission.

I suspect that this approach have a serious problem, and i need someone to confirm if my concern is true. If we have an asset loaded in a scene (World), that have references to several assets (Mission), and each of them have references to prefabs, that means that the prefabs must be loaded in memory? If thats true i need to change this, because this behaviour would force the Mission Selection menu to load all the possible enemy assets in vain.

The solution im trying to implement is just storing the prefab name and search for it in the Resources folder just in the time is needed, but that force me to make some refactoring, and because im not sure the exact behaviour of unity in this case i prefer ask here.

Thanks in advance.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by BurakMarmalade · Jun 15, 2017 at 04:50 PM

Old post but there are 79 people following this Q so i'll answer it.

I can confirm it will indeed load all referenced objects into memory.

The solution (or at least my solution), as you rightly suggested, would be to change the ScriptableObect prefab variable types from GameObject to strings, have a 'dummy' GameObject in the inspector script for your ScriptableObject and once it is not null (i.e. the designer has drag/dropped the prefab, save the path of the prefab. Then, at a later time when the designer clicks back on the ScriptableObject, it checks to see if the string path is !string.IsNullOrEmpty(yourPrefabPathString), then loads that prefab into the drag/drop slot. So visually it looks like you have a prefab object referenced while internally it's loading it on the fly and not saving it when you click off the ScriptableObject.

During runtime, to load the prefab from the path don't forget to clean up the path string by removing everything up to the end of 'Resources/' and the extension of the file with:

path = path.Remove (0, path.IndexOf ("Resources")+10); //+10 bc resources = 9 chars + 1 for '/'

path = path.Remove (path.IndexOf (".")); //Removes the extension

e.g.\Assets\MyAwesomeGame\Resources\Textures\Enemies\BigScaryGhost.png

becomes

Textures\Enemies\BigScaryGhost

and you can now load the big scary ghost with ' Resources.Load(path); '

Hope this helps!

Comment
Add comment · 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
0

Answer by oakshiro · Jul 26, 2017 at 09:52 AM

I had the same problem some years ago:

We had a dictionary of ids vs prefabs in a WorldTiles prefab. Once the worldtiles was loaded, all of the referenced prefabs were loaded in memory aswell.

In the end we had to change the values to strings pointing the paths in the resources folder, as stated.

I didn't know if with the scriptableobjects happened the same, but i suspected it. In my next project i was going to try the scriptableobject approach, but you saved me a lot of time trying it out. thanks!

Comment
Add comment · 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
0

Answer by ben-rasooli · Jul 14, 2020 at 01:09 AM

Anything in the Resources will be loaded into the memory when the app runs. I don't understand how you gain any benefit by putting your prefabs there and referencing them by a string.

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 oakshiro · Jul 14, 2020 at 05:36 AM 1
Share

That's not exactly true. Although there will indeed be a memory peak at startup, it won't be as big as having all of the prefabs directly referenced.

The difference lays in that all assets in the Resources folder will be added to the build (making it bigger) and loaded in memory whenever you call the Resources.Load(). Once the asset is no longer needed, it's recommended to call the Resources.UnloadAsset or Resources.UnloadUnusedAssets method to minimize memory overhead.

That said, nowadays the best option would be to use AssetBundles. $$anonymous$$aybe it precises to be more organised and a little more coding, but it is worth it in terms of memory usage.

avatar image oakshiro · Jul 14, 2020 at 05:48 AM 1
Share

Well, It seems that the best option at this time wouldn't be the AssetBundles either, but the new Addressables system. https://docs.unity3d.com/Packages/com.unity.addressables@0.3/manual/AddressableAssetsGettingStarted.html

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

78 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

Related Questions

For some reason I cannot drag my prefabs from the Project folder into the scene hierarchy. 2 Answers

How to redraw a texture on a prefab instance when a property in the inspector is changed by the developer? 0 Answers

Limit an amount of prefabs instantiated in javascript for Unity 5. 0 Answers

my instantiation /prefab of dan wont play death animations like orignal dan 0 Answers

Character Selection 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges