Untiy3d 脚本打包

    static string APP_NAME = "AppName";
    [MenuItem("Publish/cAndroid")]
    public static void BuildAndroid()
    {
        BuildVer(PlatformType.Android);
    }
    [MenuItem("Publish/Build IOS")]
    public static void BuildIOS()
    {
        BuildVer(PlatformType.IOS);
    }
    [MenuItem("Publish/Build Windows")]
    public static void BuildWin()
    {
        BuildVer(PlatformType.Windows);
    }
 public static bool BuildVer(PlatformType pt)
 {
         string app_name = APP_NAME;
        string target_dir = ExporterTools.GetRootDir(Exporter.VERSION_DIR);
        string target_name = app_name + RuntimeInfo.GetExt(pt);
        BuildTargetGroup targetGroup;
        BuildTarget buildTarget;
        PlayerSettings.bundleVersion = Exporter.CLIENT_VER.Replace('_', '.');
        switch(pt)
        {
            case PlatformType.Android:
                targetGroup = BuildTargetGroup.Android;
                buildTarget = BuildTarget.Android;
                PlayerSettings.bundleIdentifier = "com.leduo.buyu";
                PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, "BUYU_PUBLISH");
                break;
            case PlatformType.IOS:
                targetGroup = BuildTargetGroup.iOS;
                buildTarget = BuildTarget.iOS;
                PlayerSettings.bundleIdentifier = "com.52leduo.buyu";
                PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, "BUYU_PUBLISH");
                break;
            case PlatformType.Windows:
                targetGroup = BuildTargetGroup.Standalone;
                buildTarget = BuildTarget.StandaloneWindows;
                PlayerSettings.bundleIdentifier = "com.52leduo.buyu";
                PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, "BUYU_PUBLISH");
                break;
            default:
                Debug.Log("未知的发布类型");
                return false;
        }
        string applicationPath = target_dir + "/" + target_name;
        if (File.Exists(applicationPath))
        {
            File.Delete(applicationPath);
        }

        bool bRet = GenericBuild(FindEnabledEditorScenes(), target_dir + "/" + target_name, buildTarget, BuildOptions.None);
        if(bRet)
        {
            EditorUtility.DisplayDialog("成功", "发布成功!", "OK");
            Debug.Log("发布成功!");
            return true;
        }
        else
        {
            EditorUtility.DisplayDialog("失败", "发布失败!", "OK");
            Debug.Log("发布失败!");
            return false;
        }
 }


    private static string[] FindEnabledEditorScenes()
    {
        List<string> EditorScenes = new List<string>();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (!scene.enabled) continue;
            EditorScenes.Add(scene.path);
            Debug.Log("打包场景:" + scene.path);
        }
        return EditorScenes.ToArray();
    }

    static bool GenericBuild(string[] scenes, string target_dir, BuildTarget build_target, BuildOptions build_options)
    {
        EditorUserBuildSettings.SwitchActiveBuildTarget(build_target);
        string res = BuildPipeline.BuildPlayer(scenes, target_dir, build_target, build_options);
        if (res.Length > 0)
        {
            EditorUtility.DisplayDialog("错误", "发布版本失败!", "OK");
            return false;
        }
        return true;
    }

你可能感兴趣的:(Unity3D)