Unity Editor 編輯器擴展三 Unity Editor 數據持久化及Editor窗口的初識

目錄

Unity Editor 數據持久化及Editor窗口的初識

學習一下編輯器數據永久保存,和PlayerPrefs基本上是一樣的,順便熟悉下新建窗口,裏面控件類似OnGUI。

代碼 ExampleWindow.cs

using UnityEngine;
using UnityEditor;

public class ExampleWindow : EditorWindow
{
    int intervalTime = 60;
    string text ;
    const string AUTO_SAVE_INTERVAL_TIME = "AutoSave interval time";
    const string SIZE_WIDTH_KEY = "ExampleWindow size width";
    const string SIZE_HEIGHT_KEY = "ExampleWindow size height";
    const string INPUT_VALUE = "InputVale";

    [MenuItem ("Window/Example")]
    static void Open ()
    {
        GetWindow <ExampleWindow>("我的窗口");
    }

    void OnEnable ()
    {
        var width = EditorPrefs.GetFloat (SIZE_WIDTH_KEY, 600);
        var height = EditorPrefs.GetFloat (SIZE_HEIGHT_KEY, 400);
        position = new Rect (position.x, position.y, width, height);

        intervalTime = EditorPrefs.GetInt (AUTO_SAVE_INTERVAL_TIME, 60);
        text = EditorPrefs.GetString (INPUT_VALUE);
    }

    void OnDisable ()
    {
        EditorPrefs.SetFloat (SIZE_WIDTH_KEY, position.width);
        EditorPrefs.SetFloat (SIZE_HEIGHT_KEY, position.height);
        EditorPrefs.SetString (INPUT_VALUE,text);
    }

    void OnGUI ()
    {
        text = EditorGUILayout.TextField("輸入框:",text);
        if(GUILayout.Button("按鈕",GUILayout.Width(200)))
        {
            Debug.Log ("點了按鈕");
        }

        EditorGUI.BeginChangeCheck ();

        //自動保存間隔/秒
        intervalTime = EditorGUILayout.IntSlider ("間隔(秒)", intervalTime, 1, 3600);

        if (EditorGUI.EndChangeCheck ())
            EditorPrefs.SetInt (AUTO_SAVE_INTERVAL_TIME, intervalTime);
        Debug.Log ("保存窗口內容");
    }
}

最後上一張效果圖
這裏寫圖片描述

相關資源:http://download.csdn.net/detail/warrenmondeville/9694659
本文鏈接:http://blog.csdn.net/WarrenMondeville/article/details/53297638

發佈了31 篇原創文章 · 獲贊 17 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章