Hi guys! I want to know if there is any way I can edit a script from another script.
Let me explain:
I’m working on a serialization system based on this video from GameDev Guide:
The problem is that I need to create a Serialization Surrogate for every non-serializable script (for example, an ScriptableObject for an inventory item) and then add 2 more lines on the SerializationManager.GetBinaryFormatter() (one for creating the variable and other to add it to the selector).
What I want to do is to make an Editor script that picks a script and then verify: “if its a SerializationSurrogate script, then add it to SerializationManager.GetBinaryFormatter(), both in variable and in the selector”, for automating the process (because every time I need to make an ScriptableObject serializable, I need to do this, based on my testings).
You’d have to open and modify the .cs text files themselves, using C#'s file functions. Something along the lines of:
using System.IO;
....
string cSharpFilePath = Path.Combine(Application.dataPath, "GeneratedScripts", "SomeScript.cs");
string fileContents = "[System.Serializable]public class SomeScript { public int someValue; }";
File.WriteAllText(cSharpFilePath, fileContents);
AssetDatabase.Refresh();
That said, this is very likely not the best way to go about this problem, and I’m not sure I follow the logic behind needing this. When you say:
I don’t know what you mean? It seems like you only need to do this for certain types like Vector3 and Quaternion (looking at time 6:45 in the vid), not for every single class type you have? When the vid says “classes that aren’t natively serializable” at 6:13, MonoBehaviours and any class tagged with [System.Serializable] are natively serializable.
Yes, but my problem is mainly with ScriptableObjects, where System.Serializable don’t work with them. That’s why I wanted to know how to do this: last year I was working on a game and I needed to save the inventory (which was maded from ScriptableObjects of items) and I couldn’t implement a save because ScriptableObjects aren’t serializable (and from my recent tests, I notice that it I make a serialization surrogate for the specific ScriptableObjects that I need, I can save/load it). That stayed in my head and I decided to try once again implementing to fix it (also, I looked at the Asset Store for a save system and the good ones are paid, which I can’t buy a save system from asset store because I don’t have money)