3 数据交互
unityEditor 简单数据交互
- 1:EditorPrefs,轻易不要使用,出问题不负责,类似于PlayerPrefs,键值对存储 F12 查API就行了,项目中一般用不到
- 2:EditorUserSettings.Set/GetConfigValue方法,键值对存储,二进制数据
- 3:JsonUtility & EditorJsonUtility 一般不用使用其他第三方库替代,例如Litjson newjsonxxx(最新的那个库,忘了)
unityEditor ScriptableObject常用数据资产
- 1:ScriptableObject,作为编辑器,文件,数据,运行时的数据库使用,范围大,常用方式,在Project的Assets文件夹以下都可以。如果是编辑器扩展专用的ScriptableObject,最好制作在Editor文件夹以下。
using UnityEngine;
using UnityEditor;
public class NewBehaviourScript : ScriptableObject
{
[Range(0,10)]
public int number = 3;
public bool toggle = false;
public string[] texts = new string[5];
}
- 2: EditorWindow 继承自 ScriptableObject
制作继承自ScriptableObject的一个例子资源,保存在一个路径下面,
CreateAssetMenu可以在Project里面右键快速创建
using UnityEngine;
using UnityEditor;
[CreateAssetMenu(menuName = "Tools/Create ExampleAsset Instance")]
public class ExampleAsset : ScriptableObject
{
[MenuItem("Tools/Create ExampleAsset")]
static void CreateExampleAsset()
{
var exampleAsset = CreateInstance<ExampleAsset>();
AssetDatabase.CreateAsset(exampleAsset, "Assets/Editor/ExampleAsset.asset");
AssetDatabase.Refresh();
}
}
使用以下方法读取数据:
var exampleAsset =AssetDatabase.LoadAssetAtPath<ExampleAsset>("Assets/Editor/ExampleAsset.asset");
如何展现属性值并且可以修改:直接类似于MonoBehaviour一样写上 [SerializeField] 标签即可
- 3: 如何表现ScriptableObject的父子关系
using UnityEngine;
public class ParentScriptableObject : ScriptableObject
{
[SerializeField]
ChildScriptableObject child;
}
public class ChildScriptableObject : ScriptableObject
{
[SerializeField]
string str;
public ChildScriptableObject ()
{
name = "New ChildScriptableObject";
}
}
保存成一个asset数据:
using UnityEngine;
using UnityEditor;
public class ParentScriptableObject : ScriptableObject
{
const string PATH = "Assets/Editor/New ParentScriptableObject.asset";
[SerializeField]
ChildScriptableObject child;
[MenuItem ("Assets/Create ScriptableObject")]
static void CreateScriptableObject ()
{
var parent = ScriptableObject.CreateInstance<ParentScriptableObject> ();
parent.child = ScriptableObject.CreateInstance<ChildScriptableObject> ();
//这个地方如果直接使用的话再次重启unity,就会丢失子类数据,需要先将子类数据保存成一个asset,
//再将父类的属性child指向子类保存的asset.这样重启unity就不会丢失数据了,
//这样是可以使用,但是比较麻烦
//使用AddObjectToAsset可以生成资产父子关系
AssetDatabase.AddObjectToAsset (parent.child, PATH);
//使子资产不可见,再外部
parent.child.hideFlags = HideFlags.HideInHierarchy;
AssetDatabase.CreateAsset (parent, PATH);
AssetDatabase.ImportAsset(PATH);
}
}
static void SetHideFlags ()
{
var path = AssetDatabase.GetAssetPath (Selection.activeObject);
//显示子资产
foreach (var item in AssetDatabase.LoadAllAssetsAtPath(path)) {
item.hideFlags = HideFlags.None;
}
AssetDatabase.ImportAsset (path);
}
var parent = AssetDatabase.LoadAssetAtPath<ParentScriptableObject> (PATH);
//删除子资产
Object.DestroyImmediate (parent.child, true);
parent.child = null;
AssetDatabase.ImportAsset (PATH);
- 4:在Inspector面板上面展示数组
SerializedProperty property = serializedObject.FindProperty("nodePoints"); EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(property, new GUIContent("位置节点"), true); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); }
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1487842110@qq.com
Title:3 数据交互
文章字数:611
Author:诸子百家-谁的天下?
Created At:2020-05-11, 11:41:32
Updated At:2021-03-28, 02:59:27
Url:http://yoursite.com/2020/05/11/UnityEditor/3%20%E6%95%B0%E6%8D%AE%E4%BA%A4%E4%BA%92/Copyright: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。