Ejemplo n.º 1
0
    // 检查资源是否存在
    public static bool ContainsResourceUrl(string resourceUrl,
                                           KResourceInAppPathType inAppPathType = KResourceInAppPathType.StreamingAssetsPath)
    {
        string fullPath;

        return(GetResourceFullPath(resourceUrl, out fullPath, inAppPathType, false));
    }
Ejemplo n.º 2
0
    protected override void Init(string url, params object[] args)
    {
        base.Init(url);

        _loaderMode = (KAssetBundleLoaderMode)args[0];

        // 如果是默认模式,则要判断ResourceModule.InAppPathType的默认为依据
        if (_loaderMode == KAssetBundleLoaderMode.Default)
        {
            _inAppPathType = KResourceModule.DefaultInAppPathType;
            switch (_inAppPathType)
            {
            case KResourceInAppPathType.StreamingAssetsPath:
                _loaderMode = KAssetBundleLoaderMode.StreamingAssetsWww;
                break;

            case KResourceInAppPathType.ResourcesAssetsPath:
                _loaderMode = KAssetBundleLoaderMode.ResourcesLoad;
                break;

            case KResourceInAppPathType.PersistentAssetsPath:
                _loaderMode = KAssetBundleLoaderMode.PersitentDataPathSync;
                break;

            default:
                Logger.LogError("Error DefaultInAppPathType: {0}", _inAppPathType);
                break;
            }
        }
        // 不同的AssetBundle加载方式,对应不同的路径
        switch (_loaderMode)
        {
        case KAssetBundleLoaderMode.ResourcesLoad:
        case KAssetBundleLoaderMode.ResourcesLoadAsync:
            _inAppPathType = KResourceInAppPathType.ResourcesAssetsPath;
            break;

        case KAssetBundleLoaderMode.StreamingAssetsWww:
            _inAppPathType = KResourceInAppPathType.StreamingAssetsPath;
            break;

        case KAssetBundleLoaderMode.PersitentDataPathSync:
            _inAppPathType = KResourceInAppPathType.PersistentAssetsPath;
            break;

        default:
            Logger.LogError("[KAssetBundleLoader:Init]Unknow loader mode: {0}", _loaderMode);
            break;
        }


        if (NewAssetBundleLoaderEvent != null)
        {
            NewAssetBundleLoaderEvent(url);
        }

        RelativeResourceUrl = url;
        KResourceModule.LogRequest("AssetBundle", RelativeResourceUrl);
        KResourceModule.Instance.StartCoroutine(LoadAssetBundle(url));
    }
Ejemplo n.º 3
0
    protected override void Init(string url, params object[] args)
    {
        base.Init(url, args);

        _inAppPathType = (KResourceInAppPathType)args[0];
        _loaderMode    = (KAssetBundleLoaderMode)args[1];
        KResourceModule.Instance.StartCoroutine(CoLoad(url, _inAppPathType, _loaderMode));
    }
Ejemplo n.º 4
0
    /// <summary>
    /// 完整路径,www加载
    /// </summary>
    /// <param name="url"></param>
    /// <param name="inAppPathType"></param>
    /// <param name="isLog"></param>
    /// <returns></returns>
    public static string GetResourceFullPath(string url,
                                             KResourceInAppPathType inAppPathType = KResourceInAppPathType.Default, bool isLog = true)
    {
        string fullPath;

        if (GetResourceFullPath(url, out fullPath, inAppPathType, isLog))
        {
            return(fullPath);
        }

        return(null);
    }
Ejemplo n.º 5
0
    private IEnumerator CoLoad(string url, KResourceInAppPathType type, KAssetBundleLoaderMode loaderMode)
    {
        if (KResourceModule.GetResourceFullPath(url, out FullUrl, _inAppPathType))
        {
        }
        else
        {
            if (Debug.isDebugBuild)
            {
                Logger.LogError("[KBytesLoader]Error Path: {0}", url);
            }
            OnFinish(null);
        }
        if (_inAppPathType == KResourceInAppPathType.PersistentAssetsPath)
        {
            Progress = .5f;
            Bytes    = File.ReadAllBytes(FullUrl);
        }
        else if (_inAppPathType == KResourceInAppPathType.StreamingAssetsPath)
        {
            _wwwLoader = KWWWLoader.Load(FullUrl);
            while (!_wwwLoader.IsCompleted)
            {
                Progress = _wwwLoader.Progress / 2f; // 最多50%, 要算上Parser的嘛
                yield return(null);
            }

            if (!_wwwLoader.IsSuccess)
            {
                //if (AssetBundlerLoaderErrorEvent != null)
                //{
                //    AssetBundlerLoaderErrorEvent(this);
                //}
                Logger.LogError("[KBytesLoader]Error Load WWW: {0}", url);
                OnFinish(null);
                yield break;
            }

            Bytes = _wwwLoader.Www.bytes;
        }
        else if (_inAppPathType == KResourceInAppPathType.ResourcesAssetsPath) // 使用Resources文件夹模式
        {
            var pathExt        = Path.GetExtension(FullUrl);                   // Resources.Load无需扩展名
            var pathWithoutExt = FullUrl.Substring(0,
                                                   FullUrl.Length - pathExt.Length);
            if (_loaderMode == KAssetBundleLoaderMode.ResourcesLoad)
            {
                var textAsset = Resources.Load <TextAsset>(pathWithoutExt);
                if (textAsset == null)
                {
                    Logger.LogError("[KBytesLoader]Cannot Resources.Load from : {0}", pathWithoutExt);
                    OnFinish(null);
                    yield break;
                }

                Bytes = textAsset.bytes;
                Resources.UnloadAsset(textAsset);
            }
            else if (_loaderMode == KAssetBundleLoaderMode.ResourcesLoadAsync)
            {
                var loadReq = Resources.LoadAsync <TextAsset>(pathWithoutExt);
                while (!loadReq.isDone)
                {
                    Progress = loadReq.progress / 2f; // 最多50%, 要算上Parser的嘛
                }
                var loadAsset     = loadReq.asset;
                var loadTextAsset = loadAsset as TextAsset;
                if (loadTextAsset == null)
                {
                    Logger.LogError("[KBytesLoader]Error Resources.LoadAsync: {0}", url);
                    OnFinish(null);
                    yield break;
                }
                Bytes = loadTextAsset.bytes;
                Resources.UnloadAsset(loadTextAsset);
            }
            else
            {
                Logger.LogError("[KBytesLoader]Unvalid LoaderMode on Resources Load Mode: {0}", _loaderMode);
                OnFinish(null);
                yield break;
            }
        }
        else
        {
            Logger.LogError("[KBytesLoader]Error InAppPathType: {0}", KResourceModule.DefaultInAppPathType);
            OnFinish(null);
            yield break;
        }
        OnFinish(Bytes);
    }
Ejemplo n.º 6
0
    public static KBytesLoader Load(string path, KResourceInAppPathType inAppPathType, KAssetBundleLoaderMode loaderMode)
    {
        var newLoader = AutoNew <KBytesLoader>(path, null, false, inAppPathType, loaderMode);

        return(newLoader);
    }
Ejemplo n.º 7
0
    protected override void Init(string url, params object[] args)
    {
        base.Init(url);

        _loaderMode = (KAssetBundleLoaderMode) args[0];

        // 如果是默认模式,则要判断ResourceModule.InAppPathType的默认为依据
        if (_loaderMode == KAssetBundleLoaderMode.Default)
        {
            _inAppPathType = KResourceModule.DefaultInAppPathType;
            switch (_inAppPathType)
            {
                case KResourceInAppPathType.StreamingAssetsPath:
                    _loaderMode = KAssetBundleLoaderMode.StreamingAssetsWww;
                    break;
                case KResourceInAppPathType.ResourcesAssetsPath:
                    _loaderMode = KAssetBundleLoaderMode.ResourcesLoad;
                    break;
                default:
                    Logger.LogError("Error DefaultInAppPathType: {0}", _inAppPathType);
                    break;
            }
        }
        // 不同的AssetBundle加载方式,对应不同的路径
        switch (_loaderMode)
        {
            case KAssetBundleLoaderMode.ResourcesLoad:
            case KAssetBundleLoaderMode.ResourcesLoadAsync:
                _inAppPathType = KResourceInAppPathType.ResourcesAssetsPath;
                break;
            case KAssetBundleLoaderMode.StreamingAssetsWww:
                _inAppPathType = KResourceInAppPathType.StreamingAssetsPath;
                break;
            default:
                Logger.LogError("[KAssetBundleLoader:Init]Unknow loader mode: {0}", _loaderMode);
                break;
        }


        if (NewAssetBundleLoaderEvent != null)
            NewAssetBundleLoaderEvent(url);

        RelativeResourceUrl = url;
        if (KResourceModule.GetResourceFullPath(url, out FullUrl, _inAppPathType))
        {
            KResourceModule.LogRequest("AssetBundle", FullUrl);
            KResourceModule.Instance.StartCoroutine(LoadAssetBundle(url));
        }
        else
        {
            if (Debug.isDebugBuild)
                Logger.LogError("[KAssetBundleLoader]Error Path: {0}", url);
            OnFinish(null);
        }
    }
Ejemplo n.º 8
0
    /// <summary>
    /// 根据相对路径,获取到StreamingAssets完整路径,或Resources中的路径
    /// </summary>
    /// <param name="url"></param>
    /// <param name="fullPath"></param>
    /// <param name="inAppPathType"></param>
    /// <param name="isLog"></param>
    /// <returns></returns>
    public static bool GetResourceFullPath(string url, out string fullPath,
                                           KResourceInAppPathType inAppPathType = KResourceInAppPathType.Default, bool isLog = true)
    {
        if (string.IsNullOrEmpty(url))
        {
            Logger.LogError("尝试获取一个空的资源路径!");
        }

        if (inAppPathType == KResourceInAppPathType.Default)
        {
            inAppPathType = DefaultInAppPathType;
        }

        string docUrl;
        bool   hasDocUrl = TryGetDocumentResourceUrl(url, out docUrl);

        string inAppUrl;
        bool   hasInAppUrl;

        if (inAppPathType == KResourceInAppPathType.StreamingAssetsPath)
        {
            hasInAppUrl = TryGetInAppStreamingUrl(url, out inAppUrl);
        }
        else if (inAppPathType == KResourceInAppPathType.ResourcesAssetsPath)
        {
            hasInAppUrl = TryGetInAppResourcesFolderUrl(url, out inAppUrl); // 使用Resources某
        }
        else if (inAppPathType == KResourceInAppPathType.PersistentAssetsPath)
        {
            hasInAppUrl = TryGetPersitentResourceUrl(url, out inAppUrl);
        }
        else
        {
            Logger.LogError("[GetResourceFullPath]Invalid InAppPathType: {0}", DefaultInAppPathType);
            hasInAppUrl = false;
            inAppUrl    = null;
        }

        if (ResourcePathPriorityType == KResourcePathPriorityType.PersistentDataPathPriority) // 優先下載資源模式
        {
            if (hasDocUrl)
            {
                if (Application.isEditor)
                {
                    Logger.LogWarning("[Use PersistentDataPath] {0}", docUrl);
                }
                fullPath = docUrl;
                return(true);
            }
            // 優先下載資源,但又沒有下載資源文件!使用本地資源目錄
        }

        if (!hasInAppUrl) // 连本地资源都没有,直接失败吧 ?? 沒有本地資源但又遠程資源?竟然!!?
        {
            if (isLog)
            {
                Logger.LogError("[Not Found] StreamingAssetsPath Url Resource: {0}", url);
            }
            fullPath = null;
            return(false);
        }

        fullPath = inAppUrl; // 直接使用本地資源!

        return(true);
    }
Ejemplo n.º 9
0
    protected override void Init(string url, params object[] args)
    {
        base.Init(url);

        _loaderMode = (KAssetBundleLoaderMode)args[0];

        // 如果是默认模式,则要判断ResourceModule.InAppPathType的默认为依据
        if (_loaderMode == KAssetBundleLoaderMode.Default)
        {
            _inAppPathType = KResourceModule.DefaultInAppPathType;
            switch (_inAppPathType)
            {
            case KResourceInAppPathType.StreamingAssetsPath:
                _loaderMode = KAssetBundleLoaderMode.StreamingAssetsWww;
                break;

            case KResourceInAppPathType.ResourcesAssetsPath:
                _loaderMode = KAssetBundleLoaderMode.ResourcesLoad;
                break;

            default:
                Logger.LogError("Error DefaultInAppPathType: {0}", _inAppPathType);
                break;
            }
        }
        // 不同的AssetBundle加载方式,对应不同的路径
        switch (_loaderMode)
        {
        case KAssetBundleLoaderMode.ResourcesLoad:
        case KAssetBundleLoaderMode.ResourcesLoadAsync:
            _inAppPathType = KResourceInAppPathType.ResourcesAssetsPath;
            break;

        case KAssetBundleLoaderMode.StreamingAssetsWww:
            _inAppPathType = KResourceInAppPathType.StreamingAssetsPath;
            break;

        default:
            Logger.LogError("[KAssetBundleLoader:Init]Unknow loader mode: {0}", _loaderMode);
            break;
        }


        if (NewAssetBundleLoaderEvent != null)
        {
            NewAssetBundleLoaderEvent(url);
        }

        RelativeResourceUrl = url;
        if (KResourceModule.GetResourceFullPath(url, out FullUrl, _inAppPathType))
        {
            KResourceModule.LogRequest("AssetBundle", FullUrl);
            KResourceModule.Instance.StartCoroutine(LoadAssetBundle(url));
        }
        else
        {
            if (Debug.isDebugBuild)
            {
                Logger.LogError("[KAssetBundleLoader]Error Path: {0}", url);
            }
            OnFinish(null);
        }
    }
Ejemplo n.º 10
0
    /// <summary>
    /// 根据相对路径,获取到StreamingAssets完整路径,或Resources中的路径
    /// </summary>
    /// <param name="url"></param>
    /// <param name="fullPath"></param>
    /// <param name="inAppPathType"></param>
    /// <param name="isLog"></param>
    /// <returns></returns>
    public static bool GetResourceFullPath(string url, out string fullPath,
        KResourceInAppPathType inAppPathType = KResourceInAppPathType.Default, bool isLog = true)
    {
        if (string.IsNullOrEmpty(url))
            Logger.LogError("尝试获取一个空的资源路径!");

        if (inAppPathType == KResourceInAppPathType.Default)
            inAppPathType = DefaultInAppPathType;

        string docUrl;
        bool hasDocUrl = TryGetDocumentResourceUrl(url, out docUrl);

        string inAppUrl;
        bool hasInAppUrl;
        if (inAppPathType == KResourceInAppPathType.StreamingAssetsPath)
        {
            hasInAppUrl = TryGetInAppStreamingUrl(url, out inAppUrl);
        }
        else if (inAppPathType == KResourceInAppPathType.ResourcesAssetsPath)
        {
            hasInAppUrl = TryGetInAppResourcesFolderUrl(url, out inAppUrl); // 使用Resources某
        }
        else
        {
            Logger.LogError("[GetResourceFullPath]Invalid InAppPathType: {0}", DefaultInAppPathType);
            hasInAppUrl = false;
            inAppUrl = null;
        }

        if (ResourcePathPriorityType == KResourcePathPriorityType.PersistentDataPathPriority) // 優先下載資源模式
        {
            if (hasDocUrl)
            {
                if (Application.isEditor)
                    Logger.LogWarning("[Use PersistentDataPath] {0}", docUrl);
                fullPath = docUrl;
                return true;
            }
            // 優先下載資源,但又沒有下載資源文件!使用本地資源目錄 
        }

        if (!hasInAppUrl) // 连本地资源都没有,直接失败吧 ?? 沒有本地資源但又遠程資源?竟然!!?
        {
            if (isLog)
                Logger.LogError("[Not Found] StreamingAssetsPath Url Resource: {0}", url);
            fullPath = null;
            return false;
        }

        fullPath = inAppUrl; // 直接使用本地資源!

        return true;
    }
Ejemplo n.º 11
0
    /// <summary>
    /// 完整路径,www加载
    /// </summary>
    /// <param name="url"></param>
    /// <param name="inAppPathType"></param>
    /// <param name="isLog"></param>
    /// <returns></returns>
    public static string GetResourceFullPath(string url,
        KResourceInAppPathType inAppPathType = KResourceInAppPathType.StreamingAssetsPath, bool isLog = true)
    {
        string fullPath;
        if (GetResourceFullPath(url, out fullPath, inAppPathType, isLog))
            return fullPath;

        return null;
    }
Ejemplo n.º 12
0
 // 检查资源是否存在
 public static bool ContainsResourceUrl(string resourceUrl,
     KResourceInAppPathType inAppPathType = KResourceInAppPathType.StreamingAssetsPath)
 {
     string fullPath;
     return GetResourceFullPath(resourceUrl, out fullPath, inAppPathType, false);
 }