Unity Editor 編輯器擴充套件三 Unity Editor 資料持久化及Editor視窗的初識
阿新 • • 發佈:2019-02-15
目錄
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 ("儲存視窗內容");
}
}
最後上一張效果圖