using System.Collections; using System.Collections.Generic; using UnityEngine; public class BuiltInLoader { readonly static string PREFAB_EXTENSION = ".prefab"; readonly static string SPRITE_EXTENSION = ".png"; readonly static string AUDIO_EXTENSION = ".wav"; public static Sprite LoadSprite(string name) { Sprite sprite = null; if (AssetSource.builtInFromEditor) { #if UNITY_EDITOR var path = StringUtility.Contact("Assets/ResourcesOut/BuiltIn/Sprites/", name, SPRITE_EXTENSION); sprite = UnityEditor.AssetDatabase.LoadAssetAtPath(path); #endif } else { var assetInfo = new AssetInfo("builtin/sprites", name); sprite = AssetBundleUtility.Instance.Sync_LoadAsset(assetInfo, typeof(Sprite)) as Sprite; } if (sprite == null) { DebugEx.LogErrorFormat("BuiltInLoader.LoadSprite() => 加载不到资源: {0}.", name); } return sprite; } public static GameObject LoadPrefab(string name) { GameObject prefab = null; if (AssetSource.builtInFromEditor) { #if UNITY_EDITOR var path = StringUtility.Contact("Assets/ResourcesOut/BuiltIn/Prefabs/", name, PREFAB_EXTENSION); prefab = UnityEditor.AssetDatabase.LoadAssetAtPath(path); #endif } else { var assetInfo = new AssetInfo("builtin/prefabs", name); prefab = AssetBundleUtility.Instance.Sync_LoadAsset(assetInfo) as GameObject; } if (prefab == null) { DebugEx.LogErrorFormat("BuiltInLoader.LoadPrefab() => 加载不到资源: {0}.", name); } return prefab; } public static void UnLoadPrefab(string name) { if (!AssetSource.builtInFromEditor) { AssetBundleUtility.Instance.UnloadAsset("builtin/prefabs", name); } } public static AudioClip LoadMusic(string name) { AudioClip audioClip = null; if (AssetSource.builtInFromEditor) { #if UNITY_EDITOR var path = StringUtility.Contact("Assets/ResourcesOut/BuiltIn/Musics/", name, AUDIO_EXTENSION); audioClip = UnityEditor.AssetDatabase.LoadAssetAtPath(path); #endif } else { var assetInfo = new AssetInfo("builtin/musics", name); audioClip = AssetBundleUtility.Instance.Sync_LoadAsset(assetInfo) as AudioClip; } if (audioClip == null) { DebugEx.LogErrorFormat("BuiltInLoader.LoadMusic() => 加载不到资源: {0}.", name); } return audioClip; } public static AnimationClip LoadAnimationClip(string name) { AnimationClip clip = null; if (AssetSource.builtInFromEditor) { #if UNITY_EDITOR var path = StringUtility.Contact("Assets/ResourcesOut/BuiltIn/AnimationClips/", name, ".anim"); clip = UnityEditor.AssetDatabase.LoadAssetAtPath(path); #endif } else { var assetInfo = new AssetInfo("builtin/animationclips", name); clip = AssetBundleUtility.Instance.Sync_LoadAsset(assetInfo) as AnimationClip; } if (clip == null) { DebugEx.LogErrorFormat("BuiltInLoader.LoadAnimationClip() => 加载不到资源: {0}.", name); } return clip; } public static Material LoadMaterial(string name) { Material material = null; if (AssetSource.builtInFromEditor) { #if UNITY_EDITOR var path = StringUtility.Contact("Assets/ResourcesOut/BuiltIn/Materials/", name, ".mat"); material = UnityEditor.AssetDatabase.LoadAssetAtPath(path); #endif } else { var assetInfo = new AssetInfo("builtin/materials", name); material = AssetBundleUtility.Instance.Sync_LoadAsset(assetInfo) as Material; } if (material == null) { DebugEx.LogErrorFormat("BuiltInLoader.LoadMaterial() => 加载不到资源: {0}.", name); } return material; } public static T LoadScriptableObject(string name) where T : ScriptableObject { T config = null; if (AssetSource.builtInFromEditor) { #if UNITY_EDITOR var resourcePath = StringUtility.Contact(ResourcesPath.ResourcesOutAssetPath, "BuiltIn/ScriptableObjects/", name, ".asset"); config = UnityEditor.AssetDatabase.LoadAssetAtPath(resourcePath); #endif } else { var assetInfo = new AssetInfo("builtin/scriptableobjects", name); config = AssetBundleUtility.Instance.Sync_LoadAsset(assetInfo) as T; } if (config == null) { Debug.LogErrorFormat("BuiltIn.LoadScriptableObject() => 加载不到资源: {0}.", name); } return config; } }