C#
来源:互联网 发布:华中师范网络教育 编辑:程序博客网 时间:2024/06/18 12:27
将C#数据结构转成lua格式的文件。需要将C#数据结构封装成字典,然后调用HELua.Encode(),可以得到转换成后lua格式的table,将其保存为lua文件即可。
#if UNITY_EDITORusing System;using System.Collections;using System.Collections.Generic;using System.Globalization;using System.Text;using System.IO;namespace TileEditor{ public class HELua { private const int BUILDER_CAPACITY = 2000; public static string Encode (object lua) { StringBuilder builder = new StringBuilder (BUILDER_CAPACITY); bool success = SerializeValue (lua, builder); return (success ? builder.ToString () : null); } protected static bool SerializeValue(object value, StringBuilder builder) { bool success = true; if (value is string) { success = SerializeString ((string)value, builder); } else if (value is IDictionary) { success = SerializeObject ((IDictionary)value, builder); } else if (value is IList) { success = SerializeArray (value as IList, builder); } else if ((value is Boolean) && ((Boolean)value == true)) { builder.Append ("true"); } else if ((value is Boolean) && ((Boolean)value == false)) { builder.Append ("false"); } else if (value is ValueType) { success = SerializeNumber (Convert.ToDouble (value), builder); } else if (value == null) { builder.Append ("nil"); } else { success = false; } return success; } protected static bool SerializeString(string aString, StringBuilder builder) { builder.Append ("\""); char[] charArray = aString.ToCharArray (); for (int i = 0; i < charArray.Length; i++) { char c = charArray [i]; if (c == '"') { builder.Append ("\\\""); } else if (c == '\\') { builder.Append ("\\\\"); } else if (c == '\b') { builder.Append ("\\b"); } else if (c == '\f') { builder.Append ("\\f"); } else if (c == '\n') { builder.Append ("\\n"); } else if (c == '\r') { builder.Append ("\\r"); } else if (c == '\t') { builder.Append ("\\t"); } else { int codepoint = Convert.ToInt32 (c); if ((codepoint >= 32) && (codepoint <= 126)) { builder.Append (c); } else { builder.Append ("\\u" + Convert.ToString (codepoint, 16).PadLeft (4, '0')); } } } builder.Append ("\""); return true; } protected static bool SerializeObject(IDictionary anObject, StringBuilder builder) { builder.Append ("{"); IDictionaryEnumerator e = anObject.GetEnumerator (); bool first = true; while (e.MoveNext()) { string key = e.Key.ToString (); object value = e.Value; if (!first) { builder.Append (", "); } builder.Append("["); SerializeString (key, builder); builder.Append ("]"); builder.Append (" = "); if (!SerializeValue(value, builder)) { return false; } first = false; } builder.Append ("}"); return true; } protected static bool SerializeArray(IList anArray, StringBuilder builder) { builder.Append ("{"); bool first = true; for (int i = 0; i < anArray.Count; i++) { object value = anArray [i]; if (!first) { builder.Append (", "); } if (!SerializeValue(value, builder)) { return false; } first = false; } builder.Append ("}"); return true; } protected static bool SerializeNumber(Double number, StringBuilder builder) { builder.Append (Convert.ToString (number, CultureInfo.InvariantCulture)); return true; } }}#endif
阅读全文
0 0
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- c#
- c#
- C#
- C#
- C#
- c#
- C#
- HDOJ HDU 1057 A New Growth Industry
- kmp算法初识
- Python3学习(26)--类的量身定制
- 用朴素查找算法实现串的查找(男朋友算法)
- 这个支付宝电脑端转账怎么还要5毛钱手续费呀?
- C#
- Lua 在VS上的环境配置(一)
- IE浏览器使用原生JS操作ajax无效
- JS预处理
- Frame切换-ApiCloud学习笔记
- 沉浸栏效果-ApiCloud学习笔记
- 上拉加载下拉刷新方法-ApiCloud学习笔记
- 特征提取算法--ORB
- 双击返回键退出-ApiCloud学习笔记