- Home /
Make the same serialized class as a ScriptableObject?
Hi, let's say I'm doing a dialog system
[System.Serializable]
public class Dialog {
public string text;
}
Thats I want to be able to create and show at runtime in a script because I want to dynamically create the text like this :
Dialog dialog = new Dialog("My dialog");
dialog.Show();
But I will also have a lot of dialog and I want them to be created and saved as ScriptableObjects. So is it bad to simply do it like this?
public ScriptableDialog : ScriptableObject {
public Dialog dialog;
}
And then I can create my scriptable dialog and do something like :
public ScriptableDialog sDialog;
public void ShowDialogFromScriptable() {
sDialog.dialog.Show();
}
Is it ok? I feel like my dialog scripts are duplicated since I have one normal and one scriptable. I also need to make my class Dialog Serialized if I want to use it in my ScriptableDialog. Thanks for any answer!
Answer by jeff13gaydos · May 12 at 02:19 AM
It seems that this is a bit redundant. You could make the Dialog class inherit from ScriptableObject (and remove ScriptableDialog) and then dynamically create Dialog scriptable objects or edit existing ones. The issue with creating scriptable objects in code is I'm not sure how to make an asset out of that object at runtime (i.e. to actually save it after runtime ends). If you do need to save these dynamically created scriptable objects, you could either have many (premade) empty Dialog scriptable objects that you edit in code, or use your above solution, or you could save them into JSON files for loading later using the JsonUtility class:
Dialog newDialog = (Dialog)ScriptableObject.CreateInstance(typeof(Dialog));
newDialog.show();
JsonUtility.ToJson(newDialog);
You would obviously need to start dealing with file I/O at this point, but that seems a little overkill unless player action is intended to perminantly change dialogue. It really depends on why you need to create and save dialog at runtime, how frequently you will need to reset the dialogue, and how common this functionality is in your project. However, your proposed solution should definitely work just fine even if it seems redundant.
Your answer
Follow this Question
Related Questions
Why do I need to serialize a struct inside a ScriptableObject to save its data? 1 Answer
Error when trying to Serialize a field that is in a class 0 Answers
Serialize a list of scriptable objects to Json 0 Answers
Why do my GameObjects (prefabs) disappear from my ScriptableObject when re-opening Unity? 0 Answers