Ejemplo n.º 1
0
        public void OnPreprocessBuild(BuildReport report)
        {
            //Force save the settings before build
            ProjectSave.OnWillSaveAssets(null);

            string rootFolder   = Directory.GetParent(Application.dataPath) + "/CustomProjectSettings/";
            string targetFolder = Application.dataPath + "/Resources/CustomSettings/";

            //NO BUENO EXISTING FOLDER
            if (Directory.Exists(targetFolder))
            {
                Directory.Delete(targetFolder, true);
            }

            //Ensure the resources directory exists !!!
            if (!Directory.Exists(Application.dataPath + "/Resources"))
            {
                Directory.CreateDirectory(Application.dataPath + "/Resources");
            }

            Directory.CreateDirectory(targetFolder);

            //Try to copy all files from target directory
            if (Directory.Exists(rootFolder))
            {
                Copy(rootFolder, targetFolder);
            }
        }
        public static void GetInstance <T>(ref T settings) where T : CustomSettingsBase
        {
#if UNITY_EDITOR
            if (settings != null)
            {
                if (!ProjectSave.HasReference(settings.Save))
                {
                    ProjectSave.onSave += settings.Save;
                }
                return;
            }
#endif

            string fileName = typeof(T).ToString();
#if UNITY_EDITOR
            fileName = fileName + ".json";
            string folder = Directory.GetParent(Application.dataPath) + "/CustomProjectSettings/";

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            if (File.Exists(folder + fileName))
            {
                try
                {
                    settings = (T)ScriptableObject.CreateInstance(typeof(T));
                    //Attempt to read the file and overwrite the contents of the ScriptableObject
                    JsonUtility.FromJsonOverwrite(File.ReadAllText(folder + fileName), settings);

                    if (!ProjectSave.HasReference(settings.Save))
                    {
                        ProjectSave.onSave += settings.Save;
                    }
                    return;
                }
                catch (Exception e)
                {
                    //Something went wrong reading the file
                    string result = "Failed to read type of {0} from {1} : {2}";
                    Debug.LogWarning(string.Format(result, typeof(T), folder + fileName, e.Message));
                }
            }

            Debug.Log(string.Format("Creating new instance of {0}", typeof(T)));

            //First create an instance of the ScriptableObject
            settings = (T)ScriptableObject.CreateInstance(typeof(T));
            //Initialise the object
            settings.Initialise();

            if (!ProjectSave.HasReference(settings.Save))
            {
                ProjectSave.onSave += settings.Save;
            }

            //Weeeeeeeeee
            return;
#else
            Debug.Log("Loading setting " + fileName + " from Resources");
            try
            {
                //Load the file as a text asset
                var txtAsset = Resources.Load <TextAsset>("CustomSettings/" + fileName);

                settings = (T)ScriptableObject.CreateInstance(typeof(T));
                //Attempt to read the file and overwrite the contents of the ScriptableObject
                JsonUtility.FromJsonOverwrite(txtAsset.text, settings);
                return;
            }
            catch
            {
                Debug.LogWarning(string.Format("Failed to read custom setting {0}. Creating default instance.", fileName));

                settings = (T)ScriptableObject.CreateInstance(typeof(T));
                settings.Initialise();
                return;
            }
#endif
        }