Beispiel #1
0
    /*
     * // Get the unique sorting layer IDs -- tossed this in for good measure
     * static private int[] GetSortingLayerUniqueIDs()
     * {
     *      Type internalEditorUtilityType = typeof(InternalEditorUtility);
     *      PropertyInfo sortingLayerUniqueIDsProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
     *      return (int[])sortingLayerUniqueIDsProperty.GetValue(null, new object[0]);
     * }
     */

    static public int GetDrawCalls(bool log)
    {
        if (orderLayerNames.Count == 0)
        {
            return(0);
        }

        List <SRInfo> allsr = new List <SRInfo>();

        foreach (string l in orderLayerNames)
        {
            if (!_Layers.ContainsKey(l))
            {
                continue;
            }

            SRLayer srl = _Layers[l];
            foreach (List <SRInfo> srlist in srl.Orders.AsReadOnly())
            {
                srlist.Sort();
                allsr.AddRange(srlist);
            }
        }

        if (allsr.Count == 0)
        {
            return(0);
        }

        int    dc       = 0;
        string curTxtNM = string.Empty;

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (SRInfo sr in allsr)
        {
            sb.AppendLine(string.Format("{0} - {1} - {2}", sr.LayerName, sr.Index, sr.TextureName));
            if ((dc > 0) && (string.IsNullOrEmpty(sr.TextureName)))
            {
                continue;
            }

            if (string.Compare(curTxtNM, sr.TextureName,
                               StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                continue;
            }

            curTxtNM = sr.TextureName;
            dc++;
        }
        if (log)
        {
            Debug.Log(sb.ToString());
        }
        return(dc);
    }
Beispiel #2
0
    void OnGUI()
    {
        if (_dirty)
        {
            dicKeys.Clear();
            _dirty = false;
        }

        EditorGUILayout.BeginVertical();
        _scroll = GUILayout.BeginScrollView(_scroll);

        #region target
        // set, select gameobject
        EditorGUILayout.BeginHorizontal();
        goTarget = EditorGUILayout.ObjectField("Target", goTarget, typeof(GameObject), true) as GameObject;
        EditorGUILayout.EndHorizontal();

        // revert, apply prefab
        DrawGOBehavior(goTarget);
        #endregion target

        if (goTarget)
        {
            // delete key if not same valid GameObject
            if (_instanceID != goTarget.GetInstanceID())
            {
                _instanceID = goTarget.GetInstanceID();
                foreach (string tmpkey in dicKeys.Keys)
                {
                    if (EditorPrefs.HasKey(tmpkey))
                    {
                        EditorPrefs.DeleteKey(tmpkey);
                    }
                }
            }

            // update sprites inforamtion
            SpriteManager.Update(goTarget);

            #region draw call information
            GUILayout.BeginHorizontal();
            GUILayout.Label(string.Format("Draw calls: {0}", SpriteManager.DrawCalls), GUILayout.Width(200f));
            if (GUILayout.Button("Get Draw Call"))
            {
                SpriteManager.GetDrawCalls(true);
            }
            GUILayout.EndHorizontal();
            #endregion draw call information

            for (int i = 0; i < SpriteManager.Count; i++)
            {
                #region layer main
                SRLayer layer = SpriteManager.list[i];

                // set color
                GUI.backgroundColor = allColors[i % allColors.Length];
                GUI.color           = Color.white;
                Color oldColor = defaultColor;

                #region layer field
                GUILayout.BeginHorizontal();

                // layer field
                int    currentLayer = layer.Index;
                string keyStr       = string.Format("L_{0}", currentLayer);
                int    massLayer    = currentLayer;
                if (dicKeys.ContainsKey(keyStr))
                {
                    massLayer = dicKeys[keyStr];
                }
                GUILayout.Label("Layer ID", GUILayout.Width(60f));
                dicKeys[keyStr] = EditorGUILayout.IntField(massLayer, GUILayout.Width(50f));

                GUILayout.EndHorizontal();
                #endregion layer field

                #region layer apply/reset
                if (massLayer != currentLayer)
                {
                    GUILayout.BeginHorizontal();
                    oldColor = GUI.backgroundColor;

                    // apply
                    GUI.backgroundColor = new Color(0.4f, 1f, 0.4f);
                    if (GUILayout.Button("Apply", GUILayout.Width(100f)))
                    {
                        // set new layer
                        for (int o = 0; o < layer.Orders.Count; o++)
                        {
                            tempOrders = layer.Orders[o];
                            for (int iO = 0; iO < tempOrders.Count; iO++)
                            {
                                tempOrders[iO].SetLayerID(massLayer);
                            }
                        }

                        // delete key
                        EditorPrefs.DeleteKey(keyStr);
                        for (int o = 0; o < layer.Orders.Count; o++)
                        {
                            tempOrders = layer.Orders[o];
                            if (tempOrders.Count == 0)
                            {
                                continue;
                            }

                            string okey = string.Format("L_{0}O_{1}", currentLayer, tempOrders[0].Index);
                            EditorPrefs.DeleteKey(okey);
                        }

                        _dirty = true;
                        GUI.FocusControl(null);
                        EditorUtility.SetDirty(goTarget);
                    }

                    // reset
                    GUI.backgroundColor = new Color(1f, 0.8f, 0.8f);
                    if (GUILayout.Button("Reset", GUILayout.Width(100f)))
                    {
                        dicKeys[keyStr] = currentLayer;
                        GUI.FocusControl(null);
                        EditorUtility.SetDirty(goTarget);
                    }

                    GUI.backgroundColor = oldColor;
                    GUILayout.EndHorizontal();
                }
                #endregion layer apply/reset

                // layer fold
                string headerStr      = string.Format("<b>Sorting Layer {0} ({1}) - Count: {2}</b>", layer.Index, layer.LayerName, layer.Count);
                bool   layerFoldedOut = DrawHeader(headerStr, keyStr);
                if (!layerFoldedOut)
                {
                    continue;
                }
                #endregion layer main

                StyleEx.BeginContent();

                #region orders main
                for (int o = 0; o < layer.Orders.Count; o++)
                {
                    tempOrders = layer.Orders[o];
                    if (tempOrders.Count == 0)
                    {
                        continue;
                    }

                    #region order field, fold
                    GUILayout.BeginHorizontal();

                    // order field
                    int currentOrder = tempOrders[0].Index;
                    keyStr = string.Format("L_{0}O_{1}", currentLayer, currentOrder);
                    bool orderFoldedOut = EditorPrefs.GetBool(keyStr, false);
                    int  massOrder      = currentOrder;
                    if (dicKeys.ContainsKey(keyStr))
                    {
                        massOrder = dicKeys[keyStr];
                    }
                    GUILayout.Label("Order", GUILayout.Width(40f));
                    dicKeys[keyStr] = EditorGUILayout.IntField(massOrder, GUILayout.Width(50f));

                    // order fold
                    string collapserName = string.Format("<b>Cont: {0}</b> - Click to {1}", tempOrders.Count, (orderFoldedOut ? "collapse" : "expand"));
                    bool   foldedOut     = DrawOrderCollapser(collapserName, keyStr, orderFoldedOut);

                    GUILayout.EndHorizontal();
                    #endregion order field, fold

                    #region order apply/reset
                    if (massOrder != currentOrder)
                    {
                        GUILayout.BeginHorizontal();
                        oldColor = GUI.backgroundColor;

                        // apply
                        GUI.backgroundColor = new Color(0.4f, 1f, 0.4f);
                        if (GUILayout.Button("Apply", GUILayout.Width(100f)))
                        {
                            // set new order
                            for (int iO = 0; iO < tempOrders.Count; iO++)
                            {
                                tempOrders[iO].SetOrder(massOrder);
                            }

                            // delete key
                            EditorPrefs.DeleteKey(keyStr);

                            _dirty = true;
                            GUI.FocusControl(null);
                            EditorUtility.SetDirty(goTarget);
                        }

                        // reset
                        GUI.backgroundColor = new Color(1f, 0.8f, 0.8f);
                        if (GUILayout.Button("Reset", GUILayout.Width(100f)))
                        {
                            dicKeys[keyStr] = currentOrder;
                            GUI.FocusControl(null);
                            EditorUtility.SetDirty(goTarget);
                        }

                        GUI.backgroundColor = oldColor;
                        GUILayout.EndHorizontal();
                    }
                    #endregion order apply/reset

                    #region same set of order
                    if (foldedOut)
                    {
                        var alignBefore = GUI.skin.button.alignment;
                        oldColor = GUI.backgroundColor;
                        GUI.skin.button.alignment = TextAnchor.MiddleLeft;
                        // draw GameObject button
                        for (int iW = 0; iW < tempOrders.Count; iW++)
                        {
                            GUILayout.BeginHorizontal();
                            GUILayout.Space(10f);

                            // button to select gameobject
                            GUI.backgroundColor = defaultColor;
                            if (GUILayout.Button(tempOrders[iW].Name, GUILayout.ExpandWidth(false)))
                            {
                                Selection.activeGameObject = tempOrders[iW].gameObject;
                            }

                            // order field
                            GUI.backgroundColor = oldColor;
                            GUILayout.Label("Order", GUILayout.Width(40f));
                            keyStr       = tempOrders[iW].SpriteRender.GetInstanceID().ToString();
                            currentOrder = tempOrders[iW].Index;
                            int singleOrder = currentOrder;
                            if (dicKeys.ContainsKey(keyStr))
                            {
                                singleOrder = dicKeys[keyStr];
                            }
                            dicKeys[keyStr] = EditorGUILayout.IntField(singleOrder, GUILayout.Width(50f));

                            #region single order apply/reset, show texture
                            GUI.skin.button.alignment = alignBefore;
                            if (singleOrder != currentOrder)
                            {
                                oldColor = GUI.backgroundColor;

                                // apply
                                GUI.backgroundColor = new Color(0.4f, 1f, 0.4f);
                                if (GUILayout.Button("Apply", GUILayout.Width(100f)))
                                {
                                    // set new order
                                    tempOrders[iW].SetOrder(singleOrder);
                                    // delete key
                                    EditorPrefs.DeleteKey(keyStr);

                                    _dirty = true;
                                    GUI.FocusControl(null);
                                    EditorUtility.SetDirty(goTarget);
                                }

                                // reset
                                GUI.backgroundColor = new Color(1f, 0.8f, 0.8f);
                                if (GUILayout.Button("Reset", GUILayout.Width(100f)))
                                {
                                    dicKeys[keyStr] = currentOrder;
                                    GUI.FocusControl(null);
                                    EditorUtility.SetDirty(goTarget);
                                }

                                GUI.backgroundColor = oldColor;
                            }
                            else
                            {
                                // todo show texture
                            }
                            #endregion single order apply/reset, show texture

                            GUILayout.EndHorizontal();
                        }
                        GUI.backgroundColor       = oldColor;
                        GUI.skin.button.alignment = alignBefore;
                    }
                    #endregion same set of order

                    GUILayout.Space(3f);
                }
                #endregion orders main

                StyleEx.EndContent();
            }
            GUI.color           = Color.white;
            GUI.backgroundColor = Color.white;
        }

        GUILayout.EndScrollView();
        EditorGUILayout.EndVertical();
    }
Beispiel #3
0
    static public void Update(SpriteRenderer[] sr)
    {
        _Layers.Clear();
        // set all SpriteRenderer information
        for (int i = 0; i < sr.Length; i++)
        {
            int    layerID   = sr[i].sortingLayerID;
            string layerName = sr[i].sortingLayerName.ToLower();
            if ((layerID == 0) && (string.IsNullOrEmpty(layerName)))
            {
                layerName = DEFAULTLAYERNAME;
            }

            SRLayer tmp = null;
//			if(!_layers.ContainsKey(layerID))
            if (!_Layers.ContainsKey(layerName))
            {
                tmp = new SRLayer(layerID, sr[i].gameObject.name, sr[i]);
//				_layers.Add(layerID, tmp);
                _Layers.Add(layerName, tmp);
            }

//			tmp = _layers[layerID];
            tmp = _Layers[layerName];
            string name = string.Format("{0}/{1}", sr[i].transform.parent.name, sr[i].gameObject.name);
            tmp.Add(sr[i].sortingOrder, name, sr[i]);
        }

        // sorting Orders data
        foreach (SRLayer data in _Layers.Values)
        {
            data.Orders.Sort(delegate(List <SRInfo> x, List <SRInfo> y)
            {
                if (x == null && y == null)
                {
                    return(0);
                }
                else if (x.Count == 0 && y.Count == 0)
                {
                    return(0);
                }
                else if (x.Count == 0)
                {
                    return(-1);
                }
                else if (y.Count == 0)
                {
                    return(1);
                }
                else if (x[0].Index == y[0].Index)
                {
                    return(0);
                }
                else if (x[0].Index > y[0].Index)
                {
                    return(1);
                }
                else if (x[0].Index < y[0].Index)
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            });

            foreach (List <SRInfo> o in data.Orders)
            {
                o.Sort();
            }
        }
    }