Beispiel #1
0
    public static Object                Load(string path, System.Type type)
    {
        if (string.IsNullOrEmpty(path))
        {
            GLog.LogError("[XRes.Load] sent empty/null path!");
            return(null);
        }

        string name = path.ToLower();

        Object asset = Find(name);

        if (!asset)
        {
            XSheet.XAssetInfo info = XSheet.Instance.Find(name);
            if (info != null)
            {
                switch (info.locationType)
                {
                case XSheet.XLocationType.Resource:
                    if (type == typeof(XBufferAsset))
                    {
                        XBufferAsset bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>();
                        if (bufferAsset)
                        {
                            bufferAsset.init(
                                Resources.Load <TextAsset>(name)
                                );
                            asset = bufferAsset;
                        }
                    }
                    else
                    {
                        asset = Resources.Load(name, type);
                    }
                    break;

                case XSheet.XLocationType.Bundle:
                    GLog.LogError("[XRes.Load] can't load bundle in sync load {0}", name);
                    break;

                case XSheet.XLocationType.Data:
                    string filePath = System.IO.Path.Combine(Application.persistentDataPath, info.fullName);
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.FileStream fs = System.IO.File.OpenRead(filePath);
                        if (fs.CanRead)
                        {
                            XBufferAsset bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>();
                            if (bufferAsset)
                            {
                                bufferAsset.init((int)fs.Length);
                                fs.Read(
                                    bufferAsset.bytes, 0, (int)fs.Length
                                    );
                                fs.Close();

                                if (type == typeof(Texture2D))
                                {
                                    Texture2D buffTexture = new Texture2D(1, 1);
                                    buffTexture.LoadImage(bufferAsset.bytes);

                                    asset = buffTexture;
                                }
                                else
                                {
                                    asset = bufferAsset;
                                }
                            }
                        }
                    }
                    break;

                default:
                    GLog.LogError("[XRes] no imp resource type {0}", info.locationType.ToString());
                    break;
                }
            }
        }
        else
        {
            if (type == typeof(XBufferAsset))
            {
                XBufferAsset bufferAsset = ScriptableObject.CreateInstance <XBufferAsset>();
                bufferAsset.init(
                    Resources.Load <TextAsset>(name)
                    );

                asset = bufferAsset;
            }
            else
            {
                asset = Resources.Load(name, type);
            }
        }


        return(asset);
    }
Beispiel #2
0
    /// <summary>
    /// Dos the load async.
    /// </summary>
    /// <returns>The load async.</returns>
    /// <param name="path">Path.</param>
    /// <param name="type">Type.</param>
    /// <param name="callback">Callback.</param>
    private static IEnumerator  DoLoadAsync(string path, System.Type type, System.Action <Object> callback)
    {
        if (string.IsNullOrEmpty(path))
        {
            GLog.LogError("[XRes::Load] sent empty/null path!");
            yield break;
        }

        string name = path.ToLower();

        UnityEngine.Object obj = Find(name);
        if (!obj)
        {
            XSheet.XAssetInfo info = XSheet.Instance.Find(name);
            if (info != null)
            {
                switch (info.locationType)
                {
                case XSheet.XLocationType.Resource:
                    ResourceRequest request = null;
                    if (type == typeof(XBufferAsset))
                    {
                        request = Resources.LoadAsync <TextAsset>(name);
                        yield return(request);

                        XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>();
                        asset.init((TextAsset)request.asset);
                        obj = asset;
                    }
                    else
                    {
                        request = Resources.LoadAsync(name, type);
                        yield return(request);

                        obj = request.asset;
                    }
                    break;

                case XSheet.XLocationType.Bundle:
                    yield return(XBundleManager.Instance.LoadAssetAsync(info,
                                                                        type, delegate(Object result) { obj = result; }));

                    break;

                case XSheet.XLocationType.Data:
                    string fileUrl    = XSheet.GetLocalFileUrl(System.IO.Path.Combine(Application.persistentDataPath, info.fullName));
                    WWW    fileLoader = new WWW(fileUrl);
                    yield return(fileLoader);

                    if (!string.IsNullOrEmpty(fileLoader.error))
                    {
                        XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>();
                        asset.init(
                            fileLoader.bytes
                            );

                        if (type == typeof(Texture2D))
                        {
                            Texture2D texture = new Texture2D(1, 1);
                            texture.LoadImage(asset.bytes);

                            obj = texture;
                        }
                        else
                        {
                            obj = asset;
                        }
                    }
                    break;

                case XSheet.XLocationType.Streaming:
                    string streamUrl = XSheet.GetLocalFileUrl(System.IO.Path.Combine(Application.streamingAssetsPath, info.fullName));
                    if (Application.platform != RuntimePlatform.Android)
                    {
                        streamUrl = XSheet.GetLocalFileUrl(streamUrl);
                    }

                    WWW streamLoader = new WWW(streamUrl);
                    yield return(streamLoader);

                    if (!string.IsNullOrEmpty(streamLoader.error))
                    {
                        XBufferAsset asset = ScriptableObject.CreateInstance <XBufferAsset>();
                        asset.init(
                            streamLoader.bytes
                            );

                        if (type == typeof(Texture2D))
                        {
                            Texture2D texture = new Texture2D(1, 1);
                            texture.LoadImage(asset.bytes);
                            obj = texture;
                        }
                        else
                        {
                            obj = asset;
                        }
                    }
                    break;
                }
            }
            else
            {
                ResourceRequest request = Resources.LoadAsync(name, type);
                yield return(request);

                obj = request.asset;
            }

            if (!obj)
            {
                if (info != null)
                {
                    GLog.LogError("[XRes.Load] Can't find {0} in Location({1})", name, info.locationType);
                }
                else
                {
                    GLog.LogError("[XRes.Load] Can't find {0} in Resources", name);
                }
            }
        }

        callback(obj);
    }