/*      //put similar code in your project to use this window
 *      [MenuItem("Window/ResourceManager Build Settings", priority = 2060)]
 *      static void ShowWindow()
 *      {
 *          var window = GetWindow<ResourceManagerSettings>();
 *          window.titleContent = new GUIContent("RM Settings");
 *          window.Show();
 *      }
 */
        private void OnGUI()
        {
            var tpath    = EditorPrefs.GetString("RMTargetFolder", "Assets/Prefabs");
            var tnewPath = EditorGUILayout.DelayedTextField("Target Asset Folder", tpath);

            if (tpath != tnewPath)
            {
                EditorPrefs.SetString("RMTargetFolder", tnewPath);
            }

            var ext    = EditorPrefs.GetString("RMTargetExtension", "*.prefab");
            var newExt = EditorGUILayout.DelayedTextField("Target Asset Extension", ext);

            if (ext != newExt)
            {
                EditorPrefs.SetString("RMTargetExtension", newExt);
            }


            int mode = EditorPrefs.GetInt("RMProviderMode", 0);

            ResourceManagerRuntimeData.ProviderMode val = (ResourceManagerRuntimeData.ProviderMode)EditorGUILayout.EnumPopup("Provider Mode", (ResourceManagerRuntimeData.ProviderMode)mode);
            if (mode != (int)val)
            {
                EditorPrefs.SetInt("RMProviderMode", (int)val);
            }

            bool connectProfiler    = EditorPrefs.GetBool("RMProfileEvents", false);
            bool setConnectProfiler = EditorGUILayout.ToggleLeft("Profile Events", connectProfiler);

            if (setConnectProfiler != connectProfiler)
            {
                EditorPrefs.SetBool("RMProfileEvents", setConnectProfiler);
            }

            var path    = EditorPrefs.GetString("RMBundlePath", "Assets/StreamingAssets");
            var newPath = EditorGUILayout.DelayedTextField("AssetBundle Path", path);

            if (path != newPath)
            {
                EditorPrefs.SetString("RMBundlePath", newPath);
            }

            var lpath    = EditorPrefs.GetString("RMBundleLoadPrefix", "{Application.streamingAssetsPath}/");
            var lnewPath = EditorGUILayout.DelayedTextField("AssetBundle Load Prefix", lpath);

            if (lpath != lnewPath)
            {
                EditorPrefs.SetString("RMBundleLoadPrefix", lnewPath);
            }
        }
        //create ResourceManagerRuntimeData for the player to use
        //in the player, call ResourceManagerRuntimeData.Initialize() to load and init RM with this data
        static void PrepareRuntimeData(ResourceManagerRuntimeData.ProviderMode mode, bool isDevBuild, BuildTargetGroup targetGroup, BuildTarget target)
        {
            ResourceManagerRuntimeData runtimeData = new ResourceManagerRuntimeData();

            runtimeData.resourceProviderMode = mode;
            runtimeData.profileEvents        = EditorPrefs.GetBool("RMProfileEvents", false);

            //replace this with code specific to your project that knows how to gather all assets that need to be loadable at runtime
            List <string> paths = new List <string>(Directory.GetFiles(EditorPrefs.GetString("RMTargetFolder", "Assets/Prefabs"), EditorPrefs.GetString("RMTargetExtension", "*.prefab"), SearchOption.AllDirectories));

            //create locations for all assets
            if (runtimeData.resourceProviderMode == ResourceManagerRuntimeData.ProviderMode.FastMode)
            {
                foreach (var p in paths)
                {
                    //for this mode just create locations for the AssetDatabase
                    var path = p.Replace('\\', '/');
                    runtimeData.locationData.Add(new ResourceManagerRuntimeData.ResourceLocationData(
                                                     Path.GetFileNameWithoutExtension(path),
                                                     AssetDatabase.AssetPathToGUID(path),
                                                     path, typeof(AssetDatabaseProvider).FullName, null));
                }
            }
            else
            {
                var    assetBundleBuilds = new List <AssetBundleBuild>();
                string bundleBuildPath   = EditorPrefs.GetString("RMBundlePath", "Assets/StreamingAssets");
                string bundleLoadPrefix  = EditorPrefs.GetString("RMBundleLoadPrefix", "{Application.streamingAssetsPath}/");
                foreach (var p in paths)
                {
                    //create locations for bundled assets
                    var    path       = p.Replace('\\', '/');
                    string name       = Path.GetFileNameWithoutExtension(path);
                    string bundleName = (name + ".bundle").ToLower();
                    assetBundleBuilds.Add(new AssetBundleBuild()
                    {
                        assetBundleName = bundleName, assetNames = new string[] { path }
                    });
                    runtimeData.locationData.Add(new ResourceManagerRuntimeData.ResourceLocationData(
                                                     name, AssetDatabase.AssetPathToGUID(path), path, typeof(BundledAssetProvider).FullName, new string[] { bundleName }));
                    if (runtimeData.resourceProviderMode == ResourceManagerRuntimeData.ProviderMode.VirtualBundles)
                    {   //if simulating bundles, go ahead and create locations for bundles since they won't actually be created
                        runtimeData.locationData.Add(new ResourceManagerRuntimeData.ResourceLocationData(
                                                         bundleName, "", bundleLoadPrefix + bundleName, typeof(LocalAssetBundleProvider).FullName, new string[] {}));
                    }
                }
                if (runtimeData.resourceProviderMode == ResourceManagerRuntimeData.ProviderMode.AssetBundles)
                {   //build real bundles and then extract the locations
                    var manifest = BuildPipeline.BuildAssetBundles(bundleBuildPath, assetBundleBuilds.ToArray(), BuildAssetBundleOptions.UncompressedAssetBundle, target);
                    foreach (var bundleName in manifest.GetAllAssetBundles())
                    {
                        runtimeData.locationData.Add(new ResourceManagerRuntimeData.ResourceLocationData(
                                                         bundleName, "", bundleLoadPrefix + bundleName, typeof(LocalAssetBundleProvider).FullName, manifest.GetAllDependencies(bundleName)));
                    }
                }
            }

            // need to add all scenes to EditorBuildSettings.scenes so they can be loaded in the editor when in play mode
            if (runtimeData.resourceProviderMode != ResourceManagerRuntimeData.ProviderMode.AssetBundles)
            {
                var scenes = new List <EditorBuildSettingsScene>(EditorBuildSettings.scenes);
                scenes.AddRange(EditorBuildSettings.scenes);
                foreach (var p in paths)
                {
                    if (p.EndsWith(".unity"))
                    {
                        scenes.Add(new EditorBuildSettingsScene(new GUID(AssetDatabase.AssetPathToGUID(p)), true));
                    }
                }
                EditorBuildSettings.scenes = scenes.ToArray();
            }

            //extract data for virtual bundles if needed
            if (runtimeData.resourceProviderMode == ResourceManagerRuntimeData.ProviderMode.VirtualBundles)
            {
                CreateVirtualBundleData(runtimeData);
            }

            runtimeData.Save();
        }