/// <summary>
        /// 更新信息
        /// </summary>
        public void UpdateSpriteGroup()
        {
            if (_spriteAsset && _spriteAsset.TexSource && _spriteAsset.Row > 1 && _spriteAsset.Column > 1)
            {
                int count = _spriteAsset.IsStatic ? _spriteAsset.Row * _spriteAsset.Column : _spriteAsset.Row;
                if (_spriteAsset.ListSpriteGroup.Count != count)
                {
                    _spriteAsset.ListSpriteGroup.Clear();
                    //更新
                    //----------------------------------
                    Vector2 texSize = new Vector2(_spriteAsset.TexSource.width, _spriteAsset.TexSource.height);
                    Vector2 size    = new Vector2((_spriteAsset.TexSource.width / (float)_spriteAsset.Column)
                                                  , (_spriteAsset.TexSource.height / (float)_spriteAsset.Row));

                    if (_spriteAsset.IsStatic)
                    {
                        int index = -1;
                        for (int i = 0; i < _spriteAsset.Row; i++)
                        {
                            for (int j = 0; j < _spriteAsset.Column; j++)
                            {
                                index++;
                                SpriteInforGroup inforGroup = Pool <SpriteInforGroup> .Get();

                                SpriteInfor infor = GetSpriteInfo(index, i, j, size, texSize);

                                //inforGroup.Tag = "emoji_" + infor.Id;
                                inforGroup.Tag = string.Format("{0:00}", infor.Id + 1);
                                inforGroup.ListSpriteInfor.Add(infor);
                                _spriteAsset.ListSpriteGroup.Add(inforGroup);
                            }
                        }
                    }
                    else
                    {
                        int index = -1;
                        for (int i = 0; i < _spriteAsset.Row; i++)
                        {
                            SpriteInforGroup inforGroup = Pool <SpriteInforGroup> .Get();

                            //inforGroup.Tag = "emoji_" + (index + 1);
                            inforGroup.Tag = string.Format("{0:00}", index + 1);
                            for (int j = 0; j < _spriteAsset.Column; j++)
                            {
                                index++;

                                SpriteInfor infor = GetSpriteInfo(index, i, j, size, texSize);

                                inforGroup.ListSpriteInfor.Add(infor);
                            }
                            _spriteAsset.ListSpriteGroup.Add(inforGroup);
                        }
                    }
                }
            }
        }
        //获取精灵信息
        private SpriteInfor GetSpriteInfo(int index, int row, int column, Vector2 size, Vector2 texSize)
        {
            SpriteInfor infor = Pool <SpriteInfor> .Get();

            infor.Id           = index;
            infor.Rect         = new Rect(size.y * column, texSize.y - (row + 1) * size.x, size.x, size.y);
            infor.DrawTexCoord = new Rect(infor.Rect.x / texSize.x, infor.Rect.y / texSize.y
                                          , infor.Rect.width / texSize.x, infor.Rect.height / texSize.y);
            infor.Uv = GetSpriteUV(texSize, infor.Rect);
            return(infor);
        }
Example #3
0
        public static List <SpriteInforGroup> GetAssetSpriteInfor(Texture2D tex)
        {
            List <SpriteInforGroup> _listGroup = new List <SpriteInforGroup>();
            string filePath = UnityEditor.AssetDatabase.GetAssetPath(tex);

            Object[] objects = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(filePath);

            List <SpriteInfor> _tempSprite = new List <SpriteInfor>();

            Vector2 _texSize = new Vector2(tex.width, tex.height);

            for (int i = 0; i < objects.Length; i++)
            {
                if (objects[i].GetType() != typeof(Sprite))
                {
                    continue;
                }
                SpriteInfor temp   = new SpriteInfor();
                Sprite      sprite = objects[i] as Sprite;
                temp.Id     = i;
                temp.Name   = sprite.name;
                temp.Pivot  = sprite.pivot;
                temp.Rect   = sprite.rect;
                temp.Sprite = sprite;
                temp.Tag    = sprite.name;
                temp.Uv     = GetSpriteUV(_texSize, sprite.rect);
                _tempSprite.Add(temp);
            }

            for (int i = 0; i < _tempSprite.Count; i++)
            {
                SpriteInforGroup _tempGroup = new SpriteInforGroup();
                _tempGroup.Tag = _tempSprite[i].Tag;
                //_tempGroup.Size = 24.0f;
                //_tempGroup.Width = 1.0f;
                _tempGroup.ListSpriteInfor = new List <SpriteInfor>();
                _tempGroup.ListSpriteInfor.Add(_tempSprite[i]);
                for (int j = i + 1; j < _tempSprite.Count; j++)
                {
                    if (_tempGroup.Tag == _tempSprite[j].Tag)
                    {
                        _tempGroup.ListSpriteInfor.Add(_tempSprite[j]);
                        _tempSprite.RemoveAt(j);
                        j--;
                    }
                }
                _listGroup.Add(_tempGroup);
                _tempSprite.RemoveAt(i);
                i--;
            }

            return(_listGroup);
        }
Example #4
0
        /// <summary>
        /// 改变sprite隶属的组
        /// </summary>
        private void ChangeTag(string newTag, SpriteInfor si)
        {
            if (newTag == si.Tag)
            {
                return;
            }

            //从旧的组中移除
            SpriteInforGroup oldSpriteInforGroup = _spriteAsset.ListSpriteGroup.Find(
                delegate(SpriteInforGroup sig)
            {
                return(sig.Tag == si.Tag);
            });

            if (oldSpriteInforGroup != null && oldSpriteInforGroup.ListSpriteInfor.Contains(si))
            {
                oldSpriteInforGroup.ListSpriteInfor.Remove(si);
            }

            //如果旧的组为空,则删掉旧的组
            if (oldSpriteInforGroup.ListSpriteInfor.Count <= 0)
            {
                _spriteAsset.ListSpriteGroup.Remove(oldSpriteInforGroup);
                Init();
            }

            si.Tag = newTag;
            //添加到新的组
            SpriteInforGroup newSpriteInforGroup = _spriteAsset.ListSpriteGroup.Find(
                delegate(SpriteInforGroup sig)
            {
                return(sig.Tag == newTag);
            });

            if (newSpriteInforGroup != null)
            {
                newSpriteInforGroup.ListSpriteInfor.Add(si);
                newSpriteInforGroup.ListSpriteInfor.Sort((a, b) => a.Id.CompareTo(b.Id));
            }

            EditorUtility.SetDirty(_spriteAsset);
        }