如何将EditorWindow设置在屏幕中央?

时间:2018-08-26 13:07:59

标签: c# unity3d

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Prefab : EditorWindow
{
    [SerializeField] private GameObject prefab;
    List<Transform> transformSelection = new List<Transform>();
    int transformsCount = 0;

    [MenuItem("Tools/Prefab")]
    static void CreatePrefab()
    {
        EditorWindow.GetWindow<Prefab>();
        //GetWindow<Prefab>().position = new Rect(980, 380, 322, 278);
        GetWindow<Prefab>().position = new Rect(Screen.width / 2, Screen.height / 2, 322, 278);
    }

这会将窗口按宽度居中放置,但位于屏幕顶部。

EditorWindow

我不确定宽度是否在中间。但是高度在顶部。

白色大矩形是窗口。

1 个答案:

答案 0 :(得分:0)

工作解决方案:

    int width = 322;
    int height = 278;
    int x = (Screen.currentResolution.width - width) / 2;
    int y = (Screen.currentResolution.height - height) / 2;

    GetWindow<Prefab>().position = new Rect(x, y, width, height);
相关问题