编写Unity工具类

来源:互联网 发布:win7蓝屏修复软件 编辑:程序博客网 时间:2024/06/01 15:06

脚本功能设定:游戏GM工具(根据物品ID、数量向服务器发送信息,获取本地所需物品道具)


using UnityEngine;using System.Collections;using UnityEditor;using System.Collections.Generic;using System.Linq;using Tianyu;public class GMUtil : EditorWindow{    string[] toolbarStr = new string[] { "标准添加道具", "搜索添加道具" };//标题栏名称    int toolbarOption = 0;//默认显示也签ID    List<ItemNodeState> allItemNodeList;    List<HeroNode> allHeroNodeList;    bool isAddedListener = false;    bool isRemovedListener = false;
    //注册消息监听    private void OnEnable()    {        MessageMediator.AddListener<CReadPacket>(MessageMediatType.gm_add_player_data_ret, ReceiveServGMMsg);    }    
    //销毁消息监听    private void OnDisable()    {        MessageMediator.RemoveListener<CReadPacket>(MessageMediatType.gm_add_player_data_ret, ReceiveServGMMsg);    }    
    //Window面板绘制    static GMUtil window;    [MenuItem("Tools/GM工具 #&Q")]    static void Main()    {        window = (GMUtil)EditorWindow.GetWindow(typeof(GMUtil), false, "GM工具");        window.Show();    }    private void OnGUI()    {        if(!Application.isPlaying || SceneManage.Instance.SceneID != EnumSceneID.UI_MajorCity01)        {            GUILayout.Label("请进入主城后再使用GM工具");            return;        }        else        {
            //初始化获取所有物品表信息            if (isAddedListener == false)            {                isAddedListener = true;            }            if (allItemNodeList == null)            {                allItemNodeList = GameLibrary.Instance().ItemStateList.Values.ToList<ItemNodeState>();            }            if (allHeroNodeList == null)            {                allHeroNodeList = FSDataNodeTable<HeroNode>.GetSingleton().DataNodeList.Values.ToList<HeroNode>();            }        }        toolbarOption = GUILayout.Toolbar(toolbarOption, toolbarStr);        switch (toolbarOption)        {            case 0:                GetItemFromId();                break;            case 1:                GetItemFromSearch();                break;        }    }    int inputItemId;    int inputItemCount;    void GetItemFromId()    {        EditorGUILayout.Space();
        //存储输入的道具ID和道具数量        inputItemId = EditorGUILayout.IntField("道具Id:", inputItemId);        inputItemCount = EditorGUILayout.IntField("道具数量:", inputItemCount);        EditorGUILayout.Space();        if (GUILayout.Button("确定"))        {
            //向服务器发送            CommonGMTool.OnSubmitRequest("ty", inputItemId, inputItemCount);        }    }    string inputItemName;    string fullItemName;    bool isSearching = false;    Vector2 scrollPosition;    List<ItemNodeState> itemNodeList;    List<HeroNode> heroNodeList;    ItemData selectItemData;    bool isSending = false;
    //提供模糊字搜索功能检索用户可能需要的道具    void GetItemFromSearch()    {        EditorGUILayout.Space();        inputItemName = EditorGUILayout.TextField("道具名称:", inputItemName);        EditorGUILayout.Space();        if (GUILayout.Button("模糊搜索"))        {            Search();        }        if(isSearching)        {
            //绘制根据用户输入的模糊字检索出来的道具列表            EditorGUILayout.Space();            scrollPosition = GUILayout.BeginScrollView(scrollPosition);            if (itemNodeList != null)            {                for (int i = 0; i < itemNodeList.Count; i++)                {                    GUILayout.BeginHorizontal();                    GUILayout.Label(itemNodeList[i].props_id.ToString());                    GUILayout.Label(itemNodeList[i].name);                    if (GUILayout.Button("选中"))                    {                        selectItemData = new ItemData();                        selectItemData.id = itemNodeList[i].props_id;                        selectItemData.name = itemNodeList[i].name;                        isSearching = false;                        isSending = true;                    }                    GUILayout.EndHorizontal();                }            }            if (heroNodeList != null)            {                for (int i = 0; i < heroNodeList.Count; i++)                {                    GUILayout.BeginHorizontal();                    GUILayout.Label(heroNodeList[i].hero_id.ToString());                    GUILayout.Label(heroNodeList[i].name);                    if (GUILayout.Button("选中"))                    {                        selectItemData = new ItemData();                        selectItemData.id = heroNodeList[i].hero_id;                        selectItemData.name = heroNodeList[i].name;                        isSearching = false;                        isSending = true;                    }                    GUILayout.EndHorizontal();                }            }            GUILayout.EndScrollView();        }        if(isSending && selectItemData != null)        {            EditorGUILayout.Space();            EditorGUILayout.Space();            EditorGUILayout.LabelField("道具Id:", selectItemData.id.ToString());            EditorGUILayout.LabelField("道具名称:", selectItemData.name.ToString());            inputItemCount = EditorGUILayout.IntField("道具数量:", inputItemCount);            EditorGUILayout.Space();            if (GUILayout.Button("确定"))            {                CommonGMTool.OnSubmitRequest(GetTypeById(selectItemData.id), selectItemData.id, inputItemCount);                notificationText = "[ID:" + selectItemData.id + " 名称:" + selectItemData.name + "] 获取中……";            }            GUIStyle fontStyle = new GUIStyle();            fontStyle.normal.background = null;    //设置背景填充              fontStyle.normal.textColor = new Color(0, 1, 0);   //设置字体颜色              fontStyle.fontSize = 11;       //字体大小              GUILayout.Label(notificationText, fontStyle);        }        else        {            notificationText = "";        }        this.Repaint();    }    void Search()    {        itemNodeList = new List<ItemNodeState>();        heroNodeList = new List<HeroNode>();        try        {            var itemNodeEnumerator = allItemNodeList.GetEnumerator();            while (itemNodeEnumerator.MoveNext())            {                if (itemNodeEnumerator.Current.name.Contains(inputItemName))                {                    itemNodeList.Add(itemNodeEnumerator.Current);                }            }            var heroNodeEnumerator = allHeroNodeList.GetEnumerator();            while (heroNodeEnumerator.MoveNext())            {                if (heroNodeEnumerator.Current.name.Contains(inputItemName))                {                    heroNodeList.Add(heroNodeEnumerator.Current);                }            }        }        catch (System.Exception)        {        }        isSending = false;        isSearching = true;    }    string notificationText = "";    void ReceiveServGMMsg(CReadPacket packet)    {        if (packet == null || packet.data == null)        {            notificationText = "[ID:" + selectItemData.id + " 名称:" + selectItemData.name + "] 获取失败";            return;        }        string str = packet.data.ContainsKey("ret") ? "成功" : "失败";        notificationText = "[ID:" + selectItemData.id + " 名称:" + selectItemData.name + "] 获取" + str;    }    /// <summary>    /// 根据类型获取物品类型    /// </summary>    private string GetTypeById(long Id)    {        ItemNodeState ins = null;        GameLibrary.Instance().ItemStateList.TryGetValue(Id, out ins);        if (ins != null)        {            return ins.types + "";        }        else        {            HeroNode node = null;            FSDataNodeTable<HeroNode>.GetSingleton().DataNodeList.TryGetValue(Id, out node);            if (node != null)            {                return node.types + "";            }        }        return string.Empty;    }    class ItemData    {        public long id;        public string name;    }}



原创粉丝点击