在unity editor下创建一个窗口

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Xml.Serialization;
using System.IO;

public class EditorWindowTest : EditorWindow
{
string str = “”;
[MenuItem(“Tools/EditorWindow”)]
static void OpenWindow()
{
//获取窗口并打开
EditorWindowTest window = EditorWindow.GetWindow();
window.Show();//显示窗口
}
Color color = Color.gray;
private void OnGUI()
{
//开始控件的自动布局
EditorGUILayout.BeginVertical();//竖直方向排列

    //放UI控件
    color = EditorGUILayout.ColorField(color,GUILayout.Width(200),GUILayout.Height(20));
    GUI.color = color;
    //UI按钮, 输出路径
    EditorGUILayout.BeginHorizontal();
    if (GUILayout.Button("DataPath", GUILayout.Width(100), GUILayout.Height(20)))
    {
        Debug.Log(Application.dataPath);
    }
    GUILayout.Label("Application.dataPath是在编辑器下使用的路径——自动定位到Assets文件夹路径");
    EditorGUILayout.EndHorizontal();

    EditorGUILayout.BeginHorizontal();
    if (GUILayout.Button("StreamingAssetsPath", GUILayout.Width(150), GUILayout.Height(20)))
    {
        Debug.Log(Application.streamingAssetsPath);
    }
    GUILayout.Label("Application.streamingAssetsPath在移动平台下面是一个只读的文件夹,在PC平台下可读可写但一般在这个路径下只存放只读的资源");
    //C:/Users/Students_029/Desktop/woerjianmin/AngryBots2_Cinemachine/AssetBundle/Assets/StreamingAssets
    EditorGUILayout.EndHorizontal();

    EditorGUILayout.BeginHorizontal();
    if (GUILayout.Button("persistentDataPath", GUILayout.Width(150), GUILayout.Height(20)))
    {
        Debug.Log(Application.persistentDataPath);//沙盒路径,这是系统自动为每个应用程序自动分配的文件夹,从网上下载的AssetsBundle资源包这类的可以保存在这个路径下
    }
    GUILayout.Label("沙盒路径,这是系统自动为每个应用程序自动分配的文件夹,从网上下载的AssetsBundle资源包这类的可以保存在这个路径下");
    //C:/Users/Students_029/AppData/LocalLow/DefaultCompany/AssetBundle
    EditorGUILayout.EndHorizontal();

    EditorGUILayout.BeginHorizontal();
    //创建一个输入的文本框进来
    str = EditorGUILayout.TextField(str,GUILayout.Width(100), GUILayout.Height(20));
    if (GUILayout.Button("保存", GUILayout.Width(100), GUILayout.Height(20)))
    {
        //通过文件流,读取沙盒路径下的文件
        FileStream fs;
        if (!File.Exists(Application.persistentDataPath + "/test.txt"))
        {
            //如果不存在,就自动创建一个
            fs = File.Create(Application.persistentDataPath + "/test.txt");
        }
        else//如果文件存在,直接打开这个文件
        {
            fs = File.Open(Application.persistentDataPath + "/test.txt",FileMode.Append);//FileMode.Append 文本框里面如果有东西 就接着这个东西后面写
        }
        //字符流的方式写入我的文本里面
        StreamWriter sw = new StreamWriter(fs);
        sw.WriteLine(str);
        sw.Flush();	
        sw.Close();
        fs.Close();
    }
    if (GUILayout.Button("输出", GUILayout.Width(100), GUILayout.Height(20)))
    {
        //通过文件流,读取沙盒路径下的文件
        FileStream fs;
        if (File.Exists(Application.persistentDataPath + "/test.txt"))
        {
            fs = File.Open(Application.persistentDataPath + "/test.txt", FileMode.Open);//FileMode.Append 文本框里面如果有东西 就接着这个东西后面写
            StreamReader sr = new StreamReader(fs);
            string s = sr.ReadToEnd();
            Debug.Log(s);
            sr.Close();
            fs.Close();
        }
    }
    EditorGUILayout.EndHorizontal();
    //结束控件的自动布局
    EditorGUILayout.EndVertical();
}

}

猜你喜欢

转载自blog.csdn.net/bellainvan/article/details/108512361
今日推荐