Beispiel #1
0
    //-------------Main Function--------------
    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
    {
        if (firstOnGUICall || (reloadNextDraw && Event.current.type == EventType.Layout))
        {
            current                        = new EditorData();
            current.editor                 = materialEditor;
            current.gui                    = this;
            current.properties             = props;
            current.textureArrayProperties = new List <ShaderProperty>();
            current.firstCall              = true;
        }

        //handle events
        UpdateEvents();

        //first time call inits
        if (firstOnGUICall || (reloadNextDraw && Event.current.type == EventType.Layout))
        {
            OnOpen();
        }
        current.shader = current.materials[0].shader;

        currentlyDrawing = current;

        //sync shader and get preset handler
        Config config = Config.Get();

        if (current.materials != null)
        {
            Mediator.SetActiveShader(current.materials[0].shader, presetHandler: presetHandler);
        }


        //TOP Bar
        Rect mainHeaderRect = EditorGUILayout.BeginHorizontal();

        //draw editor settings button
        if (GUILayout.Button(new GUIContent("", Styles.settings_icon), EditorStyles.largeLabel, GUILayout.MaxHeight(20), GUILayout.MaxWidth(20)))
        {
            Thry.Settings window = Thry.Settings.getInstance();
            window.Show();
            window.Focus();
        }
        EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
        if (GUILayout.Button(Styles.search_icon, EditorStyles.largeLabel, GUILayout.MaxHeight(20)))
        {
            show_search_bar = !show_search_bar;
        }

        //draw master label if exists
        if (masterLabelText != null)
        {
            GuiHelper.DrawMasterLabel(masterLabelText, mainHeaderRect);
        }

        Rect visibilityButtonPosition = GUILayoutUtility.GetRect(new GUIContent(Styles.visibility_icon), EditorStyles.largeLabel, GUILayout.MaxHeight(20), GUILayout.MaxWidth(20));

        if (GUI.Button(visibilityButtonPosition, Styles.visibility_icon, EditorStyles.largeLabel))
        {
            HeaderHider.DrawHeaderHiderMenu(visibilityButtonPosition, current.shaderParts);
        }
        EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);
        //draw presets if exists
        presetHandler.drawPresets(current.properties, current.materials);
        EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), MouseCursor.Link);

        EditorGUILayout.EndHorizontal();

        if (show_search_bar)
        {
            header_search_term = EditorGUILayout.TextField(header_search_term);
        }

        //PROPERTIES
        if (header_search_term == "" || show_search_bar == false)
        {
            foreach (ShaderPart part in shaderparts.parts)
            {
                part.Draw();
            }
        }
        else
        {
            foreach (ShaderPart part in current.propertyDictionary.Values)
            {
                if (IsSearchedFor(part, header_search_term))
                {
                    part.Draw();
                }
            }
        }

        //Render Queue selection
        if (config.showRenderQueue)
        {
            if (config.renderQueueShaders)
            {
                customRenderQueueFieldInput = GuiHelper.drawRenderQueueSelector(current.defaultShader, customRenderQueueFieldInput);
                EditorGUILayout.LabelField("Default: " + current.defaultShader.name);
                EditorGUILayout.LabelField("Shader: " + current.shader.name);
            }
            else
            {
                materialEditor.RenderQueueField();
            }
        }

        //footer
        GuiHelper.drawFooters(footer);

        EditorGUILayout.LabelField("@UI Powered by Thryrallo", Styles.made_by_style);

        Event e      = Event.current;
        bool  isUndo = (e.type == EventType.ExecuteCommand || e.type == EventType.ValidateCommand) && e.commandName == "UndoRedoPerformed";

        if (reloadNextDraw && Event.current.type == EventType.Layout)
        {
            reloadNextDraw = false;
        }
        if (isUndo)
        {
            reloadNextDraw = true;
        }

        //on swap
        if (on_swap_to_actions != null && swapped_to_shader)
        {
            foreach (DefineableAction a in on_swap_to_actions)
            {
                a.Perform();
            }
            on_swap_to_actions = null;
            swapped_to_shader  = false;
        }

        //test if material has been reset
        if (wasUsed && e.type == EventType.Repaint)
        {
            if (current.materials[0].HasProperty("shader_is_using_thry_editor") && current.materials[0].GetFloat("shader_is_using_thry_editor") != 69)
            {
                reloadNextDraw = true;
                HandleReset();
                wasUsed = true;
            }
        }

        if (e.type == EventType.Used)
        {
            wasUsed = true;
        }
        if (config.showRenderQueue && config.renderQueueShaders)
        {
            UpdateRenderQueueInstance();
        }
        if (input.HadMouseDownRepaint)
        {
            input.HadMouseDown = false;
        }
        input.HadMouseDownRepaint = false;
        current.firstCall         = false;
    }
Beispiel #2
0
    //finds all properties and headers and stores them in correct order
    private void CollectAllProperties()
    {
        //load display names from file if it exists
        MaterialProperty[]          props  = current.properties;
        Dictionary <string, string> labels = LoadDisplayNamesFromFile();

        LoadLocales();

        current.propertyDictionary = new Dictionary <string, ShaderProperty>();
        current.shaderParts        = new List <ShaderPart>();
        shaderparts = new ShaderHeader();                            //init top object that all Shader Objects are childs of
        Stack <ShaderGroup> headerStack = new Stack <ShaderGroup>(); //header stack. used to keep track if current header to parent new objects to

        headerStack.Push(shaderparts);                               //add top object as top object to stack
        headerStack.Push(shaderparts);                               //add top object a second time, because it get's popped with first actual header item
        footer = new List <ButtonData>();                            //init footer list
        int headerCount = 0;

        Type         materialPropertyDrawerType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.MaterialPropertyHandler");
        MethodInfo   getPropertyHandlerMethod   = materialPropertyDrawerType.GetMethod("GetShaderPropertyHandler", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
        PropertyInfo drawerProperty             = materialPropertyDrawerType.GetProperty("propertyDrawer");

        Type      materialToggleDrawerType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.MaterialToggleDrawer");
        FieldInfo keyWordField             = materialToggleDrawerType.GetField("keyword", BindingFlags.Instance | BindingFlags.NonPublic);

        for (int i = 0; i < props.Length; i++)
        {
            string displayName = props[i].displayName;
            if (locale != null)
            {
                foreach (string key in locale.GetAllKeys())
                {
                    if (displayName.Contains("locale::" + key))
                    {
                        displayName = displayName.Replace("locale::" + key, locale.Get(key));
                    }
                }
            }
            displayName = Regex.Replace(displayName, @"''", "\"");

            if (labels.ContainsKey(props[i].name))
            {
                displayName = labels[props[i].name];
            }
            PropertyOptions options = ExtractExtraOptionsFromDisplayName(ref displayName);

            int offset = options.offset + headerCount;

            //Handle keywords
            object propertyHandler = getPropertyHandlerMethod.Invoke(null, new object[] { current.shader, props[i].name });
            //if has custom drawer
            if (propertyHandler != null)
            {
                object propertyDrawer = drawerProperty.GetValue(propertyHandler, null);
                //if custom drawer exists
                if (propertyDrawer != null)
                {
                    if (propertyDrawer.GetType().ToString() == "UnityEditor.MaterialToggleDrawer")
                    {
                        object keyword = keyWordField.GetValue(propertyDrawer);
                        if (keyword != null)
                        {
                            foreach (Material m in current.materials)
                            {
                                if (m.GetFloat(props[i].name) == 1)
                                {
                                    m.EnableKeyword((string)keyword);
                                }
                                else
                                {
                                    m.DisableKeyword((string)keyword);
                                }
                            }
                        }
                    }
                }
            }


            ThryPropertyType type = GetPropertyType(props[i], options);
            switch (type)
            {
            case ThryPropertyType.header:
                headerStack.Pop();
                break;

            case ThryPropertyType.header_start:
                offset = options.offset + ++headerCount;
                break;

            case ThryPropertyType.header_end:
                headerStack.Pop();
                headerCount--;
                break;

            case ThryPropertyType.on_swap_to:
                on_swap_to_actions = options.actions;
                break;
            }
            ShaderProperty newPorperty = null;
            ShaderPart     newPart     = null;
            switch (type)
            {
            case ThryPropertyType.master_label:
                masterLabelText = displayName;
                break;

            case ThryPropertyType.footer:
                footer.Add(Parser.ParseToObject <ButtonData>(displayName));
                break;

            case ThryPropertyType.header:
            case ThryPropertyType.header_start:
                ShaderHeader newHeader = new ShaderHeader(props[i], current.editor, displayName, offset, options);
                headerStack.Peek().addPart(newHeader);
                headerStack.Push(newHeader);
                HeaderHider.InitHidden(newHeader);
                newPart = newHeader;
                break;

            case ThryPropertyType.group_start:
                ShaderGroup new_group = new ShaderGroup(options);
                headerStack.Peek().addPart(new_group);
                headerStack.Push(new_group);
                newPart = new_group;
                break;

            case ThryPropertyType.group_end:
                headerStack.Pop();
                break;

            case ThryPropertyType.none:
            case ThryPropertyType.property:
                DrawingData.lastPropertyUsedCustomDrawer = false;
                current.editor.GetPropertyHeight(props[i]);
                bool forceOneLine = props[i].type == MaterialProperty.PropType.Vector && !DrawingData.lastPropertyUsedCustomDrawer;
                if (props[i].type == MaterialProperty.PropType.Texture)
                {
                    newPorperty = new TextureProperty(props[i], displayName, offset, options, props[i].flags.HasFlag(MaterialProperty.PropFlags.NoScaleOffset) == false, !DrawingData.lastPropertyUsedCustomDrawer);
                }
                else
                {
                    newPorperty = new ShaderProperty(props[i], displayName, offset, options, forceOneLine);
                }
                break;

            case ThryPropertyType.lightmap_flags:
                newPorperty = new GIProperty(props[i], displayName, offset, options, false);
                break;

            case ThryPropertyType.dsgi:
                newPorperty = new DSGIProperty(props[i], displayName, offset, options, false);
                break;

            case ThryPropertyType.instancing:
                newPorperty = new InstancingProperty(props[i], displayName, offset, options, false);
                break;

            case ThryPropertyType.locale:
                newPorperty = new LocaleProperty(props[i], displayName, offset, options, false);
                break;
            }
            if (newPorperty != null)
            {
                newPart = newPorperty;
                if (current.propertyDictionary.ContainsKey(props[i].name))
                {
                    continue;
                }
                current.propertyDictionary.Add(props[i].name, newPorperty);
                if (type != ThryPropertyType.none)
                {
                    headerStack.Peek().addPart(newPorperty);
                }
            }
            if (newPart != null)
            {
                current.shaderParts.Add(newPart);
            }
        }
    }