Пример #1
0
 public override bool GetLogicOpResult(out String returnValue)
 {
     returnValue = "false";
     if (ShaderGUIHelper.IsModeMatched(this, m_args) &&
         ShaderGUIHelper.ExcuteLogicOp(this, null, m_args) == 1)
     {
         returnValue = "true";
         return(true);
     }
     return(false);
 }
Пример #2
0
        public void DrawGUI(Material material)
        {
            if (!ShaderGUIHelper.IsModeMatched(this, m_args) || !GetBoolTestResult())
            {
                return;
            }
            m_MaterialEditor.SetDefaultGUIWidths();
            float h = m_MaterialEditor.GetPropertyHeight(m_prop, m_prop.displayName);
            Rect  r = EditorGUILayout.GetControlRect(true, h, EditorStyles.layerMaskField);

            m_MaterialEditor.ShaderProperty(r, m_prop, m_prop.displayName);
            m_MaterialEditor.SetDefaultGUIWidths();
        }
Пример #3
0
 public override bool GetLogicOpResult(out String returnValue)
 {
     returnValue = String.Empty;
     return(m_prop != null && ShaderGUIHelper.IsModeMatched(this, m_args) &&
            ShaderGUIHelper.ExcuteLogicOp(this, m_prop, m_args) == 1);
 }
Пример #4
0
        protected override bool OnInitProperties(MaterialProperty[] props)
        {
            if (m_tags != null)
            {
                return(true);
            }
            m_show = true;
            m_tags = new List <TagDefine>();
            // 从编辑器数据中获取用户自定义Tag
            var tags = m_args.GetField(Cfg.ArgsKey_CustomTag_Tags);

            if (tags != null && tags.IsArray && tags.list.Count > 0)
            {
                for (int i = 0; i < tags.list.Count; ++i)
                {
                    var tagDef = tags.list[i];
                    if (tagDef.IsObject)
                    {
                        String type;
                        if (tagDef.GetField(out type, Cfg.ArgsKey_CustomTag_Type, String.Empty))
                        {
                            type = type.Trim();
                            var values = tagDef.GetField(Cfg.ArgsKey_CustomTag_Values);
                            if (values != null && values.IsArray && values.Count > 0)
                            {
                                if (m_tags.Find(item => item.typename == type) != null)
                                {
                                    Debug.LogErrorFormat("Tag {0} already exists.", type);
                                    continue;
                                }
                                var tagdef = new TagDefine();
                                tagdef.typename = type;
                                tagDef.GetField(out tagdef.readOnly, Cfg.ArgsKey_CustomTag_Readonly, false);
                                var _values = new List <String>(values.Count);
                                for (int n = 0; n < values.Count; ++n)
                                {
                                    var v     = values[n];
                                    var value = String.Empty;
                                    if (v.IsString && !String.IsNullOrEmpty(v.str) &&
                                        !_values.Contains(value = v.str.Trim()))
                                    {
                                        _values.Add(value);
                                    }
                                }
                                // 一个定义需要升级到另外一个Tag
                                var op     = UpgradeOPType.None;
                                var target = String.Empty;
                                if (tagDef.GetField(out target, Cfg.ArgsKey_CustomTag_UpgradeTo, String.Empty))
                                {
                                    op = UpgradeOPType.Cut;
                                }
                                else if (tagDef.GetField(out target, Cfg.ArgsKey_CustomTag_CopyTo, String.Empty))
                                {
                                    op = UpgradeOPType.Copy;
                                }
                                target = target.Trim();
                                if (!String.IsNullOrEmpty(target))
                                {
                                    tagdef.upgradeOPType   = op;
                                    tagdef.upgradeTypename = target;
                                    // 找到升级目标Tag定义
                                    var targetTypeDef = m_tags.Find(item => item.typename == tagdef.upgradeTypename);
                                    if (targetTypeDef != null)
                                    {
                                        // 保存别名
                                        targetTypeDef.alias = targetTypeDef.alias ?? new HashSet <String>();
                                        targetTypeDef.alias.Add(tagdef.upgradeTypename);
                                        if (targetTypeDef.values != null)
                                        {
                                            // 把选项合并
                                            _values.AddRange(targetTypeDef.values);
                                        }
                                    }
                                    else
                                    {
                                        Debug.LogErrorFormat("upgrade type from '{0}' to '{1}' not found.", type, tagdef.upgradeTypename);
                                    }
                                    // 测试需要升级的当前tag是否可以被删除
                                    var material = m_MaterialEditor.target as Material;
                                    if (material != null)
                                    {
                                        var removeAble = ShaderGUIHelper.IsTagRemoveable(material, type);
                                        if (removeAble != null)
                                        {
                                            tagdef.removeAble = removeAble.Value;
                                        }
                                    }
                                }
                                if (_values.Count > 0)
                                {
                                    var keepValue = tagDef.GetField(Cfg.Key_FixedValue);
                                    if (keepValue != null && keepValue.IsString)
                                    {
                                        tagdef.keep = keepValue.str.Trim();
                                        if (!String.IsNullOrEmpty(tagdef.keep))
                                        {
                                            if (!_values.Contains(tagdef.keep))
                                            {
                                                Debug.LogErrorFormat("Tag value '{0}' is not defined.", tagdef.keep);
                                            }
                                            else
                                            {
                                                tagdef.readOnly = true;
                                            }
                                        }
                                    }
                                    // 去重后转换为数组
                                    tagdef.values = _values.Distinct().ToArray();
                                    m_tags        = m_tags ?? new List <TagDefine>();
                                    m_tags.Add(tagdef);
                                }
                            }
                        }
                    }
                }
            }
            // 从当前材质中获取已有的系统Tag
            var materials = m_MaterialEditor.targets.Where(e => e is Material).Cast <Material>().ToArray();

            for (int m = 0; m < materials.Length; ++m)
            {
                var mat = materials[m];
                if (mat == null)
                {
                    continue;
                }
                for (int i = 0; i < BuiltinTags.Length; ++i)
                {
                    var typename = BuiltinTags[i];
                    var value    = mat.GetTag(typename, true);
                    if (!String.IsNullOrEmpty(value))
                    {
                        m_tags = m_tags ?? new List <TagDefine>();
                        var       defIndex = m_tags.FindIndex(item => item.typename == typename);
                        TagDefine def;
                        if (defIndex >= 0)
                        {
                            def = m_tags[defIndex];
                            // 定义了系统Tag,把系统tag当前值追加进选项
                            if (def.values != null)
                            {
                                var _values = new List <String>(def.values);
                                if (!_values.Contains(value))
                                {
                                    _values.Add(value);
                                    def.values = _values.ToArray();
                                }
                            }
                            else
                            {
                                // 编辑器中没有定义选项,此选项只读,唯一选项
                                def.values   = new String[] { value };
                                def.readOnly = true;
                            }
                            if (defIndex > 0)
                            {
                                // 交换到前面去
                                var t = m_tags[0];
                                m_tags[0]        = def;
                                m_tags[defIndex] = t;
                            }
                        }
                        else
                        {
                            // 编辑器没有定义,只读唯一选项
                            def          = new TagDefine();
                            def.typename = typename;
                            def.readOnly = true;
                            def.values   = new String[] { value };
                            m_tags.Insert(0, def);
                        }
                        def.builtin = true;
                    }
                }
            }
            // 设置固定不变值
            for (int i = 0; i < m_tags.Count; ++i)
            {
                var tagdef = m_tags[i];
                if (!String.IsNullOrEmpty(tagdef.keep))
                {
                    if (tagdef.values != null)
                    {
                        if (Array.IndexOf(tagdef.values, tagdef.keep) >= 0)
                        {
                            for (int j = 0; j < materials.Length; ++j)
                            {
                                _SetOverrideTag(materials[j], tagdef.typename, tagdef.keep, true);
                            }
                        }
                    }
                }
            }
            DoUpgrade();
            return(true);
        }
Пример #5
0
        void DoUpgrade()
        {
            var targets = m_MaterialEditor.targets.Where(e => e is Material).Cast <Material>().ToArray();

            for (int i = 0; m_tags != null && i < m_tags.Count; ++i)
            {
                var tagdef = m_tags[i];
                if (!String.IsNullOrEmpty(tagdef.upgradeTypename))
                {
                    var typename = tagdef.typename;
                    tagdef.upgradeState = tagdef.upgradeState ?? new Dictionary <Material, UpgradeOPType>();
                    var opState = tagdef.upgradeState;
                    for (int mi = 0; mi < targets.Length; ++mi)
                    {
                        var           mat = targets[mi];
                        UpgradeOPType curOp;
                        if (opState.TryGetValue(mat, out curOp) && curOp == UpgradeOPType.None)
                        {
                            // 已经处理过了
                            continue;
                        }
                        var value = mat.GetTag(typename, true);
                        if (tagdef.upgradeOPType != UpgradeOPType.None)
                        {
                            if (tagdef.upgradeOPType == UpgradeOPType.Cut && tagdef.removeAble == false && IsOptionNone(value))
                            {
                                // 剪切模式下,要升级的类型值已经用空值覆盖,跳过处理
                                // 这会导致一个问题就是:如果一个没有升级的Tag,但是其值原本就是空,
                                // 这样会导致这个状态不会被拷贝到需要升级的目标中去
                                opState[mat] = UpgradeOPType.None;
                                continue;
                            }
                            var upgradeTypename = tagdef.upgradeTypename;
                            var upgradeType     = m_tags.Find(item => item.typename == upgradeTypename);
                            if (!String.IsNullOrEmpty(value) && upgradeType != null)
                            {
                                UpgradeTag(mat, tagdef, upgradeType);
                            }
                            continue;
                        }
                        // value 可能为空字符串,也可能是表示空的有效字符串如'None'
                        var index    = Array.IndexOf(tagdef.values, value);
                        var newIndex = index;
                        var fixTag   = false;
                        if (index == -1)
                        {
                            if (tagdef.builtin)
                            {
                                // 系统tag,如果当前tag值没在选项中,追加
                                if (!String.IsNullOrEmpty(value))
                                {
                                    Array.Resize(ref tagdef.values, tagdef.values.Length + 1);
                                    tagdef.values[tagdef.values.Length - 1] = value;
                                    newIndex = tagdef.values.Length - 1;
                                }
                            }
                            else
                            {
                                newIndex = 0;
                                if (!IsNullTagValue(value))
                                {
                                    // 非空值,没有在选项中找到,当作一次修复操作,需要提示用户
                                    fixTag = true;
                                }
                            }
                        }
                        if (newIndex >= 0 && newIndex != index)
                        {
                            if (!tagdef.readOnly)
                            {
                                _SetOverrideTag(mat, typename, tagdef.values[newIndex], fixTag);
                            }
                        }
                    }
                }
            }
            // tag清理
            var tagMap = new Dictionary <String, String>();

            for (int i = 0; i < targets.Length; ++i)
            {
                var target = targets[i];
                if (ShaderGUIHelper.GetMaterialOverrideTags(target, ref tagMap))
                {
                    foreach (var tag in tagMap)
                    {
                        var def = m_tags.Find(e => e.typename == tag.Key);
                        if (def == null && Array.IndexOf(BuiltinTags, tag.Key) < 0)
                        {
                            // 材质中有编辑器未定义的tag
                            var removeAble = ShaderGUIHelper.IsTagRemoveable(target, tag.Key);
                            if (removeAble != null && removeAble.Value)
                            {
                                target.SetOverrideTag(tag.Key, String.Empty);
                                Debug.LogFormat("Remove Override Tag: {0}", tag.Key);
                                EditorUtility.SetDirty(target);
                            }
                        }
                    }
                }
            }
        }