Пример #1
0
    //异步读取回调处理方法
    private static void AsyncReadCallback(IAsyncResult asyncResult)
    {
        AsyncResourceState asyncState = (AsyncResourceState)asyncResult.AsyncState;
        int readCn = asyncState.FS.EndRead(asyncResult);

        //判断是否读到内容
        if (readCn == asyncState.Buffer.Length)
        {
            UnityEngine.Object retobj = LoadByType(asyncState.Info, asyncState.Buffer, asyncState.T);
            loadedlist.Add(asyncState.Info);
            if (asyncState.Handler != null)
            {
                asyncState.Handler(asyncState.ResourcePath, retobj);
            }
        }
    }
Пример #2
0
    public static void LoadAsyn(string relpath, Type t, ResourceAsynHandlerEx handler)
    {
        if (relpath == null || relpath.Length <= 0)
        {
            return;
        }

        if (!bEnable) //editor case
        {
            //return Resources.Load(relpath, t);
            return;
        }
        else
        {
            string classname = t.Name;
            if (resourcetable.ContainsKey(classname) == false)
            {
                return;
            }

            Dictionary <string, ResourceInfoEx> infolist = resourcetable[classname];
            if (infolist == null)
            {
                return;
            }

            string resname = Path.GetFileNameWithoutExtension(relpath);
            if (infolist.ContainsKey(resname) == false)
            {
                return;
            }

            ResourceInfoEx info = infolist[resname];

            //if (info.path == null || info.path.Length <= 0) return;
            //UnityEngine.Object retobj = Resources.Load(info.path, t);
            //if (retobj == null)
            //{
            //    retobj = Resources.Load(info.path);
            //}

            //if (!t.IsInstanceOfType(retobj))
            //{
            //    UnityEngine.Object extractobj = GetObjectFromDependent(retobj, resname, t);
            //    if (extractobj != null) retobj = extractobj;
            //}

            //if (t == null || !t.IsInstanceOfType(retobj))
            //{
            //    Debug.Log("resname=" + resname + ",respath=" + info.path);
            //    Debug.Log(retobj);
            //}
            //if (handler != null)
            //{
            //    handler(relpath, retobj);
            //}

            if (info.obj != null)//已经加载过的直接回调
            {
                info.refcount++;
                if (handler != null)
                {
                    handler(relpath, info.obj);
                }
                return;
            }

            if (info.length > 0)
            {
                if (abm.abhs.ContainsKey(info.bundlename))
                {
                    AssetBundleEx ab = abm.abhs[info.bundlename];
                    if (ab.afs.ContainsKey(info.assetfilename))
                    {
                        AssetFileEx af = ab.afs[info.assetfilename];
                        Stream      fs = af.stream;
                        fs.Position = info.offset;
                        byte[]             data       = new byte[info.length];
                        AsyncResourceState asyncState = new AsyncResourceState {
                            FS = fs, Buffer = data, Info = info, T = t, Handler = handler, ResourcePath = relpath
                        };
                        IAsyncResult asyncResult = fs.BeginRead(data, (int)0, (int)info.length, new AsyncCallback(AsyncReadCallback), asyncState);
                    }
                }
            }

            return;
        }
    }