FairyGUI.UIPackage.LoadPackage C# (CSharp) Method

LoadPackage() private method

private LoadPackage ( ) : void
return void
        void LoadPackage()
        {
            string[] arr = null;
            string str;

            str = LoadString("sprites.bytes");
            if (str == null)
            {
                Debug.LogError("FairyGUI: cannot load package from '" + _assetNamePrefix + "'");
                return;
            }

            _loadingPackage = true;

            arr = str.Split('\n');
            int cnt = arr.Length;
            for (int i = 1; i < cnt; i++)
            {
                str = arr[i];
                if (str.Length == 0)
                    continue;

                string[] arr2 = str.Split(' ');
                AtlasSprite sprite = new AtlasSprite();
                string itemId = arr2[0];
                int binIndex = int.Parse(arr2[1]);
                if (binIndex >= 0)
                    sprite.atlas = "atlas" + binIndex;
                else
                {
                    int pos = itemId.IndexOf("_");
                    if (pos == -1)
                        sprite.atlas = "atlas_" + itemId;
                    else
                        sprite.atlas = "atlas_" + itemId.Substring(0, pos);
                }
                sprite.rect.x = int.Parse(arr2[2]);
                sprite.rect.y = int.Parse(arr2[3]);
                sprite.rect.width = int.Parse(arr2[4]);
                sprite.rect.height = int.Parse(arr2[5]);
                sprite.rotated = arr2[6] == "1";
                _sprites[itemId] = sprite;
            }

            byte[] hittestData = LoadBinary("hittest.bytes");
            if (hittestData != null)
            {
                ByteBuffer ba = new ByteBuffer(hittestData);
                while (ba.bytesAvailable)
                {
                    PixelHitTestData pht = new PixelHitTestData();
                    _hitTestDatas[ba.ReadString()] = pht;
                    pht.Load(ba);
                }
            }

            if (!_descPack.TryGetValue("package.xml", out str))
                throw new Exception("FairyGUI: invalid package '" + _assetNamePrefix + "', if the files is checked out from git, make sure to disable autocrlf option!");

            XML xml = new XML(str);

            id = xml.GetAttribute("id");
            name = xml.GetAttribute("name");

            XML rxml = xml.GetNode("resources");
            if (rxml == null)
                throw new Exception("FairyGUI: invalid package xml '" + _assetNamePrefix + "'");

            XMLList resources = rxml.Elements();

            _itemsById = new Dictionary<string, PackageItem>();
            _itemsByName = new Dictionary<string, PackageItem>();
            PackageItem pi;

            foreach (XML cxml in resources)
            {
                pi = new PackageItem();
                pi.owner = this;
                pi.type = FieldTypes.ParsePackageItemType(cxml.name);
                pi.id = cxml.GetAttribute("id");
                pi.name = cxml.GetAttribute("name");
                pi.exported = cxml.GetAttributeBool("exported");
                pi.file = cxml.GetAttribute("file");
                str = cxml.GetAttribute("size");
                if (str != null)
                {
                    arr = str.Split(',');
                    pi.width = int.Parse(arr[0]);
                    pi.height = int.Parse(arr[1]);
                }
                switch (pi.type)
                {
                    case PackageItemType.Image:
                        {
                            string scale = cxml.GetAttribute("scale");
                            if (scale == "9grid")
                            {
                                arr = cxml.GetAttributeArray("scale9grid");
                                if (arr != null)
                                {
                                    Rect rect = new Rect();
                                    rect.x = int.Parse(arr[0]);
                                    rect.y = int.Parse(arr[1]);
                                    rect.width = int.Parse(arr[2]);
                                    rect.height = int.Parse(arr[3]);
                                    pi.scale9Grid = rect;

                                    pi.tileGridIndice = cxml.GetAttributeInt("gridTile");
                                }
                            }
                            else if (scale == "tile")
                                pi.scaleByTile = true;
                            break;
                        }

                    case PackageItemType.Font:
                        {
                            pi.bitmapFont = new BitmapFont(pi);
                            FontManager.RegisterFont(pi.bitmapFont, null);
                            break;
                        }
                }
                _items.Add(pi);
                _itemsById[pi.id] = pi;
                if (pi.name != null)
                    _itemsByName[pi.name] = pi;
            }

            bool preloadAll = Application.isPlaying;
            if (preloadAll)
            {
                cnt = _items.Count;
                for (int i = 0; i < cnt; i++)
                    GetItemAsset(_items[i]);

                _descPack = null;
                _sprites = null;
            }
            else
                _items.Sort(ComparePackageItem);

            if (_resBundle != null)
            {
                _resBundle.Unload(false);
                _resBundle = null;
            }

            _loadingPackage = false;
        }

Usage Example

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);
        }