Ejemplo n.º 1
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();
            }
        }
    }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
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();
    }