- Home /
Scriptable Object - cloning with dynamic data
Say I have a Scriptable Object with these data fields:
Name (name of file)
FileType (txt, pdf, xls, etc...)
DataPath (where the file is located)
I also have a folder with say 10 documents in it.
What I want to do, at run time: Clone the scriptable object, and then populate those fields, for each document I have. I've already got my code to find the folder, and make a list with the names, file types, and datapaths, I just need to pass that those values to the scriptable objects.
Is this possible? Or do they not work that way? Otherwise, I'd have to create 10 scriptable objects, and fill the fields in manually. This is not an option, as the number of documents in the folder may change from day to day, so this needs to be dynamic.
So something like
void OnEnable()
{
foreach (File foo in FileList)
{
scriptableobject.Name = foo.name;
scriptableobject.FileTye = foo.filetype;
scriptableobject.Datapath = foo.path;
{
}
To take the template scriptable object, make 10 of them, each one with its data corresponding to a different document in my folder.
Answer by HarshadK · Feb 16, 2017 at 07:42 AM
Create a FileItem class which will hold the data of a single file.
[System.Serializable]
public class FileItem
{
public string name;
public FileType fileType;
public string dataPath;
}
And create a Scriptable Object that will hold the array of these file items.
public class Files : ScriptableObject
{
public FileItem[] fileItems;
}
Now you can add your scanned files data to this array.
void OnEnable()
{
// Set the length of fileItems array.
scriptableobject.fileItems = new FileItem[FileList.length];
// Now populate the array.
for (int i = 0; i < FileList.length; i++)
{
scriptableobject.fileItems[i].name = FileList[i].name;
scriptableobject.fileItems[i].fileTye = FileList[i].filetype;
scriptableobject.fileItems[i].datapath = FileList[i].path;
}
}
Note that you can only modify and save the Scriptable Object in Editor only. Also you can use SerializedObject to update the data as it is preferred way.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
ScriptableObject losing data in AssetBundle 2 Answers
Set types of scripts i can place in ScriptableObject[] 1 Answer