标签:
ScriptableObject主要实现对象序列化的保存,因为是Unity自己的序列化,所以比xml,json序列化方便很多,但相对可控性也比较差
using UnityEditor; using UnityEngine; using System.Collections; public class ScriptableTestWindow : EditorWindow { [MenuItem("ScriptableTest/MyTest")] public static void CallFunc() { EditorWindow.GetWindow<ScriptableTestWindow>(); } void OnGUI() { if (GUILayout.Button("Save Scriptable Data")) { var myData = ScriptableObject.CreateInstance<MyData>(); myData.myName = "Dark"; myData.myLevel = 70; AssetDatabase.CreateAsset(myData, "Assets/ScriptableTest.asset"); AssetDatabase.SaveAssets(); } if (GUILayout.Button("Load Scriptable Data")) { var myData = AssetDatabase.LoadAssetAtPath<MyData>("Assets/ScriptableTest.asset"); Debug.Log("myName: " + myData.myName); Debug.Log("myLevel: " + myData.myLevel); } } }
Editor下读取
using UnityEngine; using System.Collections; public class RuntimeTest : MonoBehaviour { void Start() { var myData = Resources.Load<MyData>("ScriptableTest"); Debug.Log("name " + myData.myName); Debug.Log("level " + myData.myLevel); } }
注意,在Unity编辑器中,Resource读到的数据还可以再次修改,也会被保存,要防止误操作。
标签:
原文地址:http://www.cnblogs.com/hont/p/5457922.html