简体   繁体   中英

How can I set EditorWindow to be in the center of the screen?

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);
    }

This put the window in the center by width but on the top of the screen.

编辑器窗口

I'm not sure that even the width is in the middle. But the height is on the top.

The big white rectangle is the window.

Working solution:

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM