Beispiel #1
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>();
        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;
            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);
                break;

            case ThryPropertyType.group_start:
                ShaderGroup new_group = new ShaderGroup(options);
                headerStack.Peek().addPart(new_group);
                headerStack.Push(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 != MaterialProperty.PropFlags.NoScaleOffset, !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)
            {
                if (current.propertyDictionary.ContainsKey(props[i].name))
                {
                    continue;
                }
                current.propertyDictionary.Add(props[i].name, newPorperty);
                if (type != ThryPropertyType.none)
                {
                    headerStack.Peek().addPart(newPorperty);
                }
            }
        }
    }
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>();
        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;

        for (int i = 0; i < props.Length; i++)
        {
            string displayName = props[i].displayName;
            if (locale != null)
            {
                foreach (string key in locale.GetAllKeys())
                {
                    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;

            ThryPropertyType type = GetPropertyType(props[i]);
            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;
            }
            ShaderProperty newPorperty = 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);
                break;

            case ThryPropertyType.group_start:
                ShaderGroup new_group = new ShaderGroup(options);
                headerStack.Peek().addPart(new_group);
                headerStack.Push(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 != MaterialProperty.PropFlags.NoScaleOffset, !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)
            {
                current.propertyDictionary.Add(props[i].name, newPorperty);
                if (type != ThryPropertyType.none)
                {
                    headerStack.Peek().addPart(newPorperty);
                }
            }
        }
    }