Skip to content

Instantly share code, notes, and snippets.

@paulhayes
Created May 23, 2018 16:05
Show Gist options
  • Save paulhayes/f16339ea51df4e0c90919def4bc44a67 to your computer and use it in GitHub Desktop.
Save paulhayes/f16339ea51df4e0c90919def4bc44a67 to your computer and use it in GitHub Desktop.
Simple abstract ScriptableObject Class to break settings assets into json file
using System.IO;
using UnityEngine;
public abstract class ExternalScriptableObject : ScriptableObject
{
void OnEnable()
{
string filePath = Path.Combine(Application.dataPath, GetFileName());
if( File.Exists(filePath) ){
JsonUtility.FromJsonOverwrite( File.ReadAllText(filePath), this );
}
}
public string GetFileName(){
return name+".json";
}
}
using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using System.IO;
public class ExternalScriptableObjectPostprocessBuild : IPostprocessBuild
{
public int callbackOrder { get { return 0; } }
public void OnPostprocessBuild(BuildTarget target, string path)
{
var settingsObjects = Resources.FindObjectsOfTypeAll<ExternalScriptableObject>();
foreach(var settings in settingsObjects){
string dataDir = Path.Combine( Directory.GetParent(path).FullName,Path.GetFileNameWithoutExtension(path)+"_Data");
Debug.LogFormat("{0} {1}",dataDir,Directory.Exists(dataDir)?"exists":"doesn't exist");
string filePath = Path.Combine(dataDir,settings.GetFileName());
File.WriteAllText( filePath, JsonUtility.ToJson( settings, true ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment