1. 程式人生 > >Unity3d的Build後處理,和場景build前處理

Unity3d的Build後處理,和場景build前處理

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("==================================" + "繼續打包");
                }
            }
        }
    }
}