关于Unity导出PC包用到的工具类总结

2020-11-2 雨辰 Unity3D

导出pc版,我们会遇到控制窗口的变化,监听窗口变化等需求,我这边整理下需要用的工具类:


public class PcMonitorSys : MonoBehaviour
    {
#if UNITY_STANDALONE_WIN
        private IntPtr myintptr;
        private WindowsPcUtil.RECT rect;
        private int w;
        private int h;
        private int x;
        private int y;

        //变化后的值
        private int w_c;
        private int h_c;
        private int x_c;
        private int y_c;

        private float aspect19_9;
        public int screenWidth = 1520;
        public int screenHeight = 720;
        public int widthScal = 19;
        public int heightScal = 9;

        public float w_min = 760.0f;
        public float h_min = 360.0f;

        private float old_w;
        private float old_h;

        private TimerExt _timer;
        /// <summary>
        /// 进程检查间隔
        /// </summary>
        public float _intervalCheckProcess = 10.0f;

        private bool isApplicationQuit = false;

        /// <summary>
        /// 非法进程
        /// </summary>
        private List<string> PcIllicitProcessList;

        /// <summary>
        /// 检查到非法进程是否直接杀死
        /// </summary>
        public bool IsKillIllicitProcess = false;
        
        void Awake()
        {
            AppValues.IsPcWindowChangeTipUi = false;
            isApplicationQuit = false;
            //pc端只允许启动一个游戏窗口
            Process[] gameProcess = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
            if (gameProcess.Length > 1)
            {
                isApplicationQuit = true;
                Application.Quit();
                return;
            }

            //19:9固定比例
            aspect19_9 = (float)widthScal / heightScal;
            //获取当前应用窗口
            myintptr = WindowsPcUtil.GetActiveWindow();
            //设置窗口标题
            string titleName = LocalizationService.Instance.GetFromFile(LocalizationFiles.LoginUi, LocalizationKey.PcWindowTitle);
            WindowsPcUtil.SetWindowText(myintptr, titleName);
            //启动检查游戏环境进程
            StartCheckProcess();
        }

        void Start()
        {
            WindowsPcUtil.GetWindowRect(myintptr, ref rect);
            //窗口的宽度
            w = rect.Right - rect.Left;
            //窗口的高度
            h = rect.Bottom - rect.Top;
            //窗口的位置
            x = rect.Left;
            y = rect.Top;
        }
        void LateUpdate()
        {
            SetResolution();

            WindowsPcUtil.GetWindowRect(myintptr, ref rect);
            //窗口的宽度
            w_c = rect.Right - rect.Left;
            if (w_c != w)
            {
                //提示窗口发生变化
                TipWindowChange();
                w = w_c;
                return;
            }
            //窗口的高度
            h_c = rect.Bottom - rect.Top;
            if (h_c != h)
            {
                //提示窗口发生变化
                TipWindowChange();
                h = h_c;
                return;
            }

            //x_c = rect.Left;
            //if (x_c != x)
            //{
            //    //应用窗口位置发生变化
            //    return;
            //}
            //y_c = rect.Top;
            //if (y_c != y)
            //{
            //    return;
            //}
        }

        /// <summary>
        /// 游戏窗口按19:9比例显示
        /// </summary>
        void SetResolution()
        {
            if (!AppValues.IsPcSetResolution)
            {
                return;
            }

            int curScreenWidth = Screen.width;
            int curScreenHeight = Screen.height;

            if (old_w != curScreenWidth || old_h != curScreenHeight)
            {
                old_w = curScreenWidth;
                old_h = curScreenHeight;
                return;
            }

            if (curScreenWidth <= w_min || curScreenHeight <= h_min)
            {
                curScreenWidth = (int)w_min;
                curScreenHeight = (int)h_min;
            }

            float curScale = (float)curScreenWidth / curScreenHeight;

            if (curScale > aspect19_9 + 0.01f || curScale < aspect19_9 - 0.01f)
            {
                curScreenHeight = (int)(curScreenWidth / aspect19_9);
            }

            Screen.SetResolution(curScreenWidth, curScreenHeight, false);
        }

        /// <summary>
        /// 启动检查游戏运行进程
        /// </summary>
        private void StartCheckProcess()
        {
            CheckProcess();
            RemoveCheckTimer();
            //检查游戏运行环境进程情况
            _timer = TimerExt.Register(_intervalCheckProcess, true, () =>
            {
                //10秒钟检查一次
                CheckProcess();
            });
        }

        /// <summary>
        /// 检查游戏运行环境的进程
        /// </summary>
        private void CheckProcess()
        {
            if (!AppValues.IsStartPcCheckProcess)
            {
                return;
            }

            if (PcIllicitProcessList == null || PcIllicitProcessList.Count == 0)
            {
                string pcIllicitProcessListStr = DbHelper.GetConstString(ConstClientIds.PcIllicitProcessList);
                Log.Info(string.Format("CheckProcess PcIllicitProcessList {0}", pcIllicitProcessListStr));
                PcIllicitProcessList = pcIllicitProcessListStr.Split(Constants.Comma).ToList();
            }

            //检测加速器
            Process[] sysProcess = Process.GetProcesses();
            if (sysProcess != null)
            {
                Process fristProcess = null;
                foreach (Process pro in sysProcess)
                {
                    //UnityEngine.Debug.Log("----------------------------------------------");
                    if (pro != null)
                    {
                        try
                        {
                            string processName = pro.ProcessName;
                            int processId = pro.Id;
                            if (!string.IsNullOrEmpty(processName))
                            {
                                //UnityEngine.Debug.Log("GetProcesses ProcessNameId : " + processId);
                                //UnityEngine.Debug.Log("GetProcesses ProcessName : " + processName);
                                if (PcIllicitProcessList.IndexOf(processName) != -1)
                                {
                                    fristProcess = pro;
                                    break;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            //进程已过期失效
                            UnityEngine.Debug.Log(e.ToString());
                            continue;
                        }
                    }
                    //UnityEngine.Debug.Log("----------------------++++++------------------------");
                }
                if (fristProcess != null)
                {
                    //提示关闭加速器继续游戏,或者直接杀死进程
                    if (!NewGameUiManager.IsActive(UiElementIds.PcCheckProcessTipUi))
                    {
                        NewGameUiManager.Show(UiElementIds.PcCheckProcessTipUi, fristProcess);
                    }
                    //TODO
                    if (IsKillIllicitProcess)
                    {
                        fristProcess.Kill();
                    }
                }
            }
        }

        /// <summary>
        /// 停止检查进程timer
        /// </summary>
        private void RemoveCheckTimer()
        {
            if (_timer != null)
            {
                _timer.Cancel();
                _timer = null;
            }
        }

        /// <summary>
        /// 提示玩家窗口变化
        /// </summary>
        private void TipWindowChange()
        {
            if (AppValues.IsPcWindowChangeTipUi)
            {
                return;
            }

            if (!AppValues.IsStartPcCheckProcess)
            {
                return;
            }

            if (NewGameUiManager.IsActive(UiElementIds.PcWindowChangeTipUi))
            {
                return;
            }

            AppValues.IsPcWindowChangeTipUi = true;
            NewGameUiManager.Show(UiElementIds.PcWindowChangeTipUi);
        }

        /// <summary>
        /// 退出引用window提示
        /// </summary>
//        public void OnApplicationQuit()
//        {
//            if (isApplicationQuit)
//            {
//                return;
//            }

////#if !UNITY_EDITOR
////            Application.CancelQuit();
////            string tip = LocalizationService.Instance.GetFromFile(LocalizationFiles.LoginUi, LocalizationKey.PcWindowQuitTip);
////            string titleName = LocalizationService.Instance.GetFromFile(LocalizationFiles.LoginUi, LocalizationKey.PcWindowTitle);
////            WindowsPcUtil.MessageReturnType returnType = WindowsPcUtil.MessageBoxShow(titleName, tip, WindowsPcUtil.MessageBoxTyoe.ConfirmAndCancel);
////            if (returnType == WindowsPcUtil.MessageReturnType.OK)
////            {
////                //点击确定按钮
////                Application.Quit();
////            }
////#endif
//        }
#endif
    }



-------------------------------------------------------------------我是分割线----------------------------------------------------------------------
 public class WindowsPcUtil
    {

#if UNITY_STANDALONE_WIN
        [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        public static extern int MessageBox(IntPtr handle, string message, string title, int type);

        [DllImport("user32.dll")]
        public static extern IntPtr GetActiveWindow();

        //获取当前激活窗口
        [DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
        public static extern System.IntPtr GetForegroundWindow();

        //设置窗口边框
        [DllImport("user32.dll")]
        public static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);

        //更改标题栏
        [DllImport("user32.dll", ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
        public static extern int SetWindowText(IntPtr hWnd, string text);

        //使用查找任务栏
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string strClassName, int nptWindowName);

        //获取窗口位置以及大小
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

        //设置当前窗口的显示状态
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left; //最左坐标
            public int Top; //最上坐标
            public int Right; //最右坐标
            public int Bottom; //最下坐标
        }

        //最大最小化
        private const int SW_SHOWMINIMIZED = 2;//最小化窗口
        private const int SW_SHOWMAXIMIZED = 3;//最大化窗口

        /// <summary>
        /// 系统消息框的类型
        /// </summary>
        public enum MessageBoxTyoe
        {
            Confirm = 0,
            ConfirmAndCancel = 1,
        }

        /// <summary>
        /// 系统消息框返回类型
        /// </summary>
        public enum MessageReturnType
        {
            OK = 1,
            NO = 2,
        }

        /// <summary>
        /// 弹出window 提示框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="tip"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static MessageReturnType MessageBoxShow(string title, string tip, MessageBoxTyoe type)
        {
            IntPtr myintptr = GetActiveWindow();
            MessageReturnType returnType = (MessageReturnType)MessageBox(myintptr, tip, title, (int)type);
            return returnType;
        }

        /// <summary>
        /// 最小化窗口
        /// </summary>
        public static void SetMinWindows()
        {
            ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
        }

        /// <summary>
        /// 最大化窗口
        /// </summary>
        public static void SetMaxWindows()
        {
            ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
        }

        /// <summary>
        /// 改变标题栏标题
        /// </summary>
        public static void ChangeTitleText(string titleName)
        {
            SetWindowText(GetActiveWindow(), titleName);
        }

        /// <summary>
        /// 系统级别提示退出游戏
        /// </summary>
        public static void ApplicationQuit()
        {
            string tip = LocalizationService.Instance.GetFromFile(LocalizationFiles.LoginUi, LocalizationKey.PcWindowQuitTip);
            string titleName = LocalizationService.Instance.GetFromFile(LocalizationFiles.LoginUi, LocalizationKey.PcWindowTitle);
            MessageReturnType returnType = MessageBoxShow(titleName, tip, MessageBoxTyoe.ConfirmAndCancel);
            if (returnType == MessageReturnType.OK)
            {
                //点击确定按钮
                Application.Quit();
            }
        }
#endif
    }


评论:

Tavyfrada
2021-06-14 06:52
<a href=https://priligyset.com>priligy[/url]
skylineblade
2021-06-13 11:46
<a href="https://www.aiawyoming.com/fluffy-balls-for-tassen-tassenb">fluffy balls for tassen</a> <a href="https://www.wikistrea.com/iphone-pouzdro-for-swimming-laps-pouzdroe">iphone pouzdro for swimming laps</a> <a href="https://www.ascreenworks.com/%C5%BElut%C3%A1-button-down-%C5%A1aty-satyc">啪lut谩 button down 拧aty</a> <a href="https://www.justaccord.com/pg-nasa-berretto-cappelloa">pg nasa berretto</a> <a href="https://www.ericrcourtney.com/snake-print-slip-on-sneakers-bootsa">snake print slip on sneakers</a> <a href="https://www.pepperlilly.com/zaxy-heeled-schuhe-stiefeld">zaxy heeled schuhe</a>
skylineblade https://www.skylineblade.com/
gotosubic
2021-06-13 10:54
<a href="https://www.edexeclive.com/most-popular-iphone-8-plus-hoesje-hoesjea">most popular iphone 8 plus hoesje</a> <a href="https://www.bjgremillion.com/nfl-dorian-odaniel-mens-limited-red-jersey-44-kansas-city-chiefs-nike-tank-top-suit-jerseyc">nfl dorian odaniel mens limited red jersey 44 kansas city chiefs nike tank top suit</a> <a href="https://www.chicesthetiq.com/bia%C5%82y-fanny-torba-torebkid">bia艂y fanny torba</a> <a href="https://www.enfantparents.com/tempered-glass-case-for-iphone-casec">tempered glass case for iphone</a> <a href="https://www.symedon.com/nike-soccer-rugzak-tassenb">nike soccer rugzak</a> <a href="https://www.goisecurity.com/sports-pouzdro-for-iphone-5s-pouzdroe">sports pouzdro for iphone 5s</a>
gotosubic https://www.gotosubic.com/
actiocloud
2021-06-04 16:54
<a href="https://www.aishcleve.com/yellow-flower-sandals-bootsa">yellow flower sandals</a> <a href="https://www.terramessflug.com/traditional-leder-golf-schuhe-stiefeld">traditional leder golf schuhe</a> <a href="https://www.fermecophyto.com/honor-10-lite-waterproof-hoesje-hoesjea">honor 10 lite waterproof hoesje</a> <a href="https://www.pointlesspick.com/miami-dolphins-custom-jersey-jerseyc">miami dolphins custom jersey</a> <a href="https://www.goiplow.com/small-utility-tote-torba-torebkid">small utility tote torba</a> <a href="https://www.gostpauldodge.com/oppo-a33f-case-casec">oppo a33f case</a>
actiocloud https://www.actiocloud.com/
sucrehoteles
2021-06-04 05:26
<a href="https://www.wisegenweb.com/sehr-warme-winterschuhe-damen-stiefeld">sehr warme winterschuhe damen</a> <a href="https://www.moshekosher.com/google-pixel-2-xl-hoesje-target-hoesjea">google pixel 2 xl hoesje target</a> <a href="https://www.peggymayers.com/nfl-ronnie-harrison-mens-limited-green-jersey-36-jacksonville-jaguars-nike-salute-to-service-tank-top-jerseyc">nfl ronnie harrison mens limited green jersey 36 jacksonville jaguars nike salute to service tank top</a> <a href="https://www.brewerrepair.com/dooney-i-bourke-fringe-torba-torebkid">dooney i bourke fringe torba</a> <a href="https://www.weareguthrie.com/wallet-cover-for-iphone-8-casec">wallet cover for iphone 8</a> <a href="https://www.enewsreporter.com/beth-studded-shoulder-zak-tassenb">beth studded shoulder zak</a>
sucrehoteles https://www.sucrehoteles.com/
piumissimo
2021-05-31 16:48
<a href="http://www.scgsigns.com/asics-femme-jolt-chaussures1">asics femme jolt</a> <a href="http://www.koolmania.com/hombre-adidas-nmd-r1-stlt-primeknit-zapatos1">hombre adidas nmd r1 stlt primeknit</a>
piumissimo http://www.piumissimo.com/
masessanj
2021-05-26 18:07
<a href="https://www.gotosubic.com/vintage-tea-%C5%A1aty-wedding-satyc">vintage tea 拧aty wedding</a> <a href="https://www.antakyahotel.com/kansas-state-baseball-cappello-cappelloa">kansas state baseball cappello</a> <a href="https://www.appsnuage.com/scarpa-mojito-gtx-cocoa-bootsa">scarpa mojito gtx cocoa</a> <a href="https://www.anamyth.com/bullboxer-stiefel-damen-cognac-stiefeld">bullboxer stiefel damen cognac</a> <a href="https://www.gorgianhomes.com/xundd-hoesje-iphone-6-hoesjea">xundd hoesje iphone 6</a> <a href="https://www.mozsteel.com/nike-elite-customized-mens-jersey-nfl-san-francisco-49ers-alternate-vapor-untouchable-black-jerseyc">nike elite customized mens jersey nfl san francisco 49ers alternate vapor untouchable black</a>
masessanj https://www.masessanj.com/
afolkmusician
2021-05-26 16:20
<a href="https://www.gestiarchivos.com/huawei-p20-lite-pouzdro-a-screen-protector-pouzdroe">huawei p20 lite pouzdro a screen protector</a> <a href="https://www.fluffsstuffs.com/dannijo-slip-%C5%A1aty-satyc">dannijo slip 拧aty</a> <a href="https://www.caldreamapp.com/under-armour-berretto-original-cappelloa">under armour berretto original</a> <a href="https://www.kwlagunabeach.com/waterproof-beach-shoes-bootsa">waterproof beach shoes</a> <a href="https://www.yenhost.com/wanderschuhe-salewa-herren-stiefeld">wanderschuhe salewa herren</a> <a href="https://www.allgoodguide.com/nimbus-cell-phone-hoesje-hoesjea">nimbus cell phone hoesje</a>
[url=https://www.afolkmusician.com/]afolkmusician[/url]
tatilblogu
2021-05-03 22:33
<a href="http://www.obxpubcrawl.com/golden-state-warriors-noir-enamel-9fifty-snapback-casquette-chapeauc">golden state warriors noir enamel 9fifty snapback casquette</a> <a href="http://www.isychedelic.com/logo-moto-phone-sag-saga">logo moto phone sag</a> <a href="http://www.pmlambert.com/lined-ankle-buty-damskie-tenisc">lined ankle buty damskie</a> <a href="http://www.stacyseikelmd.com/svart-kjole-with-lilla-flowers-kjoleb">svart kjole with lilla flowers</a> <a href="http://www.dianapappas.com/colombian-kaffee-burlap-taschen-tasched">colombian kaffee burlap taschen</a> <a href="http://www.koransumedang.com/adibreak-adidas-donna-tutab">adibreak adidas donna</a>
tatilblogu http://www.tatilblogu.com/

发表评论:

淘气男孩 朱文华 joyimp|@2011-2021 京ICP备16030765号-2