- Home /
ScriptableObject vs. PlayerPrefsX vs. Json (Advice needed)
Hello, I'm trying to tweak parameters of a gameObject (Enemy), and need to save them as preset for later use (before or during the runtime or for access from other classes).
Could you suggest some simple and reliable system to achieve my goal?
Here's my code:
using System;
using UnityEngine;
namespace Assets.Sandbox.PresetSystem
{
class Enemy : MonoBehaviour
{
public string enemyName;
public enum enemyKind { ORC, ZOMB, HUMAN };
public enemyKind kind;
}
}
and here's CustomEditor code:
using System;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
namespace Assets.Sandbox.PresetSystem
{
[CustomEditor(typeof(Enemy))]
public class EnemyEditor : Editor
{
string presetName;
public List<string> presets = new List<string>();
int index = 0;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
EditorGUILayout.Space();
presetName = EditorGUILayout.TextField("Preset Name: ", presetName);
if (GUILayout.Button("Save Preset"))
savePreset();
EditorGUILayout.Space();
index = EditorGUILayout.Popup(index, presets.ToArray());
if (GUILayout.Button("Load Preset")) { }
loadPreset();
}
// NOTE: this is kind of pseudocode, and basically `xxx` is something I'd like you to help me with
void savePreset()
{
presets.Add(presetName);
// xxx.addNew();
Enemy myScript = (Enemy)target;
// xxx.add(myScript.transform.position);
// xxx.add(myScript.transform.localScale);
// xxx.add(myScript.kind);
// xxx.save(presetName);
}
void loadPreset()
{
// xxx.load(presetName);
Enemy myScript = (Enemy)target;
// myScript.transform.position = xxx.load(1);
// myScript.transform.localScale = xxx.load(1); ;
// myScript.kind = xxx.load(1);
}
}
}
Here's how this code looks like in Inspector:
Please note, I simplified the whole thing for easier communication in the forum. Final project will have many more parameters, subclasses and datatypes. For now, it would be great if you give some suggestions about which way to go for my purpose.
Thank you,
intl.png
(41.0 kB)
Comment