ScriptableObject origin = AssetDatabase.LoadAssetAtPath(path, typeof(ScriptableObject)) as ScriptableObject;
ScriptableObject so = ScriptableObject.Instantiate(origin);
//change so...
//how to apply so's changes to origin
EditorUtility.SetDirty(origin);
AssetDatabase.SaveAssets();
You’ll need to give your object a method that takes an instance of itself and update it’s own values:
public class MyScriptableObject : ScriptableObject
{
public int SomeInt;
public bool SomeBool;
public void UpdateValues(MyScriptableObject myObject)
{
SomeInt = myObject.SomeInt;
SomeBool = myObject.SomeBool;
}
}
What I do for Editor stuff like this is:
- Get a JSON from the Object you want to copy with JsonUtility.ToJson.
- Apply the JSON to the other Object with JsonUtility.FromJsonOverwrite.
- Mark the Object dirty and save as you are already doing.
If your Object is not a ScriptableObject or a MonoBehaviour, use EditorJsonUtility instead of JsonUtility. If you want to be able to undo the changes, call Undo.RegisterCompleteObjectUndo before calling FromJsonOverwrite.
I found a better choice:
EditorUtility.CopySerialized(so, origin);
//or EditorUtility.CopySerializedManagedFieldsOnly(so, origin);
2 Likes