Exemplo n.º 1
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    public override void OnInspectorGUI()
    {
        // ========================================================
        // Base GUI
        // ========================================================

        base.OnInspectorGUI();
        GUILayout.Space(20);

        // ========================================================
        // init values
        // ========================================================

        //
        bool needRebuild = false;

        editSprite.spanim = editSprite.GetComponent <exSpriteAnimation>();

        // get ElementInfo first
        Texture2D editTexture = exEditorHelper.LoadAssetFromGUID <Texture2D>(editSprite.textureGUID);

        // ========================================================
        // Texture preview (input)
        // ========================================================

        bool textureChanged = false;

        GUI.enabled = !inAnimMode;
        GUILayout.BeginHorizontal();
        GUILayout.Space(20);
        EditorGUIUtility.LookLikeControls();
        Texture2D newTexture = (Texture2D)EditorGUILayout.ObjectField(editTexture
                                                                      , typeof(Texture2D)
                                                                      , false
                                                                      , GUILayout.Width(100)
                                                                      , GUILayout.Height(100)
                                                                      );

        EditorGUIUtility.LookLikeInspector();
        if (newTexture != editTexture)
        {
            editTexture            = newTexture;
            editSprite.textureGUID = exEditorHelper.AssetToGUID(editTexture);
            textureChanged         = true;
            GUI.changed            = true;
        }
        GUILayout.Space(10);
        GUILayout.BeginVertical();
        GUILayout.Space(90);
        GUILayout.Label(editTexture ? editTexture.name : "None");
        GUILayout.EndVertical();
        GUILayout.EndHorizontal();
        GUI.enabled = true;

        // ========================================================
        // get atlas element info from atlas database
        // ========================================================

        exAtlas editAtlas = null;
        int     editIndex = -1;

        exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(editSprite.textureGUID);
        if (elInfo != null)
        {
            editAtlas = exEditorHelper.LoadAssetFromGUID <exAtlas>(elInfo.guidAtlas);
            editIndex = elInfo.indexInAtlas;
        }
        bool useAtlas = editAtlas != null && editIndex != -1;

        // get atlas and index from textureGUID
        if (!EditorApplication.isPlaying)
        {
            // if we don't use atlas and current edit target use atlas, clear it.
            if (editSprite.useAtlas != useAtlas)
            {
                editSprite.Clear();
            }

            // if we use atlas, check if the atlas,index changes
            if (useAtlas)
            {
                if (editAtlas != editSprite.atlas ||
                    editIndex != editSprite.index)
                {
                    editSprite.SetSprite(editAtlas, editIndex);
                    GUI.changed = true;
                }
            }

            // check if we are first time assignment
            if (useAtlas || editTexture != null)
            {
                if (isPrefab == false && editSprite.meshFilter.sharedMesh == null)
                {
                    needRebuild = true;
                }
            }
        }

        // ========================================================
        // get trimTexture
        // ========================================================

        GUI.enabled = !inAnimMode && !useAtlas;
        bool newTrimTexture = EditorGUILayout.Toggle("Trim Texture", editSprite.trimTexture);

        if (!useAtlas &&
            (textureChanged || newTrimTexture != editSprite.trimTexture))
        {
            editSprite.GetComponent <Renderer>().sharedMaterial = exEditorHelper.GetDefaultMaterial(editTexture, editTexture.name);
            editSprite.trimTexture = newTrimTexture;

            // get trimUV
            Rect trimUV = new Rect(0, 0, 1, 1);
            if (editTexture != null)
            {
                if (editSprite.trimTexture)
                {
                    if (exTextureHelper.IsValidForAtlas(editTexture) == false)
                    {
                        exTextureHelper.ImportTextureForAtlas(editTexture);
                    }
                    trimUV = exTextureHelper.GetTrimTextureRect(editTexture);
                    trimUV = new Rect(trimUV.x / editTexture.width,
                                      (editTexture.height - trimUV.height - trimUV.y) / editTexture.height,
                                      trimUV.width / editTexture.width,
                                      trimUV.height / editTexture.height);
                }

                if (editSprite.customSize == false)
                {
                    editSprite.width  = trimUV.width * editTexture.width;
                    editSprite.height = trimUV.height * editTexture.height;
                }
            }
            editSprite.trimUV       = trimUV;
            editSprite.updateFlags |= exPlane.UpdateFlags.UV;
            editSprite.updateFlags |= exPlane.UpdateFlags.Vertex;
        }
        GUI.enabled = true;

        // ========================================================
        // color
        // ========================================================

        editSprite.color = EditorGUILayout.ColorField("Color", editSprite.color);

        // ========================================================
        // atlas & index
        // ========================================================

        GUILayout.BeginHorizontal();
        GUI.enabled = false;
        EditorGUILayout.ObjectField("Atlas"
                                    , editSprite.atlas
                                    , typeof(exAtlas)
                                    , false
                                    );
        GUI.enabled = true;

        GUI.enabled = !inAnimMode;
        if (GUILayout.Button("Edit...", GUILayout.Width(40), GUILayout.Height(15)))
        {
            exAtlasEditor editor = exAtlasEditor.NewWindow();
            editor.Edit(editSprite.atlas);
        }
        GUI.enabled = true;
        GUILayout.EndHorizontal();

        GUI.enabled = false;
        EditorGUILayout.IntField("Index", editSprite.index);
        GUI.enabled = true;

        // ========================================================
        // custom size
        // ========================================================

        GUI.enabled           = !inAnimMode;
        editSprite.customSize = EditorGUILayout.Toggle("Custom Size", editSprite.customSize);
        GUI.enabled           = true;

        // ========================================================
        // width & height
        // ========================================================

        ++EditorGUI.indentLevel;
        GUI.enabled = !inAnimMode && editSprite.customSize;
        // width
        float newWidth = EditorGUILayout.FloatField("Width", editSprite.width);

        if (newWidth != editSprite.width)
        {
            if (newWidth < 1.0f)
            {
                newWidth = 1.0f;
            }
            editSprite.width = newWidth;
        }

        // height
        float newHeight = EditorGUILayout.FloatField("Height", editSprite.height);

        if (newHeight != editSprite.height)
        {
            if (newHeight < 1.0f)
            {
                newHeight = 1.0f;
            }
            editSprite.height = newHeight;
        }
        --EditorGUI.indentLevel;

        // ========================================================
        // Reset to original
        // ========================================================

        GUILayout.BeginHorizontal();
        GUILayout.Space(30);
        if (GUILayout.Button("Reset", GUILayout.Width(50)))
        {
            if (useAtlas)
            {
                exAtlas.Element el = editAtlas.elements[editIndex];
                editSprite.width  = el.trimRect.width;
                editSprite.height = el.trimRect.height;
            }
            else if (editTexture)
            {
                editSprite.width  = editSprite.trimUV.width * editTexture.width;
                editSprite.height = editSprite.trimUV.height * editTexture.height;
            }
            GUI.changed = true;
        }
        GUILayout.EndHorizontal();
        GUI.enabled = true;

        // ========================================================
        // Rebuild button
        // ========================================================

        GUI.enabled = !inAnimMode;
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        if (GUILayout.Button("Rebuild...", GUILayout.Height(20)))
        {
            needRebuild = true;
        }
        GUILayout.EndHorizontal();
        GUI.enabled = true;
        GUILayout.Space(5);

        // if dirty, build it.
        if (!EditorApplication.isPlaying && !AnimationUtility.InAnimationMode())
        {
            if (needRebuild)
            {
                EditorUtility.ClearProgressBar();
                editSprite.Build(editTexture);
            }
            else if (GUI.changed)
            {
                if (editSprite.meshFilter.sharedMesh != null)
                {
                    editSprite.UpdateMesh(editSprite.meshFilter.sharedMesh);
                }
                EditorUtility.SetDirty(editSprite);
            }
        }
    }
Exemplo n.º 2
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    public override void OnInspectorGUI()
    {
        exSprite sprite             = editSpAnim.GetComponent <exSprite>();
        bool     checkDefaultSprite = (sprite != null) && string.IsNullOrEmpty(sprite.textureGUID);

        EditorGUIUtility.LookLikeInspector();
        EditorGUILayout.Space();
        EditorGUI.indentLevel = 1;

        // ========================================================
        // Play Automatically
        // ========================================================

        editSpAnim.playAutomatically = EditorGUILayout.Toggle("Play Automatically", editSpAnim.playAutomatically);

        // ========================================================
        // Default Animation
        // ========================================================

        GUILayout.BeginHorizontal();
        editSpAnim.defaultAnimation = (exSpriteAnimClip)EditorGUILayout.ObjectField("Default Animation"
                                                                                    , editSpAnim.defaultAnimation
                                                                                    , typeof(exSpriteAnimClip)
                                                                                    , false
                                                                                    );
        if (GUILayout.Button("Edit...", GUILayout.Width(40), GUILayout.Height(15)))
        {
            exSpriteAnimClipEditor editor = exSpriteAnimClipEditor.NewWindow();
            editor.Edit(editSpAnim.defaultAnimation);
        }
        if (editSpAnim.defaultAnimation != null)
        {
            int idx = editSpAnim.animations.IndexOf(editSpAnim.defaultAnimation);
            if (idx == -1)
            {
                editSpAnim.animations.Add(editSpAnim.defaultAnimation);
            }
        }
        GUILayout.EndHorizontal();

        // ========================================================
        // Animations
        // ========================================================

        Rect lastRect = new Rect(0, 0, 1, 1);
        Rect dropRect = new Rect(0, 0, 1, 1);

        EditorGUI.indentLevel = 0;
        showAnimations        = EditorGUILayout.Foldout(showAnimations, "Animations");
        if (showAnimations)
        {
            EditorGUI.indentLevel = 2;
            // int count = EditorGUILayout.IntField ( "Size", editSpAnim.animations.Count );
            int count = exEditorHelper.IntField("Size", editSpAnim.animations.Count);
            lastRect        = GUILayoutUtility.GetLastRect();
            dropRect.height = lastRect.yMax - dropRect.y;
            count           = Mathf.Max(count, 0);

            if (count != editSpAnim.animations.Count)
            {
                //
                if (count > editSpAnim.animations.Count)
                {
                    int num = count - editSpAnim.animations.Count;
                    for (int i = 0; i < num; ++i)
                    {
                        editSpAnim.animations.Add(null);
                    }
                }
                else
                {
                    editSpAnim.animations.RemoveRange(count, editSpAnim.animations.Count - count);
                }

                //
                GUI.changed = true;
            }

            int idxRemoved = -1;
            for (int i = 0; i < editSpAnim.animations.Count; ++i)
            {
                GUILayout.BeginHorizontal();
                editSpAnim.animations[i] =
                    (exSpriteAnimClip)EditorGUILayout.ObjectField("[" + i + "]"
                                                                  , editSpAnim.animations[i]
                                                                  , typeof(exSpriteAnimClip)
                                                                  , false
                                                                  );
                if (GUILayout.Button("-", GUILayout.Width(15), GUILayout.Height(15)))
                {
                    idxRemoved = i;
                }
                if (GUILayout.Button("Edit...", GUILayout.Width(40), GUILayout.Height(15)))
                {
                    exSpriteAnimClipEditor editor = exSpriteAnimClipEditor.NewWindow();
                    editor.Edit(editSpAnim.animations[i]);
                }
                // TODO: I think we can instantiate animation state {
                // EditorGUI.indentLevel += 1;
                // // TODO:
                // EditorGUI.indentLevel -= 1;
                // } TODO end
                GUILayout.EndHorizontal();
            }

            // if we have item to remove
            if (idxRemoved != -1)
            {
                exSpriteAnimClip animClip = editSpAnim.animations[idxRemoved];
                editSpAnim.animations.RemoveAt(idxRemoved);
                if (animClip == editSpAnim.defaultAnimation)
                {
                    editSpAnim.defaultAnimation = null;
                }
            }

            EditorGUI.indentLevel = 1;
            EditorGUILayout.Space();

            lastRect        = GUILayoutUtility.GetLastRect();
            dropRect.x      = lastRect.x + 30;
            dropRect.y      = lastRect.yMax;
            dropRect.width  = lastRect.xMax - 30 - 4;
            dropRect.height = 20;

            exEditorHelper.DrawRect(dropRect, new Color(0.2f, 0.2f, 0.2f, 1.0f), new Color(0.5f, 0.5f, 0.5f, 1.0f));
            GUILayout.Space(20);

            // ========================================================
            // drag and drop
            // ========================================================

            if (dropRect.Contains(Event.current.mousePosition))
            {
                if (Event.current.type == EventType.DragUpdated)
                {
                    // Show a copy icon on the drag
                    foreach (Object o in DragAndDrop.objectReferences)
                    {
                        if (o is exSpriteAnimClip)
                        {
                            DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                            break;
                        }
                    }
                }
                else if (Event.current.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();
                    foreach (Object o in DragAndDrop.objectReferences)
                    {
                        if (o is exSpriteAnimClip)
                        {
                            editSpAnim.animations.Add(o as exSpriteAnimClip);
                        }
                    }
                    GUI.changed = true;
                }
            }
        }
        EditorGUILayout.Space();

        // TODO: FIXME {
        // ========================================================
        //
        // ========================================================

        if (checkDefaultSprite &&
            editSpAnim.animations.Count > 0 &&
            editSpAnim.animations[0] != null &&
            editSpAnim.animations[0].frameInfos.Count > 0)
        {
            exSpriteAnimClip.FrameInfo fi = editSpAnim.animations[0].frameInfos[0];
            sprite.textureGUID = fi.textureGUID;
            sprite.SetSprite(fi.atlas, fi.index);
            sprite.Build();
        }
        // } TODO end

        // ========================================================
        // set dirty if anything changed
        // ========================================================

        if (GUI.changed)
        {
            EditorUtility.SetDirty(editSpAnim);
        }
    }
Exemplo n.º 3
0
    // ------------------------------------------------------------------
    // Desc:
    // ------------------------------------------------------------------

    public static void Rebuild(this exSprite _sprite)
    {
        Texture2D texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(_sprite.textureGUID);

        _sprite.Build(texture);
    }
Exemplo n.º 4
0
    // ------------------------------------------------------------------
    /// \param _sprites the list of sprites to rebuild
    /// rebuild the listed sprites
    // ------------------------------------------------------------------

    public static void RebuildSprites(exSpriteBase[] _sprites)
    {
        try {
            EditorUtility.DisplayProgressBar("Rebuild Scene Sprites...",
                                             "Rebuild Scene Sprites...",
                                             0.5f);

            for (int i = 0; i < _sprites.Length; ++i)
            {
                exSpriteBase spBase = _sprites[i];
                // DISABLE: it is too slow {
                // float progress = (float)i/(float)_sprites.Length;
                // EditorUtility.DisplayProgressBar( "Rebuild Scene Sprites...",
                //                                   "Build Sprite " + spBase.gameObject.name, progress );
                // } DISABLE end

                // if sprite
                if (spBase is exSprite)
                {
                    exSprite sp = spBase as exSprite;
                    exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(sp.textureGUID);
                    exSpriteEditor.UpdateAtlas(sp, elInfo);

                    Texture2D texture = null;
                    if (sp.useAtlas == false)
                    {
                        texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(sp.textureGUID);
                    }
                    sp.Build(texture);
                }

#if !(EX2D_EVALUATE)
                // if sprite font
                if (spBase is exSpriteFont)
                {
                    exSpriteFont spFont = spBase as exSpriteFont;
                    spFont.Build();
                }

                // if sprite border
                if (spBase is exSpriteBorder)
                {
                    exSpriteBorder spBorder = spBase as exSpriteBorder;

                    Texture2D texture = null;
                    if (spBorder.guiBorder)
                    {
                        exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(spBorder.guiBorder.textureGUID);
                        exSpriteBorderEditor.UpdateAtlas(spBorder, elInfo);

                        if (spBorder.useAtlas == false)
                        {
                            texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(spBorder.guiBorder.textureGUID);
                        }
                    }
                    spBorder.Build(texture);
                }
#endif // EX2D_EVALUATE
            }
            EditorUtility.ClearProgressBar();
        }
        catch (System.Exception) {
            EditorUtility.ClearProgressBar();
            throw;
        }
    }
Exemplo n.º 5
0
    // ------------------------------------------------------------------
    /// \param _atlasInfoGUIDs the list of atlas info guid
    /// update sprites in current scene and in prefab by atlas info list
    // ------------------------------------------------------------------

    public static void UpdateSprites(List <string> _atlasInfoGUIDs)
    {
        if (_atlasInfoGUIDs.Count == 0)
        {
            return;
        }

        try {
            EditorUtility.DisplayProgressBar("Update Scene Sprites With Changed Atlas...", "Scanning...", 0.0f);
            // exSpriteBase[] sprites = GameObject.FindObjectsOfType(typeof(exSpriteBase)) as exSpriteBase[];
            exSpriteBase[] sprites = Resources.FindObjectsOfTypeAll(typeof(exSpriteBase)) as exSpriteBase[];
            for (int i = 0; i < sprites.Length; ++i)
            {
                exSpriteBase spBase = sprites[i];

                // ========================================================
                // exSprite
                // ========================================================

                if (spBase is exSprite)
                {
                    exSprite sp = spBase as exSprite;
                    exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(sp.textureGUID);
                    bool needRebuild             = false;

                    // NOTE: we test sp.index is -1 or not instead of test atlas is null, because it is possible we delete an atlas and it will always be null
                    if (elInfo != null)
                    {
                        if (sp.index == -1)
                        {
                            needRebuild = true;
                        }
                        else
                        {
                            // find if the sp's atalsInfo need rebuild
                            foreach (string guidAtlasInfo in _atlasInfoGUIDs)
                            {
                                if (elInfo.guidAtlasInfo == guidAtlasInfo)
                                {
                                    needRebuild = true;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (sp.index != -1)
                        {
                            needRebuild = true;
                        }
                    }

                    //
                    if (needRebuild)
                    {
                        exSpriteEditor.UpdateAtlas(sp, elInfo);
#if UNITY_3_4
                        bool isPrefab = (EditorUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#else
                        bool isPrefab = (PrefabUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#endif
                        if (isPrefab == false)
                        {
                            Texture2D texture = null;
                            if (sp.index == -1)
                            {
                                texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(sp.textureGUID);
                            }
                            sp.Build(texture);
                        }
                        EditorUtility.SetDirty(sp);
                    }
                }

#if !(EX2D_EVALUATE)
                // ========================================================
                // exSpriteFont
                // ========================================================

                if (spBase is exSpriteFont)
                {
                    exSpriteFont spFont = spBase as exSpriteFont;

                    //
                    bool needRebuild = false;
                    if (spFont.fontInfo == null)
                    {
                        needRebuild = true;
                    }
                    else
                    {
                        foreach (string guidAtlasInfo in _atlasInfoGUIDs)
                        {
                            exAtlasInfo atlasInfo = exEditorHelper.LoadAssetFromGUID <exAtlasInfo>(guidAtlasInfo);
                            // NOTE: it is possible we process this in delete stage
                            if (atlasInfo == null)
                            {
                                continue;
                            }

                            foreach (exBitmapFont bmfont in atlasInfo.bitmapFonts)
                            {
                                if (spFont.fontInfo == bmfont)
                                {
                                    needRebuild = true;
                                    break;
                                }
                            }
                        }
                    }

                    //
                    if (needRebuild)
                    {
                        spFont.Build();
                    }
                }

                // ========================================================
                // exSpriteBorder
                // ========================================================

                if (spBase is exSpriteBorder)
                {
                    exSpriteBorder spBorder = spBase as exSpriteBorder;
                    if (spBorder.guiBorder != null)
                    {
                        exAtlasDB.ElementInfo elInfo = exAtlasDB.GetElementInfo(spBorder.guiBorder.textureGUID);
                        bool needRebuild             = false;

                        // NOTE: we test spBorder.index is -1 or not instead of test atlas is null, because it is possible we delete an atlas and it will always be null
                        if (elInfo != null)
                        {
                            if (spBorder.index == -1)
                            {
                                needRebuild = true;
                            }
                            else
                            {
                                // find if the spBorder's atalsInfo need rebuild
                                foreach (string guidAtlasInfo in _atlasInfoGUIDs)
                                {
                                    if (elInfo.guidAtlasInfo == guidAtlasInfo)
                                    {
                                        needRebuild = true;
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (spBorder.index != -1)
                            {
                                needRebuild = true;
                            }
                        }

                        //
                        if (needRebuild)
                        {
                            exSpriteBorderEditor.UpdateAtlas(spBorder, elInfo);
#if UNITY_3_4
                            bool isPrefab = (EditorUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#else
                            bool isPrefab = (PrefabUtility.GetPrefabType(spBase) == PrefabType.Prefab);
#endif
                            if (isPrefab == false)
                            {
                                Texture2D texture = null;
                                if (spBorder.index == -1)
                                {
                                    texture = exEditorHelper.LoadAssetFromGUID <Texture2D>(spBorder.guiBorder.textureGUID);
                                }
                                spBorder.Build(texture);
                            }
                            EditorUtility.SetDirty(spBorder);
                        }
                    }
                }
#endif // !(EX2D_EVALUATE)

                // DISABLE: it is too slow {
                // float progress = (float)i/(float)sprites.Length;
                // EditorUtility.DisplayProgressBar( "Update Scene Sprites...",
                //                                   "Update Sprite " + spBase.gameObject.name, progress );
                // } DISABLE end
            }
            EditorUtility.ClearProgressBar();
        }
        catch (System.Exception) {
            EditorUtility.ClearProgressBar();
            throw;
        }
    }