示例#1
0
        private static void ProcessResourceAsset(string assetGuid, string path, Object asset, ProjectResources projectResources)
        {
            if (projectResources.ResourcePaths.Contains(path))                     // Handle assets stored in `Resources`.
            {
                var otherAsset = Resources.Load(path, typeof(UnityEngine.Object)); // Check if a different asset is available under the same resources path.
                if (ObjectUtils.IsValid(otherAsset) && otherAsset != asset)
                {
                    var otherPath = AssetDatabase.GetAssetPath(otherAsset);
                    PostprocessBuild();
                    EditorUtility.ClearProgressBar();
                    throw new System.Exception($"Resource conflict detected: asset stored at `{otherPath}` conflicts with `{path}` Naninovel resource; rename or move the conflicting asset and rebuild the player.");
                }
                return;
            }

            if (useAddressables)
            {
                if (!AddressableHelper.CheckAssetConflict(assetGuid, path, out var conflictAddress))
                {
                    AddressableHelper.CreateOrUpdateAddressableEntry(assetGuid, path);
                    return;
                }
                Debug.Log($"Asset assigned as a Naninovel `{path}` resource is already registered in the Addressable Asset System as `{conflictAddress}`. It will be copied to prevent conflicts.");
            }

            var objPath      = AssetDatabase.GetAssetPath(asset);
            var resourcePath = PathUtils.Combine(tempResourcesPath, path);

            if (objPath.Contains("."))
            {
                resourcePath += $".{objPath.GetAfter(".")}";
            }

            EditorUtils.CreateFolderAsset(resourcePath.GetBeforeLast("/"));
            AssetDatabase.CopyAsset(objPath, resourcePath);
        }
示例#2
0
        private static void ProcessResourceAsset(string assetGuid, string resourcePath, string assetPath, IReadOnlyDictionary <string, Type> projectResources)
        {
            if (projectResources.Keys.Contains(resourcePath)) // Handle assets stored in `Resources`.
            {
                var otherAsset = Resources.Load(resourcePath, typeof(UnityEngine.Object));
                AssetDatabase.TryGetGUIDAndLocalFileIdentifier(otherAsset, out var otherGuid, out long _);
                if (otherAsset && otherGuid != assetGuid) // Check if a different asset is available under the same resources path.
                {
                    var otherPath = AssetDatabase.GetAssetPath(otherAsset);
                    PostprocessBuild();
                    EditorUtility.ClearProgressBar();
                    throw new Exception($"Resource conflict detected: asset stored at `{otherPath}` conflicts with `{resourcePath}` Naninovel resource; rename or move the conflicting asset and rebuild the player.");
                }
                return;
            }

            if (useAddressables)
            {
                if (!AddressableHelper.CheckAssetConflict(assetGuid, resourcePath, out var conflictAddress))
                {
                    AddressableHelper.CreateOrUpdateAddressableEntry(assetGuid, resourcePath, config.GroupByCategory);
                    return;
                }
                Debug.Log($"Asset assigned as a Naninovel `{resourcePath}` resource is already registered in the Addressable Asset System as `{conflictAddress}`. It will be copied to prevent conflicts.");
            }

            var tempPath = string.IsNullOrWhiteSpace(config.ProjectRootPath) ? PathUtils.Combine(TempResourcesPath, resourcePath) : PathUtils.Combine(TempResourcesPath, config.ProjectRootPath, resourcePath);

            if (assetPath.Contains("."))
            {
                tempPath += $".{assetPath.GetAfter(".")}";
            }

            EditorUtils.CreateFolderAsset(tempPath.GetBeforeLast("/"));
            AssetDatabase.CopyAsset(assetPath, tempPath);
        }
示例#3
0
        public static void PreprocessBuild(BuildPlayerOptions options)
        {
            config = Configuration.LoadOrDefault <ResourceProviderConfiguration>();

            useAddressables = AddressableHelper.Available && config.UseAddressables;
            if (!useAddressables)
            {
                Debug.Log("Consider installing the Addressable Asset System (via Unity's package manager) and enabling `Use Addressables` in the Naninovel's `Resource Provider` configuration menu. When the system is not available, all the assets assigned as Naninovel resources and not stored in `Resources` folders will be copied and re-imported when building the player, which could significantly increase the build time.");
            }

            if (useAddressables)
            {
                AddressableHelper.RemovePreviousEntries();
            }

            EditorUtils.CreateFolderAsset(tempResourcesPath);

            var records          = EditorResources.LoadOrDefault().GetAllRecords();
            var projectResources = ProjectResources.Get();
            var progress         = 0;

            foreach (var record in records)
            {
                progress++;

                var resourcePath      = record.Key;
                var assetGuid         = record.Value;
                var resourceAssetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
                if (string.IsNullOrEmpty(resourceAssetPath) || !EditorUtils.AssetExistsByPath(resourceAssetPath))
                {
                    Debug.LogWarning($"Failed to resolve `{resourcePath}` asset path from GUID stored in `EditorResources` asset. The resource won't be included to the build.");
                    continue;
                }

                var resourceAsset = AssetDatabase.LoadAssetAtPath <Object>(resourceAssetPath);
                if (string.IsNullOrEmpty(resourceAssetPath))
                {
                    Debug.LogWarning($"Failed to load `{resourcePath}` asset. The resource won't be included to the build.");
                    continue;
                }

                if (EditorUtility.DisplayCancelableProgressBar("Processing Naninovel Resources", $"Processing '{resourceAssetPath}' asset...", progress / (float)records.Count))
                {
                    PostprocessBuild(); // Remove temporary assets.
                    throw new System.OperationCanceledException("Build was cancelled by the user.");
                }

                if (resourceAsset is SceneAsset)
                {
                    ProcessSceneResource(resourcePath, resourceAsset as SceneAsset);
                }
                else if (resourceAsset is UnityEngine.Video.VideoClip && options.target == BuildTarget.WebGL)
                {
                    ProcessVideoResourceForWebGL(resourcePath, resourceAsset);
                }
                else
                {
                    ProcessResourceAsset(assetGuid, resourcePath, resourceAsset, projectResources);
                }
            }

            AssetDatabase.SaveAssets();

            if (useAddressables && config.AutoBuildBundles)
            {
                EditorUtility.DisplayProgressBar("Processing Naninovel Resources", "Building asset bundles...", 1f);
                AddressableHelper.RebuildPlayerContent();
            }

            EditorUtility.ClearProgressBar();
        }