unity创建一个EditorWindow

 [MenuItem("Tools/MyWindow")]//菜单栏的路径
    static void Openwindow()
    {
        TestEditorWindow window = GetWindow<TestEditorWindow>("我的测试小窗口");
        window.Show();//窗口显示
        window.minSize=new Vector2(800,600);//窗口的最大最小的是多少
    }
    Color colorField = Color.green;
     string inpustr = "";//默认输入的字符串
    bool istoggle = false;
    float sliderValue;
    MonsterName monster = MonsterName.小怪;
    Vector3 pos;
    GameObject obj;
   
    private void OnGUI()
    {
        EditorGUILayout.BeginVertical();//开始确定垂直方向的排列
        #region
        colorField = EditorGUILayout.ColorField(colorField, GUILayout.Width(300), GUILayout.Height(20));
        EditorGUILayout.BeginHorizontal();//开始水平布局
        GUILayout.Label("StreammingAssets是特殊文件夹", GUILayout.Width(300), GUILayout.Height(50));
        if (GUILayout.Button("StreammingAssets", GUILayout.Width(300), GUILayout.Height(50)))//编辑器状态下点击Button响应
        {
            Debug.Log(Application.streamingAssetsPath);
        }
        EditorGUILayout.EndHorizontal();//结束水平布局
        EditorGUILayout.BeginHorizontal();//有开始,就要有结束
        GUILayout.Label("Resources是特殊文件夹", GUILayout.Width(300), GUILayout.Height(50));
        if(GUILayout.Button("Resources",GUILayout.Width(300),GUILayout.Height(50)))
        {
            Debug.Log("Resources");
        }
        EditorGUILayout.EndHorizontal();
        #endregion
        #region
        EditorGUILayout.BeginHorizontal();
        //inpustr = EditorGUILayout.TextField(inpustr);//文本输入框字段
        inpustr = EditorGUILayout.TextArea(inpustr);//多行文本
        istoggle = EditorGUILayout.Toggle(istoggle, "Toggle");
        if (istoggle)
        {
            if (GUILayout.Button("保存", GUILayout.Height(50)))
            {
                FileStream fs;
                //一定先要有streamingAssets文件夹
                string path = Application.streamingAssetsPath + "/MyText.txt";
                if (!File.Exists(path))
                {
                    fs = File.Create(path);
                }
                else
                {
                    //fs = File.Open(path, FileMode.Append);//每次内容追加到最后面
                    fs = File.Open(path, FileMode.Truncate);//替换【覆盖原有的内容】
                }

                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(inpustr);

                sw.Flush();//刷新
                sw.Close();//关闭资源
                fs.Close();//关闭资源

            } 
        }
        sliderValue = GUILayout.HorizontalSlider(sliderValue, 0, 1);
        GUILayout.Label("SilderValue"+sliderValue.ToString());
      //  GameObject.Find("Audio Source").GetComponent<AudioSource>().volume = sliderValue;
        monster = (MonsterName)EditorGUILayout.EnumPopup(monster, GUILayout.Width(200));
        GUILayout.Label("Monster"+monster.ToString());
        
        
        EditorGUILayout.EndHorizontal();

        //保存下来的位置
        pos = EditorGUILayout.Vector3Field("坐标", pos);//赋值vector3
       // GameObject.Find("Cube").transform.position = pos;
       //参数一:字段前的标签  名字
       //参数二:字段显示的对象   对象的变量名字
       //参数三 :可以分配的类型对象  泛型的类型
       //参数四:允许分配场景的对象   true (允许选择场景中的对象)
        obj = EditorGUILayout.ObjectField("选择对象", obj, typeof(GameObject), true) as GameObject;
        if(obj != null)
        {
            obj.transform.position = pos;
        }
        #endregion


        EditorGUILayout.EndVertical();//结束垂直方向的排列
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值