LoadPackage() private method

private LoadPackage ( ) : void
return void
Example #1
0
        /// <summary>
        /// Add a UI package from two assetbundles with a optional main asset name.
        /// </summary>
        /// <param name="desc">A assetbunble contains description file.</param>
        /// <param name="res">A assetbundle contains resources.</param>
        /// <param name="mainAssetName">Main asset name. e.g. Basics_fui.bytes</param>
        /// <returns>UIPackage</returns>
        public static UIPackage AddPackage(AssetBundle desc, AssetBundle res, string mainAssetName)
        {
            byte[] source = null;
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
            if (mainAssetName != null)
            {
                TextAsset ta = desc.LoadAsset <TextAsset>(mainAssetName);
                if (ta != null)
                {
                    source = ta.bytes;
                }
            }
            else
            {
                string[] names         = desc.GetAllAssetNames();
                string   searchPattern = "_fui";
                foreach (string n in names)
                {
                    if (n.IndexOf(searchPattern) != -1)
                    {
                        TextAsset ta = desc.LoadAsset <TextAsset>(n);
                        if (ta != null)
                        {
                            source        = ta.bytes;
                            mainAssetName = Path.GetFileNameWithoutExtension(n);
                            break;
                        }
                    }
                }
            }
#else
            if (mainAssetName != null)
            {
                TextAsset ta = (TextAsset)desc.Load(mainAssetName, typeof(TextAsset));
                if (ta != null)
                {
                    source = ta.bytes;
                }
            }
            else
            {
                source        = ((TextAsset)desc.mainAsset).bytes;
                mainAssetName = desc.mainAsset.name;
            }
#endif
            if (source == null)
            {
                throw new Exception("FairyGUI: no package found in this bundle.");
            }


            if (desc != res)
            {
                desc.Unload(true);
            }

            ByteBuffer buffer = new ByteBuffer(source);

            UIPackage pkg = new UIPackage();
            pkg._resBundle  = res;
            pkg._fromBundle = true;
            int    pos = mainAssetName.IndexOf("_fui");
            string assetNamePrefix;
            if (pos != -1)
            {
                assetNamePrefix = mainAssetName.Substring(0, pos);
            }
            else
            {
                assetNamePrefix = mainAssetName;
            }
            if (!pkg.LoadPackage(buffer, res.name, assetNamePrefix))
            {
                return(null);
            }

            _packageInstById[pkg.id]     = pkg;
            _packageInstByName[pkg.name] = pkg;
            _packageList.Add(pkg);

            return(pkg);
        }