Esempio n. 1
0
        public void GetExportPosition(SpriteAtlas atlas, out Rectf rect, out Vector2f pivot, out Vector4f border)
        {
            Vector2f rectOffset;

            if (atlas == null)
            {
                Vector2f textureOffset = RD.TextureRect.Position;
                Vector2f textureSize   = RD.TextureRect.Size;
                rectOffset = RD.TextureRectOffset;                 // should be equal to RD.TextureRect.Position - Rect.Position
                rect       = new Rectf(textureOffset, textureSize);
            }
            else
            {
                SpriteAtlasData atlasData     = atlas.RenderDataMap[RenderDataKey];
                Vector2f        textureOffset = atlasData.TextureRect.Position;
                Vector2f        textureSize   = atlasData.TextureRect.Size;
                rectOffset = atlasData.TextureRectOffset;
                rect       = new Rectf(textureOffset, textureSize);
            }

            Vector2f sizeDif           = Rect.Size - rect.Size;
            Vector2f pivotShiftSize    = new Vector2f(Pivot.X * sizeDif.X, Pivot.Y * sizeDif.Y);
            Vector2f relPivotShiftPos  = new Vector2f(rectOffset.X / rect.Size.X, rectOffset.Y / rect.Size.Y);
            Vector2f relPivotShiftSize = new Vector2f(pivotShiftSize.X / rect.Size.X, pivotShiftSize.Y / rect.Size.Y);

            pivot = Pivot - relPivotShiftPos + relPivotShiftSize;

            float borderL = Border.X == 0.0f ? 0.0f : Border.X - rectOffset.X;
            float borderB = Border.Y == 0.0f ? 0.0f : Border.Y - rectOffset.Y;
            float borderR = Border.Z == 0.0f ? 0.0f : Border.Z + rectOffset.X - sizeDif.X;
            float borderT = Border.W == 0.0f ? 0.0f : Border.W + rectOffset.Y - sizeDif.Y;

            border = new Vector4f(borderL, borderB, borderR, borderT);
        }
Esempio n. 2
0
        void Get(int id, out SpriteAtlasData atlas, out SpriteAtlasData.SpriteInfo sprite, out Sprite origin)
        {
            int atlasIndex  = Mathf.FloorToInt(id / 100000) - 1;
            int spriteIndex = Mathf.FloorToInt(id % 100000 / 100) - 1;
            int originIndex = id % 100 - 1;

            atlas  = null;
            sprite = null;
            origin = null;

            if (atlasIndex >= 0 && atlasIndex < atlasList.Count)
            {
                atlas = atlasList[atlasIndex];
            }
            if (atlas == null)
            {
                return;
            }

            if (spriteIndex >= 0)
            {
                sprite = atlas.sprites[spriteIndex];
            }

            if (originIndex >= 0 && originIndex < sprite.origins.Length)
            {
                origin = sprite.origins[originIndex];
            }
        }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            GUILayout.BeginVertical();
            GUILayout.Space(8);

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();

            if (GUILayout.Button("打开图集编辑器", GUILayout.MinWidth(120)))
            {
                SpriteAtlasData gen = (SpriteAtlasData)target;
                if (gen.name != defaultSpriteAtlasnName)
                {
                    EditorUtility.DisplayDialog("提示", "请修改预设名" + defaultSpriteAtlasnName + "再操作", "Ok");
                }
                else
                {
                    SpriteAtlasEditorPopup v = EditorWindow.GetWindow(typeof(SpriteAtlasEditorPopup), false, "图集编辑器") as SpriteAtlasEditorPopup;
                    v.SetGenerator(gen);
                    v.Show();
                }
            }

            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            GUILayout.Space(8);
        }
Esempio n. 4
0
    void OnGUI()
    {
        if (Application.isPlaying)
        {
            GUILayout.Label("playing");
            SpriteReferenceInfoDetector.watchEditorChange = false;
            return;
        }

        GUILayout.BeginHorizontal();
        bool scanAtlas      = GUILayout.Button("Scan Atlas", GUILayout.Width(100));
        bool scanGameObject = GUILayout.Button("Scan Reference", GUILayout.Width(100));
        bool updateAtlas    = GUILayout.Button("Update Atlas", GUILayout.Width(100));

        SpriteReferenceInfoDetector.watchEditorChange = GUILayout.Toggle(SpriteReferenceInfoDetector.watchEditorChange, "watching");
        scan_scene = GUILayout.Toggle(scan_scene, "scan scene");
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();

        Rect atlasRect = new Rect(0, 30, position.width / 2 - 5, position.height);

        if (m_AtlasListTree == null)
        {
            m_AtlasListTree = new AtlasListView(this);
            ReloadTreeView();
        }
        m_AtlasListTree.OnGUI(atlasRect);

        if (m_ReferenceListView == null)
        {
            m_ReferenceListView = new ReferenceListView();
            m_ReferenceListView.Reload();
        }

        Rect refRect = new Rect(position.width / 2 + 5, 30, position.width / 2 - 5, position.height);

        m_ReferenceListView.OnGUI(refRect);

        if (scanAtlas)
        {
            ScanSpriteAtlas();
        }

        if (scanGameObject)
        {
            SpriteReferenceInfo.Instance.Scan(scan_scene);
        }

        if (updateAtlas)
        {
            SpriteAtlasData            atlas  = null;
            SpriteAtlasData.SpriteInfo sprite = null;
            Sprite origin = null;
            if (m_AtlasListTree.GetSelectAtlas(out atlas, out sprite, out origin))
            {
                TexturePacker.Pack(atlas.sprites, atlas.texture);
                atlas.Scan();
            }
        }
    }
Esempio n. 5
0
    public void onSelectChange(AtlasListView view, SpriteAtlasData atlas, SpriteAtlasData.SpriteInfo sprite, Sprite origin)
    {
        if (m_ReferenceListView == null)
        {
            return;
        }

        // m_ReferenceListView = new ReferenceListView();
        // m_ReferenceListView.Reload();

        if (origin != null)
        {
            var r = SpriteReferenceInfo.Instance.FindSpriteInfo(origin);
            m_ReferenceListView.refs = (r != null) ? r.objInfo : null;
        }
        else if (sprite != null)
        {
            var r = SpriteReferenceInfo.Instance.FindSpriteInfo(sprite.packed);
            m_ReferenceListView.refs = (r != null) ? r.objInfo : null;
        }
        else
        {
            m_ReferenceListView.refs = null;
        }

        m_ReferenceListView.ExpandAll();
    }
        public void CopyToTarget(SpriteAtlasData target)
        {
            target.version = SpriteAtlasData.CURRENT_VERSION;

            spriteDataList.Sort((a, b) => a.name.CompareTo(b.name));
            target.spriteDataList = spriteDataList.ToArray();

            target.atlasTextures  = atlasTextures;
            target.atlasMaterials = atlasMaterials;
        }
        public static void CreateSpriteAtlas(string prefabName)
        {
            string path = GameEditorUtility.CreateNewPrefab(prefabName);

            if (path.Length != 0)
            {
                SpriteAtlasData spriteAtlasData = ScriptableObject.CreateInstance <SpriteAtlasData>();
                spriteAtlasData.version = SpriteAtlasData.CURRENT_VERSION;
                AssetDatabase.CreateAsset(spriteAtlasData, path);
                //创建后拾取
                Selection.activeObject = AssetDatabase.LoadAssetAtPath(path, typeof(SpriteAtlasData));
            }
        }
Esempio n. 8
0
        public void GetSpriteCoordinatesInAtlas(SpriteAtlas atlas, out Rectf sAtlasRect, out Vector2f sAtlasPivot, out Vector4f sAtlasBorder)
        {
            // sprite values are relative to original image (image, it was created from).
            // since atlas shuffle and crop sprite images, we need to recalculate those values.
            // if sprite doesn't belong to an atlas, consider its image as single sprite atlas

            Vector2f cropBotLeft;
            Vector2f cropTopRight;

            if (atlas == null)
            {
                Vector2f spriteOffset = RD.TextureRect.Position;
                Vector2f spriteSize   = RD.TextureRect.Size;
                sAtlasRect  = new Rectf(spriteOffset, spriteSize);
                cropBotLeft = RD.TextureRectOffset;
            }
            else
            {
                SpriteAtlasData atlasData    = atlas.RenderDataMap[RenderDataKey];
                Vector2f        spriteOffset = atlasData.TextureRect.Position;
                Vector2f        spriteSize   = atlasData.TextureRect.Size;
                sAtlasRect  = new Rectf(spriteOffset, spriteSize);
                cropBotLeft = atlasData.TextureRectOffset;
            }

            Vector2f sizeDelta = Rect.Size - sAtlasRect.Size;

            cropTopRight = new Vector2f(sizeDelta.X - cropBotLeft.X, sizeDelta.Y - cropBotLeft.Y);

            Vector2f pivot = Pivot;

            if (!HasPivot(File.Version))
            {
                Vector2f center      = new Vector2f(Rect.Size.X / 2.0f, Rect.Size.Y / 2.0f);
                Vector2f pivotOffset = center + Offset;
                pivot = new Vector2f(pivotOffset.X / Rect.Size.X, pivotOffset.Y / Rect.Size.Y);
            }

            Vector2f pivotPosition       = new Vector2f(pivot.X * Rect.Size.X, pivot.Y * Rect.Size.Y);
            Vector2f aAtlasPivotPosition = pivotPosition - cropBotLeft;

            sAtlasPivot = new Vector2f(aAtlasPivotPosition.X / sAtlasRect.Size.X, aAtlasPivotPosition.Y / sAtlasRect.Size.Y);

            float borderL = Border.X == 0.0f ? 0.0f : Border.X - cropBotLeft.X;
            float borderB = Border.Y == 0.0f ? 0.0f : Border.Y - cropBotLeft.Y;
            float borderR = Border.Z == 0.0f ? 0.0f : Border.Z - cropTopRight.X;
            float borderT = Border.W == 0.0f ? 0.0f : Border.W - cropTopRight.Y;

            sAtlasBorder = new Vector4f(borderL, borderB, borderR, borderT);
        }
Esempio n. 9
0
    public static SpriteAtlasData Get(string path)
    {
        TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;

        if (importer == null || importer.textureType != TextureImporterType.Sprite)
        {
            return(null);
        }

        if (importer.spriteImportMode == SpriteImportMode.Single)
        {
            return(null);
        }

        if (importer.spriteImportMode != SpriteImportMode.Multiple)
        {
            return(null);
        }

        string atlas_guid = AssetDatabase.AssetPathToGUID(path);

        if (string.IsNullOrEmpty(atlas_guid))
        {
            return(null);
        }

        string data_path = string.Format("{0}/{1}_SpriteAtlasData.asset",
                                         System.IO.Path.GetDirectoryName(path),
                                         System.IO.Path.GetFileNameWithoutExtension(path));

        SpriteAtlasData data = AssetDatabase.LoadAssetAtPath <SpriteAtlasData>(data_path);

        if (data == null)
        {
            data         = ScriptableObject.CreateInstance <SpriteAtlasData>();
            data.texture = AssetDatabase.LoadAssetAtPath <Texture2D>(path);
            data.Scan();
            AssetDatabase.CreateAsset(data, data_path);
        }
        else
        {
            if (data.texture == null)
            {
                data.texture = AssetDatabase.LoadAssetAtPath <Texture2D>(path);
            }
        }
        return(data);
    }
Esempio n. 10
0
 public void RemoveAtlas(SpriteAtlasData atlas, bool delete = false)
 {
     if (_spriteAtlasInfo.Contains(atlas))
     {
         _spriteAtlasInfo.Remove(atlas);
         if (delete)
         {
             string path = AssetDatabase.GetAssetPath(atlas);
             if (!string.IsNullOrEmpty(path))
             {
                 Debug.LogFormat("Delete {0}", path);
                 AssetDatabase.DeleteAsset(path);
             }
         }
     }
 }
Esempio n. 11
0
        public TextureExportCollection(IAssetExporter assetExporter, Texture2D texture, bool convert) :
            base(assetExporter, texture, CreateImporter(texture, convert))
        {
            m_convert = convert;
            if (convert)
            {
                TextureImporter textureImporter          = (TextureImporter)MetaImporter;
                Dictionary <Sprite, SpriteAtlas> sprites = new Dictionary <Sprite, SpriteAtlas>();
                foreach (Object asset in texture.File.Collection.FetchAssets())
                {
                    switch (asset.ClassID)
                    {
                    case ClassIDType.Sprite:
                    {
                        Sprite sprite = (Sprite)asset;
                        if (sprite.RD.Texture.IsAsset(sprite.File, texture))
                        {
                            SpriteAtlas atlas = Sprite.IsReadRendererData(sprite.File.Version) ? sprite.SpriteAtlas.FindAsset(sprite.File) : null;
                            sprites.Add(sprite, atlas);
                            AddAsset(sprite);
                        }
                    }
                    break;

                    case ClassIDType.SpriteAtlas:
                    {
                        SpriteAtlas atlas = (SpriteAtlas)asset;
                        foreach (PPtr <Sprite> spritePtr in atlas.PackedSprites)
                        {
                            Sprite sprite = spritePtr.FindAsset(atlas.File);
                            if (sprite != null)
                            {
                                SpriteAtlasData atlasData = atlas.RenderDataMap[sprite.RenderDataKey];
                                if (atlasData.Texture.IsAsset(atlas.File, texture))
                                {
                                    sprites.Add(sprite, atlas);
                                    AddAsset(sprite);
                                }
                            }
                        }
                    }
                    break;
                    }
                }
                textureImporter.Sprites = sprites;
            }
        }
        public TextureExportCollection(IAssetExporter assetExporter, Texture2D texture, bool convert) :
            base(assetExporter, texture)
        {
            m_convert = convert;
            if (convert)
            {
                foreach (Object asset in texture.File.Collection.FetchAssets())
                {
                    switch (asset.ClassID)
                    {
                    case ClassIDType.Sprite:
                    {
                        Sprite sprite = (Sprite)asset;
                        if (sprite.RD.Texture.IsAsset(sprite.File, texture))
                        {
                            SpriteAtlas atlas = Sprite.HasRendererData(sprite.File.Version) ? sprite.SpriteAtlas.FindAsset(sprite.File) : null;
                            m_sprites.Add(sprite, atlas);
                            AddAsset(sprite);
                        }
                    }
                    break;

                    case ClassIDType.SpriteAtlas:
                    {
                        SpriteAtlas atlas = (SpriteAtlas)asset;
                        if (atlas.RenderDataMap.Count > 0)
                        {
                            foreach (PPtr <Sprite> spritePtr in atlas.PackedSprites)
                            {
                                Sprite sprite = spritePtr.FindAsset(atlas.File);
                                if (sprite != null)
                                {
                                    SpriteAtlasData atlasData = atlas.RenderDataMap[sprite.RenderDataKey];
                                    if (atlasData.Texture.IsAsset(atlas.File, texture))
                                    {
                                        m_sprites.Add(sprite, atlas);
                                        AddAsset(sprite);
                                    }
                                }
                            }
                        }
                    }
                    break;
                    }
                }
            }
        }
Esempio n. 13
0
    List <Sprite> ScanGameObject(GameObject obj)
    {
        List <Sprite> single = new List <Sprite>();

        if (obj != null)
        {
            SpriteReferenceInfo.ScanGameObjectSprite(obj, (o, s, p) => {
                string key = GetSpriteKey(s);

                List <SpriteReferenceInfo.GameObjectInfo> _refs;
                if (!refInfo.TryGetValue(key, out _refs))
                {
                    _refs        = new List <SpriteReferenceInfo.GameObjectInfo>();
                    refInfo[key] = _refs;
                    var oi       = _refs.Find((i) => i.obj == obj);
                    if (oi == null)
                    {
                        var info = new SpriteReferenceInfo.GameObjectInfo(obj);
                        info.paths.Add(p);
                        _refs.Add(info);
                    }
                    else
                    {
                        oi.paths.Add(p);
                    }
                }

                SpriteAtlasData a = SpriteAtlasManager.Get(s.texture);
                if (a != null)
                {
                    if (!atlas.Contains(a))
                    {
                        atlas.Add(a);
                    }
                }
                else
                {
                    if (!single.Contains(s))
                    {
                        single.Add(s);
                    }
                }
            });
        }
        return(single);
    }
Esempio n. 14
0
        public bool GetSelectAtlas(out SpriteAtlasData atlas, out SpriteAtlasData.SpriteInfo sprite, out Sprite origin)
        {
            atlas  = null;
            sprite = null;
            origin = null;

            IList <int> sel = GetSelection();

            if (sel.Count == 0)
            {
                return(false);
            }

            Get(sel[0], out atlas, out sprite, out origin);

            return(true);
        }
        private static void SetAtlas(string path, SpriteAnimationData spriteAnimationData)
        {
            DirectoryInfo info = new DirectoryInfo(Path.GetDirectoryName(path));

            FileInfo[] files = info.GetFiles();
            foreach (FileInfo file in files)
            {
                if (file.FullName.EndsWith("prefab")) //only prefab
                {
                    string          fullPath           = getAssetPath(file.FullName);
                    SpriteAtlasData objSpriteAtlasData = AssetDatabase.LoadAssetAtPath(fullPath, typeof(SpriteAtlasData)) as SpriteAtlasData;
                    if (objSpriteAtlasData != null)
                    {
                        spriteAnimationData.SpriteAtlasData = objSpriteAtlasData;
                    }
                }
            }
        }
Esempio n. 16
0
        protected override void SelectionChanged(IList <int> selectedIds)
        {
            List <Object> objs = new List <Object>();

            foreach (int id in selectedIds)
            {
                SpriteAtlasData            atlas  = null;
                SpriteAtlasData.SpriteInfo sprite = null;
                Sprite origin = null;

                Get(id, out atlas, out sprite, out origin);

                if (origin != null)
                {
                    objs.Add(origin);
                }
                else if (sprite != null)
                {
                    if (sprite.packed != null)
                    {
                        objs.Add(sprite.packed);
                    }

                    if (id % 100 == 0 && select_origin_with_pack && sprite.origin != null && sprite.origin != sprite.packed)
                    {
                        objs.Add(sprite.origin);
                    }
                }
                else if (atlas != null)
                {
                    objs.Add(atlas.texture);
                }

                if (_delegate != null)
                {
                    if (atlas != null || sprite != null || origin != null)
                    {
                        _delegate.onSelectChange(this, atlas, sprite, origin);
                    }
                }
            }
            Selection.objects = objs.ToArray();
        }
Esempio n. 17
0
        public SpriteMetaData(Sprite sprite)
        {
            Name      = sprite.Name;
            Alignment = SpriteAlignment.Custom;

            Vector2f    rectOffset;
            SpriteAtlas atlas = sprite.SpriteAtlas.FindObject(sprite.File);

            if (atlas == null)
            {
                Rectf    textureRect   = sprite.RD.TextureRect;
                Vector2f textureOffset = sprite.RD.TextureRect.Position + Rect.Position;
                Vector2f textureSize   = sprite.RD.TextureRect.Size;
                Rect       = new Rectf(textureOffset, textureSize);
                rectOffset = sprite.RD.TextureRectOffset;
            }
            else
            {
                SpriteAtlasData atlasData     = atlas.RenderDataMap[sprite.RenderDataKey];
                Vector2f        textureOffset = atlasData.TextureRect.Position + Rect.Position;
                Vector2f        textureSize   = atlasData.TextureRect.Size;
                Rect       = new Rectf(textureOffset, textureSize);
                rectOffset = atlasData.TextureRectOffset;
            }

            Vector2f decSizeDif        = sprite.Rect.Size - Rect.Size;
            Vector2f pivotShiftSize    = new Vector2f(sprite.Pivot.X * decSizeDif.X, sprite.Pivot.Y * decSizeDif.Y);
            Vector2f relPivotShiftPos  = new Vector2f(rectOffset.X / Rect.Size.X, rectOffset.Y / Rect.Size.Y);
            Vector2f relPivotShiftSize = new Vector2f(pivotShiftSize.X / Rect.Size.X, pivotShiftSize.Y / Rect.Size.Y);

            Pivot = sprite.Pivot - relPivotShiftPos + relPivotShiftSize;

            float borderL = sprite.Border.X == 0.0f ? 0.0f : sprite.Border.X - rectOffset.X;
            float borderB = sprite.Border.Y == 0.0f ? 0.0f : sprite.Border.Y - rectOffset.Y;
            float borderR = sprite.Border.Z == 0.0f ? 0.0f : sprite.Border.Z + rectOffset.X - decSizeDif.X;
            float borderT = sprite.Border.W == 0.0f ? 0.0f : sprite.Border.W + rectOffset.Y - decSizeDif.Y;

            Border = new Vector4f(borderL, borderB, borderR, borderT);

            Outline            = sprite.GenerateOutline(Rect, Pivot);
            PhysicsShape       = sprite.GeneratePhysicsShape();
            TessellationDetail = 0;
        }
Esempio n. 18
0
        public List <SpriteAtlasData> GetSpriteAtlasDatasList()
        {
            IList <int>            selectedIds = GetSelection();
            List <SpriteAtlasData> objs        = new List <SpriteAtlasData>();

            foreach (int id in selectedIds)
            {
                SpriteAtlasData            atlas  = null;
                SpriteAtlasData.SpriteInfo sprite = null;
                Sprite origin = null;

                Get(id, out atlas, out sprite, out origin);

                if (!objs.Contains(atlas))
                {
                    objs.Add(atlas);
                }
            }
            return(objs);
        }
Esempio n. 19
0
    public void onSelectChange(SpriteAtlasManagerWindow.AtlasListView view, SpriteAtlasData data, SpriteAtlasData.SpriteInfo si, Sprite s)
    {
        if (s == null && si != null)
        {
            s = si.packed;
        }

        string key = GetSpriteKey(s);

        List <SpriteReferenceInfo.GameObjectInfo> list;

        if (refInfo.TryGetValue(key, out list))
        {
            m_refList.refs = list;
        }
        else
        {
            m_refList.refs = new List <SpriteReferenceInfo.GameObjectInfo>();
        }
        m_refList.Reload();
        m_refList.ExpandAll();
        return;
    }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            GUILayout.BeginVertical();
            GUILayout.Space(8);

            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();

            if (GUILayout.Button("打开图集编辑器", GUILayout.MinWidth(120)))
            {
                SpriteAtlasData gen = (SpriteAtlasData)target;

                SpriteAtlasEditorPopup v = EditorWindow.GetWindow(typeof(SpriteAtlasEditorPopup), false, "图集编辑器") as SpriteAtlasEditorPopup;
                v.SetGenerator(gen);
                v.Show();
            }

            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            GUILayout.Space(8);
        }
 public void SetGenerator(SpriteAtlasData spriteAtlasData)
 {
     this._spriteAtlasData  = spriteAtlasData;
     this._spriteAtlasProxy = new SpriteAtlasProxy(spriteAtlasData);
     dir = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(spriteAtlasData)) + "/";
 }
Esempio n. 22
0
        private Vector2f[][] FixRotation(SpriteAtlas atlas, Vector2f[][] outlines)
        {
            bool isPacked = RD.IsPacked;
            SpritePackingRotation rotation = RD.PackingRotation;

            if (atlas != null)
            {
                SpriteAtlasData atlasData = atlas.RenderDataMap[RenderDataKey];
                isPacked = atlasData.IsPacked;
                rotation = atlasData.PackingRotation;
            }

            if (isPacked)
            {
                switch (rotation)
                {
                case SpritePackingRotation.FlipHorizontal:
                {
                    foreach (Vector2f[] outline in outlines)
                    {
                        for (int i = 0; i < outline.Length; i++)
                        {
                            Vector2f vertex = outline[i];
                            outline[i] = new Vector2f(-vertex.X, vertex.Y);
                        }
                    }
                }
                break;

                case SpritePackingRotation.FlipVertical:
                {
                    foreach (Vector2f[] outline in outlines)
                    {
                        for (int i = 0; i < outline.Length; i++)
                        {
                            Vector2f vertex = outline[i];
                            outline[i] = new Vector2f(vertex.X, -vertex.Y);
                        }
                    }
                }
                break;

                case SpritePackingRotation.Rotate90:
                {
                    foreach (Vector2f[] outline in outlines)
                    {
                        for (int i = 0; i < outline.Length; i++)
                        {
                            Vector2f vertex = outline[i];
                            outline[i] = new Vector2f(vertex.Y, vertex.X);
                        }
                    }
                }
                break;

                case SpritePackingRotation.Rotate180:
                {
                    foreach (Vector2f[] outline in outlines)
                    {
                        for (int i = 0; i < outline.Length; i++)
                        {
                            Vector2f vertex = outline[i];
                            outline[i] = new Vector2f(-vertex.X, -vertex.Y);
                        }
                    }
                }
                break;
                }
            }
            return(outlines);
        }
Esempio n. 23
0
        protected override void KeyEvent()
        {
            base.KeyEvent();
            if (GUIUtility.keyboardControl != treeViewControlID)
            {
                return;
            }

            if (Event.current.type != EventType.KeyDown)
            {
                return;
            }

            if (GetSelection().Count <= 0)
            {
                return;
            }

            SpriteAtlasData atlas = null;

            SpriteAtlasData.SpriteInfo sprite = null;
            Sprite origin = null;

            if (!GetSelectAtlas(out atlas, out sprite, out origin))
            {
                return;
            }

            if (Event.current.keyCode == KeyCode.Space)
            {
                if (sprite != null && origin == null)
                {
                    if (sprite.delete || _refInfo.GetRefCount(sprite.packed) == 0)
                    {
                        sprite.delete = !sprite.delete;
                        Repaint();
                    }
                }
            }

            if (Event.current.keyCode == KeyCode.Delete || Event.current.keyCode == KeyCode.Backspace)
            {
                if (origin != null)
                {
                    if (origin.name != sprite.name)
                    {
                        sprite.RemoveOrigin(origin);
                        Reload();
                    }
                }
                else if (sprite != null)
                {
                    if (Event.current.control && sprite.packed == null && sprite.delete)
                    {
                        atlas.RemoveSprite(sprite);
                        Reload();
                    }
                    else
                    {
                        sprite.delete = true;
                        Repaint();
                    }
                }
                else if (atlas)
                {
                    // _manager.RemoveAtlas(atlas, true);
                    // Reload();
                }
            }

            int use = 0;

            if (Event.current.keyCode == KeyCode.F5)
            {
                use = 1;
            }
            else if (Event.current.keyCode == KeyCode.F6)
            {
                use = 2;
            }

            if (use != 0 && origin != null && origin == sprite.origin)
            {
                Sprite from = (use == 1) ? origin : sprite.packed;
                Sprite to   = (use == 1) ? sprite.packed : origin;
                if (from == null || to == null || from == to)
                {
                    return;
                }

                List <GameObject> objInfo = _delegate.GetReserenceObjects(from);

                if (objInfo.Count > 0)
                {
                    List <GameObject> objs = new List <GameObject>();
                    try {
                        for (int i = 0; i < objInfo.Count; i++)
                        {
                            var obj = objInfo[i];
                            EditorUtility.DisplayProgressBar("replace", AssetDatabase.GetAssetPath(obj), i / objInfo.Count);

                            if (obj != null && obj.GetType() == typeof(GameObject))
                            {
                                GameObject go = obj as GameObject;
                                SpriteReferenceInfo.ScanGameObject(go, (c, p) => {
                                    SpriteReferenceInfo.ReplaceSprite(c, from, to);
                                });
                                EditorUtility.SetDirty(go);
                                objs.Add(go);
                            }
                        }

                        foreach (GameObject go in objs)
                        {
                            SpriteReferenceInfo.Instance.Reload(go);
                        }
                    } catch (System.Exception e) {
                        Debug.LogError(e);
                    } finally {
                        EditorUtility.ClearProgressBar();
                    }
                    Reload();
                }
            }
        }
Esempio n. 24
0
        protected override DragAndDropVisualMode HandleDragAndDrop(DragAndDropArgs args)
        {
            if (args.parentItem == null)
            {
                return(DragAndDropVisualMode.Rejected);
            }

            // if (args.parentItem.id == -1) return DragAndDropVisualMode.Rejected;

            SpriteAtlasData atlas;

            SpriteAtlasData.SpriteInfo sprite;
            Sprite origin;

            Get(args.parentItem.id, out atlas, out sprite, out origin);

            List <Texture2D> draging_atlas = new List <Texture2D>();

            Object[] objs = DragAndDrop.objectReferences;

            List <Sprite> sprites = new List <Sprite>();

            foreach (var obj in DragAndDrop.objectReferences)
            {
                Sprite s = null;
                if (obj.GetType() == typeof(Sprite))
                {
                    s = obj as Sprite;
                }
                else if (obj.GetType() == typeof(Texture2D))
                {
                    TextureImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(obj)) as TextureImporter;
                    if (importer.textureType != TextureImporterType.Sprite)
                    {
                        continue;
                    }

                    if (importer.spriteImportMode == SpriteImportMode.Multiple)
                    {
                        draging_atlas.Add(obj as Texture2D);
                        continue;
                    }
                    else
                    {
                        s = AssetDatabase.LoadAssetAtPath <Sprite>(AssetDatabase.GetAssetPath(obj));
                    }
                }

                if (s == null || s.texture == atlas.texture)
                {
                    continue;
                }

                if (atlas.HaveSprite(s.name))
                {
                    continue;
                }

                sprites.Add(s);
            }

            if (atlas == null)
            {
                if (draging_atlas.Count == 0)
                {
                    return(DragAndDropVisualMode.Rejected);
                }
            }
            else
            {
                if (sprites.Count == 0)
                {
                    return(DragAndDropVisualMode.Rejected);
                }
            }

            if (!args.performDrop)
            {
                return(DragAndDropVisualMode.Copy);
            }

            if (atlas == null)
            {
                foreach (Texture2D tex in draging_atlas)
                {
                    SpriteAtlasData spriteAtlasData = SpriteAtlasManager.Get(tex);
                    if (!atlasList.Contains(spriteAtlasData))
                    {
                        atlasList.Add(spriteAtlasData);
                    }
                }
            }
            else
            {
                foreach (Sprite s in sprites)
                {
                    atlas.AddSprite(s);
                }
            }

            DragAndDrop.AcceptDrag();
            Reload();

            return(DragAndDropVisualMode.Copy);
        }
Esempio n. 25
0
    void Reload()
    {
        atlas.Clear();
        refInfo.Clear();
        // single_sprites.Clear();

        if (__debug)   // debug
        {
            string[] mapSceneUI =
            {
                "prefabs/mapSceneUI/mapSceneUI.prefab",
                "prefabs/mapSceneUI/QuestGuideTip.prefab",
                "prefabs/mapSceneUI/MainUITeam.prefab",
                "prefabs/mapSceneUI/WorldBossBuffList.prefab",
                "prefabs/mapSceneUI/mapSceneQuestList.prefab",

/*
 * "prefabs/battlefield/BattleDialog.prefab",
 * "prefabs/battlefield/BuffTips.prefab",
 * "prefabs/battlefield/MonsterInfo.prefab",
 * "prefabs/battlefield/pet.prefab",
 * "prefabs/battlefield/pet_enemy.prefab",
 * "prefabs/battlefield/RandomBuffSlots.prefab",
 * "prefabs/battlefield/ItemBoxPanel.prefab",
 * "prefabs/battlefield/randomBuffItem.prefab",
 * "prefabs/fightResult/FightResultFrame.prefab",
 * "prefabs/battlefield/storyloading.prefab",
 * "prefabs/battlefield/enemy.prefab",
 * "prefabs/battlefield/enemy2.prefab",
 * "prefabs/battlefield/enemy2.prefab",
 * "prefabs/battlefield/enemy2.prefab",
 * "prefabs/battlefield/enemy2.prefab",
 * "prefabs/battlefield/enemyBoss.prefab",
 * "prefabs/battlefield/BattlefieldTargetRingMenu.prefab",
 * "prefabs/battlefield/RoleInfoPanel.prefab",
 * "prefabs/battlefield/RoleInfoPanel.prefab",
 * "prefabs/battlefield/TeamMembers.prefab",
 * "prefabs/base/FightingBtn.prefab",
 * "prefabs/battlefield/randomBuffItem.prefab",
 * "prefabs/battlefield/randomHeroBuffItem.prefab",
 */
            };

            targetGameObjects.Clear();
            foreach (string s in mapSceneUI)
            {
                string debug_prefab = "Assets/assetbundle/" + s;
                if (!string.IsNullOrEmpty(debug_prefab) && (Selection.activeGameObject == null || AssetDatabase.GetAssetPath(Selection.activeGameObject) != debug_prefab))
                {
                    GameObject obj = AssetDatabase.LoadAssetAtPath <GameObject>(debug_prefab);
                    targetGameObjects.Add(obj);
                }
            }
            targetTexture = AssetDatabase.LoadAssetAtPath <Texture2D>("Assets/UI8/mapScene/mapSceneUI.png");
        }

        SpriteAtlasData single = ScriptableObject.CreateInstance <SpriteAtlasData>();

        single.name = "Sprites";
        foreach (GameObject obj in targetGameObjects)
        {
            var list = ScanGameObject(obj);
            foreach (var s in list)
            {
                string path = AssetDatabase.GetAssetPath(s.texture);
                if (path.StartsWith("Assets/assetbundle/"))
                {
                    continue;
                }

                TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
                if (importer == null || (importer.textureType == TextureImporterType.Sprite && importer.spriteImportMode == SpriteImportMode.Multiple))
                {
                    continue;
                }

                if (!single.HaveSprite(s))
                {
                    single.AddSprite(s);
                }
            }
        }

        if (single.sprites != null && single.sprites.Length > 0)
        {
            atlas.Add(single);
        }

        ReloadTreeView();
    }
 public SpriteAtlasProxy(SpriteAtlasData obj)
 {
     this.obj = obj;
     CopyFromSource();
 }
Esempio n. 27
0
    public void Scan(bool single = false)
    {
        List <SpriteAtlasData> list = new List <SpriteAtlasData>();

        try {
            int      findCount = 0;
            string[] assets    = AssetDatabase.FindAssets("t:texture");
            for (int i = 0; i < assets.Length; i++)
            {
                string path = AssetDatabase.GUIDToAssetPath(assets[i]);

                if (EditorUtility.DisplayCancelableProgressBar(string.Format("{0}", path), string.Format("{0}/{1}, {2}", i + 1, assets.Length, findCount), (float)i / (float)assets.Length))
                {
                    break;
                }

                if (InBlacklist(path))
                {
                    continue;
                }

                TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
                if (importer == null)
                {
                    continue;
                }

                if (importer.textureType != TextureImporterType.Sprite)
                {
                    continue;
                }

                if (importer.spriteImportMode == SpriteImportMode.Single)
                {
                    if (single)
                    {
                        singleSpriteList.Add(AssetDatabase.LoadAssetAtPath <Sprite>(path));
                        findCount++;
                    }
                }
                else
                {
                    SpriteAtlasData data = Get(path);
                    if (data != null)
                    {
                        data.Scan();
                        list.Add(data);
                        findCount++;
                    }
                }
            }

            list.Sort((a, b) => {
                return(string.Compare(a.path, b.path));
            });
        } catch (System.Exception e) {
            Debug.LogError(e);
            EditorUtility.ClearProgressBar();
            return;
        }

        _spriteAtlasInfo.Clear();
        _spriteAtlasInfo.AddRange(list);

        EditorUtility.ClearProgressBar();
    }
Esempio n. 28
0
        void LoadTextureAndAtlasFiles()
        {
            if (_texturePtr != IntPtr.Zero)
            {
                Core.GetGlobalManager <ImGuiManager>().UnbindTexture(_texturePtr);
            }

            _spriteAtlasData.Clear();
            _nonEditableAnimations.Clear();
            _atlasAllowsAnimationEditing = true;
            _hasSlicedContent            = false;

            var _atlasTexture = Texture2D.FromStream(Core.GraphicsDevice, File.OpenRead(_sourceImageFile));

            _textureSize            = new Num.Vector2(_atlasTexture.Width, _atlasTexture.Height);
            _textureAspectRatio     = _textureSize.X / _textureSize.Y;
            _texturePtr             = Core.GetGlobalManager <ImGuiManager>().BindTexture(_atlasTexture);
            _textureLoadedThisFrame = true;

            if (File.Exists(_sourceAtlasFile))
            {
                _hasSlicedContent = true;
                _spriteAtlasData  = SpriteAtlasLoader.ParseSpriteAtlasData(_sourceAtlasFile, true);

                // ensure animations are contiguous, since that is all we support.
                // First check that all frames are in order in the animations
                for (var j = 0; j < _spriteAtlasData.AnimationFrames.Count; j++)
                {
                    var animation = _spriteAtlasData.AnimationFrames[j];
                    for (var i = 0; i < animation.Count; i++)
                    {
                        if (i == 0)
                        {
                            continue;
                        }

                        if (animation[i] != animation[i - 1] + 1)
                        {
                            _atlasAllowsAnimationEditing = false;
                            _nonEditableAnimations.Add(j);
                        }
                    }
                }

                // Next check that all frames are in order, ghetto style. We check that all the y-values of the rects
                // always increase or stay the same. Not perfect by any means, but we dont know if this is padded in some
                // odd way or contains sprites of odd sizes so this is a quick and dirty solution.
                var lastRectY = -1;
                for (var i = 0; i < _spriteAtlasData.SourceRects.Count; i++)
                {
                    if (i == 0 || lastRectY <= _spriteAtlasData.SourceRects[i].Y)
                    {
                        lastRectY = _spriteAtlasData.SourceRects[i].Y;
                        continue;
                    }

                    _atlasAllowsAnimationEditing = false;
                    return;
                }
            }
        }
Esempio n. 29
0
        protected override void RowGUI(RowGUIArgs args)
        {
            SpriteAtlasData atlas = null;

            SpriteAtlasData.SpriteInfo sprite = null;
            Sprite origin = null;

            Get(args.item.id, out atlas, out sprite, out origin);

            if (atlas == null)
            {
                Debug.LogWarningFormat("atlas == null, {0}", args.item.id);
                return;
            }


            if (args.item.depth == 0)
            {
                base.RowGUI(args);
            }
            else if (args.item.depth == 1)
            {
                var info = sprite;

                if (_dirty < 1)
                {
                    if (info.FindOrigin())
                    {
                        EditorUtility.SetDirty(atlas);
                        _dirty += 1;
                    }
                }
                base.RowGUI(args);

                Rect re = args.rowRect;
                re.x     = re.xMax - 100;
                re.width = 60;
                EditorGUI.LabelField(re, string.Format("{0}", (_delegate == null) ? 0 : _delegate.GetReferenceCount(info.packed)));  // _refInfo.GetRefCount(info.packed))

                if (info.origin != null && info.origin != info.packed)
                {
                    int count = (_delegate == null) ? 0 : _delegate.GetReferenceCount(info.origin);
                    if (count > 0)
                    {
                        re       = args.rowRect;
                        re.x     = re.xMax - 60;
                        re.width = 60;
                        Color c = DefaultStyles.label.normal.textColor;
                        DefaultStyles.label.normal.textColor = Color.red;
                        EditorGUI.LabelField(re, string.Format("{0}", count));
                        DefaultStyles.label.normal.textColor = c;
                    }
                }

                Rect r = args.rowRect;
                r.x = r.xMax - 15;
                var d = !EditorGUI.Toggle(r, !info.delete);
                if (d != info.delete)
                {
                    info.delete = d;
                    EditorUtility.SetDirty(atlas);
                }
            }
            else if (args.item.depth == 2)
            {
                Rect r = args.rowRect;
                r.x    += 60;
                r.width = 300;
                if (args.rowRect.width < 500)
                {
                    r.width = args.rowRect.width - 200;
                }

                bool e = GUI.enabled;
                if (args.item.displayName == "----")
                {
                    EditorGUI.BeginChangeCheck();
                    origin = (Sprite)EditorGUI.ObjectField(r, origin, typeof(Sprite), false);
                    if (origin != null && EditorGUI.EndChangeCheck())
                    {
                        // manual find origin
                        TextureImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(origin.texture)) as TextureImporter;
                        if (importer != null && importer.textureType == TextureImporterType.Sprite && importer.spriteImportMode == SpriteImportMode.Single)
                        {
                            sprite.AddOrigin(origin);
                            sprite.origin = origin;
                            EditorUtility.SetDirty(atlas);
                            Reload();
                        }
                    }
                }
                else
                {
                    GUI.enabled = false;
                    EditorGUI.ObjectField(r, origin, typeof(Sprite), false);
                    GUI.enabled = e;

                    r.x     = r.xMax;
                    r.width = 15;

                    bool is_origin = origin == sprite.origin;

                    bool nt = EditorGUI.Toggle(r, is_origin);
                    if (nt != is_origin)
                    {
                        if (is_origin)
                        {
                            sprite.origin = sprite.packed;
                        }
                        else
                        {
                            sprite.origin = origin;
                        }
                        EditorUtility.SetDirty(atlas);
                    }

                    if (sprite.origin == origin)
                    {
                        r.x     = r.xMax + 15;
                        r.width = 15;
                        bool nu = EditorGUI.Toggle(r, sprite.update);
                        if (nu != sprite.update)
                        {
                            EditorUtility.SetDirty(atlas);
                            sprite.update = nu;
                        }
                    }

                    int count = (_delegate == null) ? 0 : _delegate.GetReferenceCount(origin);
                    if (count > 0)
                    {
                        Rect re = args.rowRect;
                        re.x     = re.xMax - 60;
                        re.width = 60;
                        Color c = DefaultStyles.label.normal.textColor;
                        DefaultStyles.label.normal.textColor = Color.red;
                        EditorGUI.LabelField(re, string.Format("{0}", count));
                        DefaultStyles.label.normal.textColor = c;
                    }
                }
            }
        }