コード例 #1
0
        public void OnPreprocessBuild(BuildReport report)
        {
            var asset = ProjectResources.Get();

            EditorUtils.CreateFolderAsset(assetPath.GetBeforeLast("/"));
            AssetDatabase.CreateAsset(asset, assetPath);
            AssetDatabase.SaveAssets();
        }
コード例 #2
0
        public ProjectResourceProvider(string rootPath = null)
        {
            RootPath         = rootPath;
            projectResources = GetProjectResources();
            redirectors      = new Dictionary <Type, TypeRedirector>();
            foreach (var kv in projectResources)
            {
                LocationsCache.Add(new CachedResourceLocation(kv.Key, kv.Value));
            }

            IReadOnlyDictionary <string, Type> GetProjectResources()
            {
                var filter = string.IsNullOrEmpty(RootPath) ? null : $"{RootPath}/";

                return(ProjectResources.Get().GetAllResources(filter));
            }
        }
コード例 #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();
        }
コード例 #4
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);
        }
コード例 #5
0
        public static List <Folder> LocateProjectFolders(string rootPath, string resourcesPath, ProjectResources projectResources)
        {
            var path = string.IsNullOrEmpty(rootPath) ? resourcesPath : string.IsNullOrEmpty(resourcesPath) ? rootPath : $"{rootPath}/{resourcesPath}";

            return(projectResources.ResourcePaths.LocateFolderPathsAtFolder(path)
                   .Select(p => new Folder(string.IsNullOrEmpty(rootPath) ? p : p.GetAfterFirst(rootPath + "/"))).ToList());
        }
コード例 #6
0
 public ProjectFolderLocator(IResourceProvider provider, string rootPath, string resourcesPath, ProjectResources projectResources)
     : base(provider, resourcesPath ?? string.Empty)
 {
     RootPath = rootPath;
     this.projectResources = projectResources;
 }
コード例 #7
0
 public ProjectResourceProvider(string rootPath = null)
 {
     projectResources = ProjectResources.Get();
     redirectors      = new Dictionary <Type, TypeRedirector>();
     RootPath         = rootPath;
 }
コード例 #8
0
        public static IReadOnlyCollection <string> LocateProjectResources(string rootPath, string resourcesPath, ProjectResources projectResources)
        {
            var path   = string.IsNullOrEmpty(rootPath) ? resourcesPath : string.IsNullOrEmpty(resourcesPath) ? rootPath : $"{rootPath}/{resourcesPath}";
            var result = projectResources.ResourcePaths.LocateResourcePathsAtFolder(path);

            if (!string.IsNullOrEmpty(rootPath))
            {
                return(result.Select(p => p.GetAfterFirst(rootPath + "/")).ToArray());
            }
            return(result.ToArray());
        }