您的位置:首页 > 移动开发 > Unity3D

Unity Editor 编辑器扩展三 Unity Editor 数据持久化及Editor窗口的初识

2016-11-23 02:29 2321 查看

目录

目录

Unity Editor 数据持久化及Editor窗口的初识
代码 ExampleWindowcs

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息