道具搜索框(|=, & , ^=)实现的列子

需求:

勾上界面上多选框筛选出符合的道具

思路:

1. 使用组合数字让一个数字包含多这个搜索条件,比如2代表搜索衣服和武器, 数字按照2的n次幂的值递增,通过|,&,^运算符实现一个数字包含多个条件的情况

运算符的介绍: http://www.cnblogs.com/plateFace/p/4192351.html.

 

2. 拼接一长串字符代表搜索条件比如”武器_衣服_裤子", 通过_分割字符串知道这次搜索 武器, 衣服,裤子三样东西.

代码采用的是第一个思路

效果图

image

搜索代码:

public class AuctionHouseManager
{
    //其他省略...
    
    
    //搜索代码
    public List<AuctionItem> SearchAuction(ulong condition)
    {
        List<AuctionItem> newItemList = new List<AuctionItem>();

        if (condition == 0)
        {
            foreach (var item in m_autionItemList)
            {
                newItemList.Add(item);
            }
            return newItemList;
        }

        AuctionItem auctionItem = null;
        int level = 0;
        bool isRowCondition = false;
        bool isComparison = false;

        for (int i = 0; i < m_autionItemList.Count; i++)
        {
            auctionItem = m_autionItemList[i];


            if ((condition & AuctionSearchOption.AttentionAll) > 0)
            {
                if (!m_autionItemList[i].IsAttention)
                    continue;
            }

            #region 等级
            isRowCondition = false;
            isComparison = false;
            level = auctionItem.Item.Cfg.requiredLevel;
            if ((condition & AuctionSearchOption.LvAll) > 0)
            {

                if (level != 30
                    && level != 60
                    && level != 90
                    && level != 120
                    && level != 150
                    && level != 180)
                {
                    continue;
                }
            }
            else
            {
                if ((condition & AuctionSearchOption.Lv30) > 0 && !isRowCondition)
                {
                    isRowCondition = level == 30;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.Lv60) > 0 && !isRowCondition)
                {
                    isRowCondition = level == 60;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.Lv90) > 0 && !isRowCondition)
                {
                    isRowCondition = level == 90;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.Lv120) > 0 && !isRowCondition)
                {
                    isRowCondition = level == 120;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.Lv150) > 0 && !isRowCondition)
                {
                    isRowCondition = level == 150;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.Lv180) > 0 && !isRowCondition)
                {
                    isRowCondition = level == 180;
                    isComparison = true;
                }

                if (!isRowCondition && isComparison)
                    continue;

            }

            #endregion

            #region 升星条件
            //升星条件判断
            isRowCondition = false;
            isComparison = false;
            if ((condition & AuctionSearchOption.StarLvWhite) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.StarLevel == 0;
                isComparison = true;
            }

            if ((condition & AuctionSearchOption.StarLvGreen) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.StarLevel == 1;
                isComparison = true;
            }

            if ((condition & AuctionSearchOption.StarLvBlue) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.StarLevel == 2;
                isComparison = true;
            }

            if ((condition & AuctionSearchOption.StarLvViolet) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.StarLevel == 3;
                isComparison = true;
            }

            if ((condition & AuctionSearchOption.StarLvOrange) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.StarLevel == 4;
                isComparison = true;
            }

            if ((condition & AuctionSearchOption.StarLvRed) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.StarLevel == 5;
                isComparison = true;
            }

            if (!isRowCondition && isComparison)
                continue;

            #endregion

            #region 货币
            //货币
            isRowCondition = false;
            isComparison = false;
            if ((condition & AuctionSearchOption.MoneyCopper) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.SellType == SellType.Copper;
                isComparison = true;
            }
            if ((condition & AuctionSearchOption.MoneyGold) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.SellType == SellType.Gold;
                isComparison = true;
            }

            if (!isRowCondition && isComparison)
                continue;
            #endregion

            #region 职业

            //职业
            isRowCondition = false;
            isComparison = false;
            if ((condition & AuctionSearchOption.JobWarrior) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.ClassType == RoleClass.Null || auctionItem.Item.ClassType == RoleClass.Warrior;
                isComparison = true;
            }
            if ((condition & AuctionSearchOption.JobMaster) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.ClassType == RoleClass.Null || auctionItem.Item.ClassType == RoleClass.Mage;
                isComparison = true;
            }
            if ((condition & AuctionSearchOption.JobPastor) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.ClassType == RoleClass.Null || auctionItem.Item.ClassType == RoleClass.Priest;
                isComparison = true;
            }

            if (!isRowCondition && isComparison)
                continue;

            #endregion

            #region 技能和技能书
            //装备&技能书
            /*
                道具功能类型:0
                武器:0
                衣服:1
                裤子:2
                鞋子:3
                头盔:4
                项链:5
                戒指:6
                拳套:7
                防御宝物:8
                攻击宝物:9
             */

            isRowCondition = false;                 //是否符合条件
            isComparison = false;                   //道具已经比较过

            int classType = auctionItem.Item.Cfg.classType;
            int equipType = auctionItem.Item.Cfg.v[0];

            bool isClassType13 = (classType >= 1 && classType <= 3);           //大类型是1-3

            if ((condition & AuctionSearchOption.EquipAll) > 0)
            {
                //没有全部判断条件
                isComparison = true;
                if (isClassType13 && (equipType >= 0 && equipType <= 9))
                    isRowCondition = true;
            }
            else
            {
                if ((condition & AuctionSearchOption.EquipWeapon) > 0 && !isRowCondition)
                {
                    isRowCondition = isClassType13 && equipType == 0;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.EquipClothing) > 0 && !isRowCondition)
                {
                    isRowCondition = isClassType13 && equipType == 1;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.EquipPants) > 0 && !isRowCondition)
                {
                    isRowCondition = isClassType13 && equipType == 2;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.EquipShoes) > 0 && !isRowCondition)
                {
                    isRowCondition = isClassType13 && equipType == 3;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.EquipHelmet) > 0 && !isRowCondition)
                {
                    isRowCondition = isClassType13 && equipType == 4;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.EquipNecklace) > 0 && !isRowCondition)
                {
                    isRowCondition = isClassType13 && equipType == 5;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.EquipRing) > 0 && !isRowCondition)
                {
                    isRowCondition = isClassType13 && equipType == 6;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.EquipHandguard) > 0 && !isRowCondition)
                {
                    isRowCondition = isClassType13 && equipType == 7;
                    isComparison = true;
                }
                if ((condition & AuctionSearchOption.EquipMagicWeapon) > 0 && !isRowCondition)
                {
                    isRowCondition = isClassType13 && (equipType == 8 || equipType == 9);
                    isComparison = true;
                }

                //if ((condition & AuctionSearchOption.EquipFashionable) > 0 && !isRowCondition)
                //{
                //    isRowCondition = auctionItem.Item.Cfg.v[0] == 8 || auctionItem.Item.Cfg.v[0] == 9;
                //}

                if ((condition & AuctionSearchOption.EquipSkill) > 0 && !isRowCondition)
                {
                    isRowCondition = classType == 5;
                    isComparison = true;
                }
            }

            if (!isRowCondition && isComparison)
                continue;

            #endregion


            #region 消耗品,技能和技能书是一起的
            /*
                装备道具:0
                效果道具:1
                材料道具:2
                任务道具:3
                宝石道具:4
                技能书卷:5
                技能宝物:6
                货币道具:7
                宝箱道具:8
                虚拟货币:999
             */

            if ((condition & AuctionSearchOption.ConsumeDrug) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.Cfg.type == 1 && auctionItem.Item.Cfg.v[0] == 0;
                isComparison = true;
            }
            if ((condition & AuctionSearchOption.ConsumeForgeMaterial) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.Cfg.type == 2 && (auctionItem.Item.Cfg.v[0] >= 1 && auctionItem.Item.Cfg.v[0] <= 6);
                isComparison = true;
            }
            if ((condition & AuctionSearchOption.ConsumeWayMasterial) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.Cfg.type == 2 && auctionItem.Item.Cfg.v[0] == 0;
                isComparison = true;
            }
            if ((condition & AuctionSearchOption.ConsumeGem) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.Cfg.type == 4;
                isComparison = true;
            }
            if ((condition & AuctionSearchOption.ConsumeReputation) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.Cfg.type == 7 && auctionItem.Item.Cfg.v[0] == 53;
                isComparison = true;
            }
            if ((condition & AuctionSearchOption.ConsumeSkillCheats) > 0 && !isRowCondition)
            {
                isRowCondition = auctionItem.Item.Cfg.type == 6;
                isComparison = true;
            }

            if (!isRowCondition && isComparison)
                continue;
            #endregion


            newItemList.Add(m_autionItemList[i]);
        }

        return newItemList;
    }

   
   
}


public struct AuctionSearchOption
{
    public const ulong None = 1;
    //关注
    public const ulong AttentionAll = 2;
    //等级
    public const ulong LvAll = 4;
    public const ulong Lv30 = 8;
    public const ulong Lv60 = 16;
    public const ulong Lv90 = 32;
    public const ulong Lv120 = 64;
    public const ulong Lv150 = 128;
    public const ulong Lv180 = 256;
    //品质
    public const ulong StarLvAll = 512;
    public const ulong StarLvWhite = 1024;
    public const ulong StarLvGreen = 2048;
    public const ulong StarLvBlue = 4096;
    public const ulong StarLvViolet = 8192;
    public const ulong StarLvOrange = 16384;
    public const ulong StarLvRed = 32768;
    //货币
    public const ulong MoneyAll = 65536;
    public const ulong MoneyGold = 131072;
    public const ulong MoneyCopper = 262144;
    //职业
    public const ulong JobAll = 524288;
    public const ulong JobWarrior = 1048576;
    public const ulong JobMaster = 2097152;
    public const ulong JobPastor = 4194304;
    //装备和技能书
    public const ulong EquipAll = 8388608;
    public const ulong EquipWeapon = 16777216;
    public const ulong EquipClothing = 33554432;
    public const ulong EquipPants = 67108864;
    public const ulong EquipShoes = 134217728;
    public const ulong EquipHelmet = 268435456;
    public const ulong EquipNecklace = 536870912;
    public const ulong EquipRing = 1073741824;
    public const ulong EquipHandguard = 2147483648;
    public const ulong EquipMagicWeapon = 4294967296;
    public const ulong EquipFashionable = 8589934592;
    public const ulong EquipSkill = 17179869184;
    //消耗品
    public const ulong ConsumeAll = 34359738368;
    public const ulong ConsumeDrug = 68719476736;
    public const ulong ConsumeForgeMaterial = 137438953472;
    public const ulong ConsumeWayMasterial = 274877906944;
    public const ulong ConsumeGem = 549755813888;
    public const ulong ConsumeReputation = 1099511627776;
    public const ulong ConsumeSkillCheats = 2199023255552;
}

 

 

界面代码:

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine.UI;
using AHM = AuctionHouseManager;
using ASO = AuctionSearchOption;

public class AdvancedSearch1 : UI_Base
{

    public event Action CloseUIEvent;
    private List<AuctionItem> m_searchItemList;

    [SerializeField]
    private UIButton m_BtnCancel;


    [SerializeField]
    private UIToggle m_AttentionTo;
    [SerializeField]
    private UIToggle[] m_LvToggleList;
    [SerializeField]
    private UIToggle[] m_StarToggleList;
    [SerializeField]
    private UIToggle[] m_MoneyToggleList;
    [SerializeField]
    private UIToggle[] m_JobToggleList;
    [SerializeField]
    private UIToggle[] m_EquipToggleList;
    [SerializeField]
    private UIToggle[] m_ConsumeToggleList;

    private bool isInit = false;

    public List<AuctionItem> SearchItemList
    {
        get { return m_searchItemList; }
        set { m_searchItemList = value; }
    }

    public void Awake()
    {
        //m_ToggleList = GetComponentsInChildren<Toggle>();
        m_BtnCancel.onClick.Add(new EventDelegate(Cancel));

        m_AttentionTo.onChange.Clear();
        m_AttentionTo.onChange.Add(new EventDelegate(delegate { AdvancedSearch_ChangeValueEvent(m_AttentionTo); }));

        ulong value = 0;
        m_AttentionTo.Set((AHM.I.Condition & ASO.AttentionAll) > 0, false);

        for (int i = 0; i < m_LvToggleList.Length; i++)
        {
            UIToggle t = m_LvToggleList[i];
            m_LvToggleList[i].onChange.Clear();
            m_LvToggleList[i].onChange.Add(new EventDelegate(delegate { AdvancedSearch_ChangeValueEvent(t); }));

            value = Convert.ToUInt64(m_LvToggleList[i].name);
            m_LvToggleList[i].Set((AHM.I.Condition & value) > 0 || ((AHM.I.Condition & ASO.LvAll) > 0 && value != ASO.LvAll), false);
        }

        for (int i = 0; i < m_StarToggleList.Length; i++)
        {
            UIToggle t = m_StarToggleList[i];
            m_StarToggleList[i].onChange.Clear();
            m_StarToggleList[i].onChange.Add(new EventDelegate(delegate { AdvancedSearch_ChangeValueEvent(t); }));

            value = Convert.ToUInt64(m_StarToggleList[i].name);
            m_StarToggleList[i].Set((AHM.I.Condition & value) > 0 || ((AHM.I.Condition & ASO.StarLvAll) > 0 && value != ASO.StarLvAll), false);
        }

        for (int i = 0; i < m_MoneyToggleList.Length; i++)
        {
            UIToggle t = m_MoneyToggleList[i];
            m_MoneyToggleList[i].onChange.Clear();
            m_MoneyToggleList[i].onChange.Add(new EventDelegate(delegate { AdvancedSearch_ChangeValueEvent(t); }));

            value = Convert.ToUInt64(m_MoneyToggleList[i].name);
            m_MoneyToggleList[i].Set((AHM.I.Condition & value) > 0 || ((AHM.I.Condition & ASO.MoneyAll) > 0 && value != ASO.MoneyAll), false);
        }

        for (int i = 0; i < m_JobToggleList.Length; i++)
        {
            UIToggle t = m_JobToggleList[i];
            m_JobToggleList[i].onChange.Clear();
            m_JobToggleList[i].onChange.Add(new EventDelegate(delegate { AdvancedSearch_ChangeValueEvent(t); }));

            value = Convert.ToUInt64(m_JobToggleList[i].name);
            m_JobToggleList[i].Set((AHM.I.Condition & value) > 0 || ((AHM.I.Condition & ASO.JobAll) > 0 && value != ASO.JobAll), false);
        }

        for (int i = 0; i < m_EquipToggleList.Length; i++)
        {
            UIToggle t = m_EquipToggleList[i];
            m_EquipToggleList[i].onChange.Clear();
            m_EquipToggleList[i].onChange.Add(new EventDelegate(delegate { AdvancedSearch_ChangeValueEvent(t); }));

            value = Convert.ToUInt64(m_EquipToggleList[i].name);
            m_EquipToggleList[i].Set((AHM.I.Condition & value) > 0 || ((AHM.I.Condition & ASO.EquipAll) > 0 && value != ASO.EquipAll), false);
        }

        for (int i = 0; i < m_ConsumeToggleList.Length; i++)
        {
            UIToggle t = m_ConsumeToggleList[i];
            m_ConsumeToggleList[i].onChange.Clear();
            m_ConsumeToggleList[i].onChange.Add(new EventDelegate(delegate { AdvancedSearch_ChangeValueEvent(t); }));

            value = Convert.ToUInt64(m_ConsumeToggleList[i].name);
            m_ConsumeToggleList[i].Set((AHM.I.Condition & value) > 0 || ((AHM.I.Condition & ASO.ConsumeAll) > 0 && value != ASO.ConsumeAll), false);
        }
    }


    public void LateUpdate()
    {
        if (!isInit)
        {
            //AHM.I.Condition = 0;
            isInit = true;
        }
    }

    public void AdvancedSearch_ChangeValueEvent(UIToggle obj)
    {
        if (!isInit)
            return;


        //Debug.Log(obj.name + " - " + obj.value);
        //Debug.Log(isOn + " - " + row.TitleName + " - " + cell.Name + " - " + cell.Value);
        ulong value = Convert.ToUInt64(obj.name);

        if (obj.value)
        {
            AHM.I.Condition |= value;

            //如果勾选的是全部,全部勾选剩下的小选项
            switch (value)
            {
                case ASO.LvAll:
                    for (int i = 1; i < m_LvToggleList.Length; i++)
                    {
                        m_LvToggleList[i].value = true;
                        AHM.I.Condition |= Convert.ToUInt64(m_LvToggleList[i].name);
                    }
                    break;
                case ASO.StarLvAll:
                    for (int i = 1; i < m_StarToggleList.Length; i++)
                    {
                        m_StarToggleList[i].value = true;
                        AHM.I.Condition |= Convert.ToUInt64(m_StarToggleList[i].name);
                    }
                    break;
                case ASO.MoneyAll:
                    for (int i = 1; i < m_MoneyToggleList.Length; i++)
                    {
                        m_MoneyToggleList[i].value = true;
                        AHM.I.Condition |= Convert.ToUInt64(m_MoneyToggleList[i].name);
                    }
                    break;
                case ASO.JobAll:
                    for (int i = 1; i < m_JobToggleList.Length; i++)
                    {
                        m_JobToggleList[i].value = true;
                        AHM.I.Condition |= Convert.ToUInt64(m_JobToggleList[i].name);
                    }
                    break;
                case ASO.EquipAll:
                    for (int i = 1; i < m_EquipToggleList.Length; i++)
                    {
                        m_EquipToggleList[i].value = true;
                        AHM.I.Condition |= Convert.ToUInt64(m_EquipToggleList[i].name);
                    }
                    break;
                case ASO.ConsumeAll:
                    for (int i = 1; i < m_ConsumeToggleList.Length; i++)
                    {
                        m_ConsumeToggleList[i].value = true;
                        AHM.I.Condition |= Convert.ToUInt64(m_ConsumeToggleList[i].name);
                    }
                    break;
            }
        }
        else
        {
            AHM.I.Condition ^= value;

            //Debugger.Log("等级值:" + (ASO.LvAll & value));
            if (value >= ASO.Lv30 && value <= ASO.Lv180)
            {
                m_LvToggleList[0].value = false;
                if ((AHM.I.Condition & ASO.LvAll) > 0)
                    AHM.I.Condition ^= ASO.LvAll;
            }
            else if (value >= ASO.StarLvWhite && value <= ASO.StarLvRed)
            {
                m_StarToggleList[0].value = false;
                if ((AHM.I.Condition & ASO.StarLvAll) > 0)
                    AHM.I.Condition ^= ASO.StarLvAll;
            }
            else if (value >= ASO.MoneyGold && value <= ASO.MoneyCopper)
            {
                m_MoneyToggleList[0].value = false;
                if ((AHM.I.Condition & ASO.MoneyAll) > 0)
                    AHM.I.Condition ^= ASO.MoneyAll;
            }
            else if (value >= ASO.JobWarrior && value <= ASO.JobPastor)
            {
                m_JobToggleList[0].value = false;
                if ((AHM.I.Condition & ASO.JobAll) > 0)
                    AHM.I.Condition ^= ASO.JobAll;
            }
            else if (value >= ASO.EquipWeapon && value <= ASO.EquipSkill)
            {
                m_EquipToggleList[0].value = false;
                if ((AHM.I.Condition & ASO.EquipAll) > 0)
                    AHM.I.Condition ^= ASO.EquipAll;
            }
            else if (value >= ASO.ConsumeDrug && value <= ASO.ConsumeSkillCheats)
            {
                m_ConsumeToggleList[0].value = false;
                if ((AHM.I.Condition & ASO.ConsumeAll) > 0)
                    AHM.I.Condition ^= ASO.ConsumeAll;
            }


            switch (value)
            {
                case ASO.LvAll:
                    for (int i = 1; i < m_LvToggleList.Length; i++)
                    {
                        m_LvToggleList[i].value = false;
                        AHM.I.Condition ^= Convert.ToUInt64(m_LvToggleList[i].name);
                    }
                    break;
                case ASO.StarLvAll:
                    for (int i = 1; i < m_StarToggleList.Length; i++)
                    {
                        m_StarToggleList[i].value = false;
                        AHM.I.Condition ^= Convert.ToUInt64(m_StarToggleList[i].name);
                    }
                    break;
                case ASO.MoneyAll:
                    for (int i = 1; i < m_MoneyToggleList.Length; i++)
                    {
                        m_MoneyToggleList[i].value = false;
                        AHM.I.Condition ^= Convert.ToUInt64(m_MoneyToggleList[i].name);
                    }
                    break;
                case ASO.JobAll:
                    for (int i = 1; i < m_JobToggleList.Length; i++)
                    {
                        m_JobToggleList[i].value = false;
                        AHM.I.Condition ^= Convert.ToUInt64(m_JobToggleList[i].name);
                    }
                    break;
                case ASO.EquipAll:
                    for (int i = 1; i < m_EquipToggleList.Length; i++)
                    {
                        m_EquipToggleList[i].value = false;
                        AHM.I.Condition ^= Convert.ToUInt64(m_EquipToggleList[i].name);
                    }
                    break;
                case ASO.ConsumeAll:
                    for (int i = 1; i < m_ConsumeToggleList.Length; i++)
                    {
                        m_ConsumeToggleList[i].value = false;
                        AHM.I.Condition ^= Convert.ToUInt64(m_ConsumeToggleList[i].name);
                    }
                    break;
            }
        }
    }



    public void Cancel()
    {
        UIManager.Instance.CloseUI(eUI.AuctionSearch);
    }


    public override void OnClose()
    {
        SearchItemList = AHM.I.SearchAuction(AHM.I.Condition);

        if (CloseUIEvent != null)
            CloseUIEvent();
    }
}
posted @ 2017-01-14 11:57  盘子脸  阅读(515)  评论(0编辑  收藏  举报