码迷,mamicode.com
首页 > 编程语言 > 详细

[Unity编辑器]编辑器与序列化

时间:2016-05-12 23:51:03      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

1.

using UnityEngine;
using System.Collections;
using UnityEditor;

/// <summary>
/// EditorPrefs可以序列化的数据有:bool/float/int/string
/// </summary>
public class TestEditor : EditorWindow {

    string s;
    
    [MenuItem("Window/TestEditor")]
    static void Init()
    {
        EditorWindow.GetWindow<TestEditor>();
    }

    void OnEnable()
    {
        s = EditorPrefs.GetString(GetType().Name + "s");
    }

    void OnDisable()
    {
        EditorPrefs.SetString(GetType().Name + "s", s); 
    }

    void OnGUI()
    {
        s = EditorGUILayout.TextField("data:", s);
    }
}

2.

using UnityEngine;
using System.Collections;
using UnityEditor;

/// <summary>
/// NGUISettings是对EditorPrefs的进一步封装,可以序列化Object
/// </summary>
public class TestEditor2 : EditorWindow {

    Object o;

    [MenuItem("Window/TestEditor2")]
    static void Init()
    {
        EditorWindow.GetWindow<TestEditor2>();
    }

    void OnEnable()
    {
        o = NGUISettings.Get<GameObject>(GetType().Name + "o", null);
    }

    void OnDisable()
    {
        NGUISettings.Set(GetType().Name + "o", o);
    }

    void OnGUI()
    {
        o = EditorGUILayout.ObjectField("data:", o, typeof(GameObject));
    }
}

3.

using UnityEngine;
using System.Collections;

public class TestEditor3SO : ScriptableObject {

    public int id;
    public Vector3 pos; 
    public GameObject go;

}

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

public class TestEditor3 : EditorWindow {

    public int id;
    public Vector3 pos;
    public GameObject go;

    [MenuItem("Window/TestEditor3")]
    static void Init()
    {
        EditorWindow.GetWindow<TestEditor3>();
    }

    void OnEnable()
    {
        string path = "Assets/Editor/Data/" + GetType().Name + ".asset";
        if (!File.Exists(path)) return;
        TestEditor3SO so = AssetDatabase.LoadAssetAtPath(path, typeof(TestEditor3SO)) as TestEditor3SO;
        id = so.id;
        pos = so.pos;
        go = so.go;
    }

    void OnDisable()
    {
        TestEditor3SO so = CreateInstance<TestEditor3SO>();
        so.id = id;
        so.pos = pos;
        so.go = go;
        string path = "Assets/Editor/Data/" + GetType().Name + ".asset";
        AssetDatabase.CreateAsset(so, path);
        AssetDatabase.Refresh();
    }

    void OnGUI()
    {
        id = EditorGUILayout.IntField("id:", id);
        pos = EditorGUILayout.Vector3Field("pos:", pos);
        go = EditorGUILayout.ObjectField("go:", go, typeof(GameObject)) as GameObject;
    }

}


[Unity编辑器]编辑器与序列化

标签:

原文地址:http://blog.csdn.net/lyh916/article/details/51347591

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!