How to have a script that creates scriptable objects but also works in the build.
I have a part of my game where players are able to make journal entries. I have it all working in the editor. When a journal is created a scriptable object is also created. The way im doing this now is by using the asset database to create a scriptable object. This asset database does not work in the Buidl though. So how do I go about dynamically creating scriptable objects in the built version of the game.
This is what my create journal script looks like.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR //Editor only tag
using UnityEditor;
#endif //Editor only tag
public class CreateJornalEntry : MonoBehaviour {
//-------Temp Variables----------
int jornalPage;
string title;
string notes;
string audioPath;
string colorMood;
bool share;
//-------------------------------
List<string> colorMoods = new List<string>() {"Select Colour...", "Red", "Green", "Blue", "Gray" };
AudioClip myAudioClip;
AudioSource audioSource;
public InputField inputTitle;
public InputField inputNotes;
public Dropdown colorDropdown;
public Text infoText;
public Toggle toggleRecord;
public Toggle togglePlayRecording;
bool recordingInProgress;
bool playingInProgress;
public void JornalConstructor(int _jornalPage, string _title, string _notes, string _audioPath, string _colorMood, bool _share)
{
JournalAsset asset = ScriptableObject.CreateInstance<JournalAsset>();
asset.journalPage = _jornalPage;
asset.title = _title;
asset.notes = _notes;
asset.date = System.DateTime.Today.Date.ToString("MM-dd-yyyy");
asset.time = System.DateTime.Now.ToString("HH:mm:ss");
asset.audioPath = _audioPath;
asset.colorMood = _colorMood;
asset.share = _share;
AssetDatabase.CreateAsset(asset, "Assets/Data/Jornal_"+ _jornalPage +"_"+_title + ".asset");
}
// Use this for initialization
void Start ()
{
audioSource = gameObject.GetComponent<AudioSource>();
inputTitle.onEndEdit.AddListener(input => title = input);
inputNotes.onEndEdit.AddListener(input => notes = input);
toggleRecord.onValueChanged.AddListener(ToggleRecording);
togglePlayRecording.onValueChanged.AddListener(TogglePlayRecording);
colorDropdown.AddOptions(colorMoods);
//Incrament Jornal Page and save it.
PlayerPrefs.SetInt("SavedJornalPage", PlayerPrefs.GetInt("SavedJornalPage") + 1);
jornalPage = PlayerPrefs.GetInt("SavedJornalPage");
}
private void Update()
{
//When playback has finished turn toggle off.
if (playingInProgress && audioSource.isPlaying == false)
{
playingInProgress = false;
print("Audio playback over");
togglePlayRecording.isOn = false;
}
}
//Assign Color
public void DropDownIndexChanged(int index)
{
colorMood = colorMoods[index];
if (index== 0)
{
colorMood = null;
}
}
void ToggleRecording(bool toggle)
{
if (toggle)
{
myAudioClip = Microphone.Start(null, false, 120, 44100);
recordingInProgress = true;
//Disable blayback button interaction.
playingInProgress = false;
togglePlayRecording.isOn = false;
audioSource.Stop();
togglePlayRecording.interactable = false;
}
else
{
//Enable Playback button.
togglePlayRecording.gameObject.SetActive(true);
togglePlayRecording.interactable = true;
recordingInProgress = false;
string audioFileName = "Log_" + jornalPage;
string strAudioPath = "file://" + Application.persistentDataPath + "/";
audioPath = string.Format(strAudioPath + "{0}", audioFileName + ".wav");
SavWav.Save(audioFileName, SavWav.TrimSilence(myAudioClip, 0));
}
}
void TogglePlayRecording(bool toggle)
{
if (toggle)
{
StartCoroutine(LoadAudio());
}
else
{
playingInProgress = false;
audioSource.Stop();
}
}
private IEnumerator LoadAudio()
{
WWW request = new WWW(audioPath);
yield return request;
myAudioClip = request.GetAudioClip();
audioSource.clip = myAudioClip;
playingInProgress = true;
audioSource.Play();
}
//Submit Jornal
public void Submit()
{
//Need check to make sure other field are filled out before form is submitted.
//If not already done so stop recording.
if (title == null)
{
infoText.text = "Can you enter Title for the Journal";
return;
}
//Stop recording button has not been pressed.
if (audioPath == null && recordingInProgress)
{
ToggleRecording(false);
}
//Start Recording button has not been pressed
if (audioPath == null && !recordingInProgress)
{
infoText.text = "You need to record a bit of audio for this journal";
return;
}
else
{
JornalConstructor(jornalPage, title, notes, audioPath, colorMood, share);
infoText.text = "Jornal has been submitted";
Destroy(gameObject);
}
}
}
In build, people save data ( simple version of scriptable object) to json => disk space like savefile.txt
Your answer
Follow this Question
Related Questions
How to do something similar to AssetDatabase.Refresh during build? 0 Answers
Addressables.LoadAssets only loading first asset in the list 0 Answers
ScriptableObject's asset selection doesn't work! 0 Answers
Why is new Vector3 stronger when player is in the air? 0 Answers
How to make the stock first person controller able to be used while networking? 0 Answers