编写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; }}
阅读全文
0 0
- 编写Unity工具类
- Unity Editor 编写unity插件类
- 【Unity】工具类
- Unity工具类
- 如何编写工具类
- JDBC工具类编写
- unity工具
- HibernateUtil工具类的编写
- 工具类的编写方式
- 【Unity 3D】学习笔记二十八:unity工具类
- Unity简单AI编写
- unity 中编写xml
- unity 对象池编写
- Unity简单AI编写
- Unity编写第一人称控制器
- Unity中CS文件类头注释的编写实现
- RCP应用程序工具类编写小结
- RCP应用程序工具类编写小结
- html5 local storage and session storage
- 23种设计模式详解
- c++传递指针 指针的指针 指针的引用的区别
- 多线程下载,进度条,以及安装
- 梳理1Spring框架
- 编写Unity工具类
- C/C++程序CPU问题分析
- MyBatis 通过包含的jdbcType类型
- redis学习-5种数据结构
- SQL之列名属性为关键字时如何查询
- linux下的grep和find命令简单总结
- SSM-CRUD-总结
- 5.2.4 队列方法
- 【已开源】Qt 艾宾浩斯(Ebbinghaus)记忆 软件