ScriptableObject Instances Referencing One Set of Prefabs - Changing One SO Changes The same prefab
I've been working on understanding and utilizing ScriptableObjects for the last couple of weeks and after a lot of TypeMismatch and It not saving in a custom editor which may have mostly been because of using the ui elements from the heirarchy to start with and I needed to use SetDirty(currentSO) as well at as the SetDirty(target) after which I thought i had my problem solved.
Only by using prefabs of UI Elements dragged in from the Project folder to the custom editor did i get a successful save of unique SO instances using AssetDatabase functions - Or so i thought...
Now i have realized that as every instance of my SO references the same Prefabs, If i change one SO settings the prefab itself is changed and essentially all SO instances are not unique as they get/set the same single instance of the prefabs.
I hope that is clear enough to understand. My question is what is the best way to go about having the SO's work as intended, which is basically a set of variations of style and images to be applied to the ui at runtime (i know i need to use LoadResources at runtime btw)
Basically I have an SO that is the DEFAULT_SETTINGS game wide, Then i have a copied SO for each group of Items in my Interact/Inventory system that can then be changed to be unique (color, font, icon, Display or not etc)
So, would i best...
A. Change my SO's so they do not reference the UI Element prefabs and use custom fields to store the appropriate settings to apply at runtime.
B. Save a copy of each prefab within the SO.asset if the developer selects settings to be overridden to the defaults. This method i think would make for some much larger asset files as well (i am assuming you can add a prefab to a .asset file along with the existing SO?)
C. something else again?
I was Hoping/thinking that each SO already would make a copy of the settings for these prefabs and be unique and ready to apply SO Instance DEFAULT or SO OVERRIDE to the ui as required.
And i have checked, I am almost 100% sure each SO is unique and not a reference to the one copied... It's only where they reference the Prefabs which is actually most of the fields now which looked like a nice and neat way to do it - but alas it's a reference and not a copy.
Answer by ZenMicro · Jan 12, 2016 at 06:21 AM
To wrap this question up and hopefully help others if they run into a similar problem what I've done to resolve the issue is;
I've made the UI Manger script (a single instance per game) have the only reference to the ui elements, and kept the Scriptable Objects contain 'loose' variables such as the Text and images and style to be applied to the ui. One Instance in the UI manager for Defaults/Fallback and one per object group as required.
I also needed change allowSceneObjects: false to back to true as I was forcing the project prefabs to be assigned instead of the hierarchy instances.
So simply put, I have the ui elements in game referenced only once in the ui manager editor and then use my Scriptable object settings to apply at run-time. My main issue all along was not applying SetDirty() on the SO's and also referencing the ui objects in those SO's. It still may be an option to have an instance of these prefabs on the SO's and swap the out but i'm just applying changes to the one canvas now. Hope this is of some use!
Your answer
Follow this Question
Related Questions
AssetDatabase.LoadAssetAtPath Not finding assets 0 Answers
Changes to asset data persist - unable to discard changes 1 Answer
Call function for instantated prefab. "Object reference not set" 1 Answer
Detect changes in the value of other objects referenced? 1 Answer
Compute Shader Pass RWStructuredBuffer by Reference 1 Answer