/// <summary> /// Exports the core Unity props file. /// </summary> /// <param name="exporter">The overal exporter to use for creating exporters.</param> /// <param name="platform">The compilation platform to export.</param> /// <param name="inEditor">Whether to export the inEditor version.</param> public static void ExportCoreUnityPropFile(IUnityProjectExporter exporter, CompilationPlatformInfo platform, bool inEditor) { string configuration = inEditor ? "InEditor" : "Player"; FileInfo outputFile = new FileInfo(Path.Combine(Utilities.MSBuildProjectFolder, $"{platform.Name}.{configuration}.props")); IPlatformPropsExporter propsExporter = (!inEditor && platform.BuildTarget == BuildTarget.WSAPlayer) ? CreateWSAPlayerExporter(exporter, outputFile, platform.ScriptingBackend) : exporter.CreatePlatformPropsExporter(outputFile, configuration, platform.Name, platform.ScriptingBackend); propsExporter.TargetFramework = platform.TargetFramework.AsMSBuildString(); propsExporter.DefineConstants.AddRange(platform.CommonPlatformDefines); propsExporter.DefineConstants.AddRange(inEditor ? platform.AdditionalInEditorDefines : platform.AdditionalPlayerDefines); // Common references foreach (string reference in platform.CommonPlatformReferences) { propsExporter.References[Path.GetFileNameWithoutExtension(reference)] = new Uri(reference); propsExporter.AssemblySearchPaths.Add(Path.GetDirectoryName(reference)); } // Additional references foreach (string reference in (inEditor ? platform.AdditionalInEditorReferences : platform.AdditionalPlayerReferences)) { propsExporter.References[Path.GetFileNameWithoutExtension(reference)] = new Uri(reference); propsExporter.AssemblySearchPaths.Add(Path.GetDirectoryName(reference)); } propsExporter.Write(); }
private static void RunGenerateSDKProjects() { // Create a copy of the packages as they might change after we create the MSBuild project string generatedProjectPath = Path.Combine(Utilities.MSBuildOutputFolder, "Projects"); try { Utilities.EnsureCleanDirectory(generatedProjectPath); } catch (IOException ex) { if (ex.Message.Contains(@"db.lock")) { Debug.LogError("Generated project appears to be still open with Visual Studio."); throw new InvalidDataException("Generated project appears to be still open with Visual Studio.", ex); } else { throw; } } Utilities.EnsureCleanDirectory(Path.Combine(Utilities.MSBuildOutputFolder, "Output")); MakePackagesCopy(Utilities.MSBuildOutputFolder); List <CompilationPlatformInfo> platforms = CompilationPipeline.GetAssemblyDefinitionPlatforms() .Where(t => supportedBuildTargets.Contains(t.BuildTarget)) .Select(CompilationPlatformInfo.GetCompilationPlatform) .OrderBy(t => t.Name) .ToList(); CompilationPlatformInfo editorPlatform = CompilationPlatformInfo.GetEditorPlatform(); CreateCommonPropsFile(platforms, editorPlatform, generatedProjectPath); UnityProjectInfo unityProjectInfo = new UnityProjectInfo(platforms, generatedProjectPath); // Read the solution template string solutionTemplateText = File.ReadAllText(TemplateFiles.Instance.MSBuildSolutionTemplatePath); // Read the project template string projectTemplateText = File.ReadAllText(TemplateFiles.Instance.SDKProjectFileTemplatePath); unityProjectInfo.ExportSolution(solutionTemplateText, projectTemplateText, generatedProjectPath); foreach (string otherFile in TemplateFiles.Instance.OtherFiles) { File.Copy(otherFile, Path.Combine(generatedProjectPath, Path.GetFileName(otherFile))); } string buildProjectsFile = "BuildProjects.proj"; if (!File.Exists(Path.Combine(Utilities.MSBuildOutputFolder, buildProjectsFile))) { GenerateBuildProjectsFile(buildProjectsFile, unityProjectInfo.UnityProjectName, platforms); } }
private void TryAddEnabledPlatform(Dictionary <BuildTarget, CompilationPlatformInfo> playerPlatforms, Dictionary <string, bool> enabledPlatforms, string platformName, BuildTarget platformTarget) { if (enabledPlatforms.TryGetValue(platformName, out bool platformEnabled) && platformEnabled) { CompilationPlatformInfo platform = UnityProjectInfo.AvailablePlatforms.FirstOrDefault(t => t.BuildTarget == platformTarget); if (platform != null) { playerPlatforms.Add(platformTarget, platform); } else { Debug.LogError($"Platform '{platformName}' was specified as enabled by '{ReferencePath.LocalPath}' plugin, but not available in processed compilation settings."); } } }
private static void ProcessPlatformTemplateForConfiguration(CompilationPlatformInfo platform, string projectOutputFolder, bool inEditorConfiguration) { string configuration = inEditorConfiguration ? "InEditor" : "Player"; string platformTemplate = File.ReadAllText(TemplateFiles.Instance.GetTemplateFilePathForPlatform(platform.Name, configuration, platform.ScriptingBackend)); string platformPropsText; if (inEditorConfiguration) { platformPropsText = ProcessPlatformTemplate(platformTemplate, platform.Name, configuration, platform.BuildTarget, platform.TargetFramework, platform.CommonPlatformReferences.Concat(platform.AdditionalInEditorReferences), platform.CommonPlatformDefines.Concat(platform.AdditionalInEditorDefines)); } else { platformPropsText = ProcessPlatformTemplate(platformTemplate, platform.Name, configuration, platform.BuildTarget, platform.TargetFramework, platform.CommonPlatformReferences.Concat(platform.AdditionalPlayerReferences), platform.CommonPlatformDefines.Concat(platform.AdditionalPlayerDefines)); } File.WriteAllText(Path.Combine(projectOutputFolder, $"{platform.Name}.{configuration}.props"), platformPropsText); }
public UnityProjectInfo(HashSet <BuildTarget> supportedBuildTargets) { AvailablePlatforms = new ReadOnlyCollection <CompilationPlatformInfo>(CompilationPipeline.GetAssemblyDefinitionPlatforms() .Where(t => supportedBuildTargets.Contains(t.BuildTarget)) .Select(CompilationPlatformInfo.GetCompilationPlatform) .OrderBy(t => t.Name).ToList()); EditorPlatform = CompilationPlatformInfo.GetEditorPlatform(); UnityProjectName = Application.productName; if (string.IsNullOrWhiteSpace(UnityProjectName)) { UnityProjectName = "UnityProject"; } RefreshPlugins(); RefreshProjects(); CurrentPlayerPlatform = AvailablePlatforms.First(t => t.BuildTarget == EditorUserBuildSettings.activeBuildTarget); }
/// <summary> /// Parses the current state of the Unity project. /// </summary> /// <param name="supportedBuildTargets">BuildTargets that are considered supported.</param> /// <param name="config">Config for MSBuildTools.</param> /// <param name="performCompleteParse">If this is false, UnityProjectInfo will parse only the minimum required information about current project. Includes: <see cref="ExistingCSProjects"/> <see cref="CurrentPlayerPlatform"/>.</param> public UnityProjectInfo(ILogger logger, Dictionary <BuildTarget, string> supportedBuildTargets, MSBuildToolsConfig config, bool performCompleteParse = true) { this.logger = logger; this.config = config; if (performCompleteParse) { AvailablePlatforms = new ReadOnlyCollection <CompilationPlatformInfo>(CompilationPipeline.GetAssemblyDefinitionPlatforms() .Where(t => Utilities.IsPlatformInstalled(t.BuildTarget)) .Where(t => supportedBuildTargets.ContainsKey(t.BuildTarget)) .Select(CompilationPlatformInfo.GetCompilationPlatform) .OrderBy(t => t.Name).ToList()); EditorPlatform = CompilationPlatformInfo.GetEditorPlatform(); CurrentPlayerPlatform = AvailablePlatforms.First(t => t.BuildTarget == EditorUserBuildSettings.activeBuildTarget); } else { CurrentPlayerPlatform = CompilationPlatformInfo.GetCompilationPlatform( CompilationPipeline.GetAssemblyDefinitionPlatforms() .First(t => t.BuildTarget == EditorUserBuildSettings.activeBuildTarget)); } UnityProjectName = Application.productName; if (string.IsNullOrWhiteSpace(UnityProjectName)) { UnityProjectName = "UnityProject"; } RefreshPlugins(performCompleteParse); if (performCompleteParse) { RefreshProjects(); } }
private bool ContainsDefineHelper(string define, bool inEditor, CompilationPlatformInfo platform) { return(platform.CommonPlatformDefines.Contains(define) || (inEditor ? platform.AdditionalInEditorDefines.Contains(define) : platform.AdditionalPlayerDefines.Contains(define))); }
private static void CreateCommonPropsFile(IEnumerable <CompilationPlatformInfo> availablePlatforms, CompilationPlatformInfo editorPlatform, string projectOutputFolder) { foreach (CompilationPlatformInfo platform in availablePlatforms) { // Check for specialized template, otherwise get the common one ProcessPlatformTemplateForConfiguration(platform, projectOutputFolder, true); ProcessPlatformTemplateForConfiguration(platform, projectOutputFolder, false); } ProcessPlatformTemplateForConfiguration(editorPlatform, projectOutputFolder, true); }
private void ProcessConfigurationPlatformMapping(SolutionProject solutionProject, UnityConfigurationType configType, CompilationPlatformInfo platform, IReadOnlyDictionary <BuildTarget, CompilationPlatformInfo> enabledPlatforms) { ConfigurationPlatformPair configPair = new ConfigurationPlatformPair(configType, platform.Name); solutionProject.ConfigurationPlatformMapping.Add(configPair, new ProjectConfigurationPlatformMapping() { ConfigurationPlatform = configPair, EnabledForBuild = enabledPlatforms.ContainsKey(platform.BuildTarget) }); }
/// <summary> /// Exports the common MSBuildForUnity.Common.props file. /// </summary> /// <param name="exporter">The overal exporter to use for creating exporters.</param> /// <param name="currentPlayerPlatform">Current unity platform.</param> public static void ExportCommonPropsFile(IUnityProjectExporter exporter, Version msb4uVersion, CompilationPlatformInfo currentPlayerPlatform) { ICommonPropsExporter propsExporter = exporter.CreateCommonPropsExporter(new FileInfo(Path.Combine(Utilities.ProjectPath, "MSBuildForUnity.Common.props"))); propsExporter.MSBuildForUnityVersion = msb4uVersion; string[] versionParts = Application.unityVersion.Split('.'); propsExporter.UnityMajorVersion = versionParts[0]; propsExporter.UnityMinorVersion = versionParts[1]; propsExporter.UnityEditorInstallPath = new DirectoryInfo(Path.GetDirectoryName(EditorApplication.applicationPath)); propsExporter.GeneratedProjectOutputPath = new DirectoryInfo(Utilities.MSBuildOutputFolder); propsExporter.UnityProjectAssetsDirectory = new DirectoryInfo(Utilities.AssetPath); propsExporter.CurrentTargetFramework = currentPlayerPlatform.TargetFramework.AsMSBuildString(); propsExporter.CurrentUnityPlatform = currentPlayerPlatform.Name; propsExporter.Write(); }