Example #1
0
    /// <summary>
    /// Draw the specified sprite.
    /// </summary>

    public static void DrawTexture(Texture2D tex, Rect rect, Rect uv, Color color, Material mat)
    {
        int w = Mathf.RoundToInt(tex.width * uv.width);
        int h = Mathf.RoundToInt(tex.height * uv.height);

        // Create the texture rectangle that is centered inside rect.
        Rect outerRect = rect;

        outerRect.width  = w;
        outerRect.height = h;

        if (outerRect.width > 0f)
        {
            float f = rect.width / outerRect.width;
            outerRect.width  *= f;
            outerRect.height *= f;
        }

        if (rect.height > outerRect.height)
        {
            outerRect.y += (rect.height - outerRect.height) * 0.5f;
        }
        else if (outerRect.height > rect.height)
        {
            float f = rect.height / outerRect.height;
            outerRect.width  *= f;
            outerRect.height *= f;
        }

        if (rect.width > outerRect.width)
        {
            outerRect.x += (rect.width - outerRect.width) * 0.5f;
        }

        // Draw the background
        WrapGUITools.DrawTiledTexture(outerRect, WrapGUITools.backdropTexture);

        // Draw the sprite
        GUI.color = color;

        if (mat == null)
        {
            GUI.DrawTextureWithTexCoords(outerRect, tex, uv, true);
        }
        else
        {
            // NOTE: There is an issue in Unity that prevents it from clipping the drawn preview
            // using BeginGroup/EndGroup, and there is no way to specify a UV rect... le'suq.
            UnityEditor.EditorGUI.DrawPreviewTexture(outerRect, tex, mat);
        }
        GUI.color = Color.white;

        // Draw the lines around the sprite
        Handles.color = Color.black;
        Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMin, outerRect.yMax));
        Handles.DrawLine(new Vector3(outerRect.xMax, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMax));
        Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMin), new Vector3(outerRect.xMax, outerRect.yMin));
        Handles.DrawLine(new Vector3(outerRect.xMin, outerRect.yMax), new Vector3(outerRect.xMax, outerRect.yMax));

        // Sprite size label
        string text = string.Format("Texture Size: {0}x{1}", w, h);

        EditorGUI.DropShadowLabel(GUILayoutUtility.GetRect(Screen.width, 18f), text);
    }
Example #2
0
    void OnGUI()
    {
        if(_isCompiling != EditorApplication.isCompiling){
            _isCompiling = EditorApplication.isCompiling;
            ReloadWrap();
        }

        GUI.backgroundColor = Color.green;
        if(GUILayout.Button("Register All Types", GUILayout.Width(200))){
            UpdateRegisterTypes();
        }
        GUI.backgroundColor = Color.white;

        if(WrapGUITools.DrawHeader("Option")) {
            WrapGUITools.BeginContents();

            //TODO 这里可以输入黑名单和自动添加名单
            int height = Mathf.Min(Mathf.Max(_whiteList.Count, 2) * 20,200);
            GUILayout.BeginHorizontal();
            {
                GUILayout.BeginVertical();
                GUILayout.Label("BlackList (Class)");
                _classBlackScroll = GUILayout.BeginScrollView(_classBlackScroll, GUILayout.Width(Screen.width * 0.5f - 5), GUILayout.Height(height * 0.8f));
                for(int i = 0; i < _classBlackList.Count; i++) {
                    GUILayout.BeginHorizontal();
                    _classBlackList[i] = EditorGUILayout.TextField(_classBlackList[i]);
                    if(GUILayout.Button("X", GUILayout.Width(25))) {
                        _classBlackList.RemoveAt(i);
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndScrollView();
                if(GUILayout.Button("Add")) {
                    _classBlackList.Add("");
                    _classBlackScroll += new Vector2(0,20);
                }

                GUILayout.Space(5);

                GUILayout.Label("BlackList (Member)");
                _memberBlackScroll = GUILayout.BeginScrollView(_memberBlackScroll, GUILayout.Width(Screen.width * 0.5f - 5), GUILayout.Height(height * 0.4f));
                for(int i = 0; i < _memberBlackList.Count; i++) {
                    GUILayout.BeginHorizontal();
                    _memberBlackList[i] = EditorGUILayout.TextField(_memberBlackList[i]);
                    if(GUILayout.Button("X", GUILayout.Width(25))) {
                        _memberBlackList.RemoveAt(i);
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndScrollView();
                if(GUILayout.Button("Add")) {
                    _memberBlackList.Add("");
                    _memberBlackScroll += new Vector2(0,20);
                }
                GUILayout.EndVertical();
            }

            GUILayout.Space(5);

            {
                GUILayout.BeginVertical();
                GUILayout.Label("WhiteList (Class)");
                _whiteScroll = GUILayout.BeginScrollView(_whiteScroll, GUILayout.Width(Screen.width * 0.5f - 5), GUILayout.Height(height));
                for(int i = 0; i < _whiteList.Count; i++) {
                    GUILayout.BeginHorizontal();
                    _whiteList[i] = EditorGUILayout.TextField(_whiteList[i]);
                    if(GUILayout.Button("X", GUILayout.Width(25))) {
                        _whiteList.RemoveAt(i);
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndScrollView();
                if(GUILayout.Button("Add")) {
                    _whiteList.Add("");
                    _whiteScroll += new Vector2(0,20);
                }

                GUILayout.Space(5);

                m_wrapNonNameSpaceClass = GUILayout.Toggle(m_wrapNonNameSpaceClass, "Auto Wrap Non-Namespace Class");
                m_ignoreObsolete = GUILayout.Toggle(m_ignoreObsolete, "Ignore Obsolete");
                m_generateLog = GUILayout.Toggle(m_generateLog, "Generate Manifest");

                GUILayout.Space(5);

                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                if(GUILayout.Button("Reload", GUILayout.Width(60))) {
                    LoadOption();
                }
                if(GUILayout.Button("Save", GUILayout.Width(60))) {
                    SaveOption();
                }
                GUILayout.EndVertical();

                GUILayout.EndHorizontal();
            }

            GUILayout.EndHorizontal();

            WrapGUITools.EndContents();
        }else{

            GUILayout.Space(5);

            GUILayout.BeginHorizontal();
            GUILayout.Label("  STEP 1 : Click the button on right side →", "BoldLabel");
            GUI.backgroundColor = Color.green;

            if(GUILayout.Button("Wrap Common", GUILayout.Width(100))) {
                WrapCommon();
            }

            GUILayout.EndHorizontal();
            GUILayout.Label("  to wrap UnityEngine's COMMON class and ALL non-namespace class");

            GUILayout.Space(10);

            GUI.backgroundColor = Color.white;
            GUILayout.Label("  If you'd like wrap other class or custom namespace, Then ");
            GUILayout.Label("  STEP 2 : input FULL class name in the box below and click \"Wrap Custom\"", "BoldLabel");

            GUILayout.BeginHorizontal();
            GUI.backgroundColor = Color.green;
            _classInput = EditorGUILayout.TextField(_classInput, GUILayout.MinWidth(100));
            GUI.enabled = !string.IsNullOrEmpty(_classInput);
            if(GUILayout.Button("Wrap Custom", GUILayout.Width(100))) {
                WrapCustom(_classInput);
            }

            GUI.enabled = true;

            GUI.backgroundColor = Color.white;
            GUILayout.EndHorizontal();
            EditorGUILayout.HelpBox("  eg. input \"LitJson.JSONNode\" for wrap a class. \n  eg. input \"LitJson\" for wrap all classes in this namespace. ", MessageType.Info);
        }
        GUILayout.Space(10);

        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        int fontSize = GUI.skin.label.fontSize;
        GUI.skin.label.fontSize = 30;
        GUILayout.Label("Wraps");
        GUI.skin.label.fontSize = fontSize;
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();

        //搜索框
        GUILayout.BeginHorizontal();
        GUILayout.Space(84f);
        string before = _search;
        string after = EditorGUILayout.TextField("", before, "SearchTextField");
        if(before != after)
            _search = after;

        if(GUILayout.Button("", "SearchCancelButton", GUILayout.Width(18f))) {
            _search = "";
            GUIUtility.keyboardControl = 0;
        }
        GUILayout.Space(84f);

        GUILayout.EndHorizontal();
        //搜索结束

        mScroll = GUILayout.BeginScrollView(mScroll);
        GUILayout.BeginVertical();
        foreach(var kvp in m_wrapClasses){
            GUILayout.BeginHorizontal();
            if(_folderNamespace.Contains(kvp.m_nameSpace)) {
                if(GUILayout.Button("  \u25BC", "PreToolbar2", GUILayout.Width(20))) {
                    _folderNamespace.Remove(kvp.m_nameSpace);
                }
            }
            else {
                if(GUILayout.Button("  \u25BA",  "PreToolbar2", GUILayout.Width(20))) {
                    _folderNamespace.Add(kvp.m_nameSpace);
                }
            }

            Action<string> selectAll = delegate(string ns) {
                for(int i = 0; i < m_wrapClasses.Count; i++){
                    if(m_wrapClasses[i].m_nameSpace == ns){
                        for(int j = 0; j < m_wrapClasses[i].m_classes.Count; j++){
                            string fullName = string.IsNullOrEmpty(ns) ? "" : ns + ".";
                            fullName += m_wrapClasses[i].m_classes[j];
                            if(!_selectedClasses.Contains(fullName))
                                _selectedClasses.Add(fullName);
                        }
                        break;
                    }
                }
            };
            Action<string> deselectAll = delegate(string ns) {
                for(int i = 0; i < m_wrapClasses.Count; i++){
                    if(m_wrapClasses[i].m_nameSpace == ns){
                        for(int j = 0; j < m_wrapClasses[i].m_classes.Count; j++){
                            string fullName = string.IsNullOrEmpty(ns) ? "" : ns + ".";
                            fullName += m_wrapClasses[i].m_classes[j];
                            if(_selectedClasses.Contains(fullName))
                                _selectedClasses.Remove(fullName);
                        }
                        break;
                    }
                }
            };
            if(IsAllSelect(kvp.m_nameSpace)){
                bool b = GUILayout.Toggle(true,"",GUILayout.Width(12));
                if(!b){
                    deselectAll(kvp.m_nameSpace);
                }
            }else if(IsNoneSelect(kvp.m_nameSpace)){
                bool b = GUILayout.Toggle(false,"",GUILayout.Width(12));
                if(b){
                    selectAll(kvp.m_nameSpace);
                }
            }else{
                bool b = GUILayout.Toggle(true, "", "ToggleMixed",GUILayout.Width(12));
                if(!b){
                    deselectAll(kvp.m_nameSpace);
                }
            }

            GUILayout.Label("Ns", "sv_label_2", GUILayout.Width(38));
            EditorGUILayout.SelectableLabel(kvp.m_nameSpace, GUILayout.Height(16));	//EditorGUILayour可以把文字拷出来

            GUILayout.EndHorizontal();

            if(_folderNamespace.Contains(kvp.m_nameSpace)) {
                for(int i = 0; i < kvp.m_classes.Count; i++) {
                    if(string.IsNullOrEmpty(_search) || (kvp.m_nameSpace.Contains(_search) || kvp.m_classes[i].Contains(_search))) {
                        GUILayout.BeginHorizontal();
                        GUILayout.Space(34);

                        string fullName = (string.IsNullOrEmpty(kvp.m_nameSpace) ? "" : kvp.m_nameSpace + ".") + kvp.m_classes[i];
                        bool select = _selectedClasses.Contains(fullName);
                        bool old = GUILayout.Toggle(select, "", GUILayout.Width(12));
                        if(old != select) {
                            if(old)
                                _selectedClasses.Add(fullName);
                            else
                                _selectedClasses.Remove(fullName);
                        }
                        GUILayout.Label("C", "sv_label_1", GUILayout.Width(28));
                        EditorGUILayout.SelectableLabel(kvp.m_classes[i], GUILayout.Height(16));

                        GUILayout.EndHorizontal();
                    }
                }
            }
            GUILayout.Space(5);
        }
        GUILayout.EndVertical();
        GUILayout.EndScrollView();

        GUILayout.BeginHorizontal();
        if(GUILayout.Button("All", GUILayout.Width(60))){
            _selectedClasses.Clear();
            for(int i = 0; i < m_wrapClasses.Count; i++){
                for(int j = 0; j < m_wrapClasses[i].m_classes.Count; j++){
                    string fullName = string.IsNullOrEmpty(m_wrapClasses[i].m_nameSpace) ? "" : m_wrapClasses[i].m_nameSpace + ".";
                    fullName += m_wrapClasses[i].m_classes[j];
                    _selectedClasses.Add(fullName);
                }
            }
        }
        if(GUILayout.Button("None", GUILayout.Width(60))){
            _selectedClasses.Clear();
        }
        if(GUILayout.Button("Reload", GUILayout.Width(60))) {
            ReloadWrap();
        }

        GUILayout.FlexibleSpace();

        GUI.backgroundColor = Color.cyan;
        if(GUILayout.Button("Update", GUILayout.Width(60))) {
            UpdateClass(_selectedClasses.ToArray());
        }
        GUI.backgroundColor = Color.red;
        if(GUILayout.Button("Remove", GUILayout.Width(60))) {
            RemoveClass(_selectedClasses.ToArray());
            _selectedClasses.Clear();
            return;
        }
        GUI.backgroundColor = Color.white;
        GUILayout.EndHorizontal();

        GUILayout.Space(10);
    }