IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]Unity3d的Build后处理,和场景build前处理

    u010019717发表于 2016-07-31 09:38:11
    love 0
    孙广东     2016.7.24

    Unity3d的Build后处理   就是打完包之后的回调,你可以发出邮件通知或者对这个包最什么操作都可以!
    ,和场景build前处理, build场景之前你是可以对场景做一下操作的, 比如检查一些全局的状态等等!

    http://blog.csdn.net/u010019717

    using UnityEngine;
    using UnityEditor;
    using UnityEditor.Callbacks;
    using System;
    using Object = UnityEngine.Object;
    
    // C#中使用该函数首先导入命名空间:
    using System.Runtime.InteropServices; 
    
    
    // http://blog.csdn.net/bpy/article/details/6886638
    public class BuildPostprocessor
    {
        // 然后写API引用部分的代码,放入 class 内部
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); 
    
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")] 
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 
    
    
        // 获取到该窗口句柄后,可以对该窗口进行操作.比如,关闭该窗口或在该窗口隐藏后,使其显示
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
    /*其中ShowWindow(IntPtr hwnd, int nCmdShow);
    nCmdShow的含义
    0 关闭窗口
    1 正常大小显示窗口
    2 最小化窗口
    3 最大化窗口
    使用实例: ShowWindow(myPtr, 0);
    */
    
        [DllImport("User32.dll", EntryPoint = "SendMessage")] 
        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); 
        // 你可以使用VS自带的工具spy++  找出相关控件的下面四个参数
    
        const int WM_GETTEXT = 0x000D; 
        const int WM_SETTEXT = 0x000C; 
        // const int WM_CLICK = 0x00F5;
        const int BM_CLICK = 0xF5; 
    
    
        static string lpszParentClass = "#32770"; //整个窗口的类名    这个代表 对话框   , 没有这个使用下一个参数也可以
        static string lpszParentWindow = "Building Player"; //窗口标题       
    
        static string lpszClass_Submit = "Button"; //需要查找的Button的类名 
        static string lpszName_Submit = "Cancel"; //需要查找的Button的标题 
    
        /// <summary>
        /// Building的 后处理
        /// todo 需要打开这个所在的路径(文件夹)   这个Unity会自动打开
        /// </summary>
        [PostProcessBuildAttribute(1)]
        public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
        {
            Debug.Log("打包完成 =========================" + pathToBuiltProject);
        }
    
    
        /// <summary>
        /// Building场景的 前处理
        /// OK 
        /// </summary>
        [PostProcessSceneAttribute(2)]
        public static void OnPostprocessScene()
        {
            //GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            //cube.transform.position = new Vector3(0.0f, 0.5f, 0.0f);
            var gameEventCenter = Object.FindObjectOfType<GameEventCenter>();
            if (gameEventCenter)
            {
                if (!gameEventCenter.TutorialOn)
                {
                    if (EditorUtility.DisplayDialog("确定不开启新手引导么?",
                "你没有开启新手引导", "取消,需要手动", "继续"))
                    {
                        Debug.Log("==================================" + "取消打包");
                        
                        IntPtr ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);   
                        if (ParenthWnd != IntPtr.Zero)   
                        {   
                            Debug.Log("==================================" + "找到窗口");
                            // ShowWindow(ParenthWnd, 0);     // 直接销毁这个窗口并没有什么卵用,因为进程还在跑
                            
                            //得到Button这个子窗体,并触发它的Click事件 
                            IntPtr EdithWnd = FindWindowEx(ParenthWnd, new IntPtr(0), lpszClass_Submit, lpszName_Submit); 
                            if (!EdithWnd.Equals(IntPtr.Zero)) 
                            { 
                                Debug.Log("==================================" + "得到了Button");
                                SendMessage(EdithWnd, BM_CLICK, new IntPtr(0), "0"); 
                                // SendMessage(EdithWnd, BM_CLICK, EdithWnd, lpszName_Submit); 
                                // SendMessage(EdithWnd, BM_CLICK, EdithWnd, lpszName_Submit); 
                            } 
                            else
                            {
                                Debug.Log("==================================" + "没找到Button");
                            }
                        }
    
                    }
                    else
                    {
                        Debug.Log("==================================" + "继续打包");
                    }
                }
            }
        }
    }
    







沪ICP备19023445号-2号
友情链接