1使用unsafe,直接修改字符串

public static class UnsafeString
{
public static unsafe void Copy(this string str, LuaString luastr)
{
if (luastr.ptr == IntPtr.Zero)
Clear( str );
fixed (char* pstr = str)
{
var dest = (char*)luastr.ptr.ToPointer();
var len = luastr.len;
for (int i = ; i < len; i++)
*(pstr + i) = *(dest + i);
*(pstr + len) = '\0';
int* strlen = (int*)pstr - ;
*strlen = len;
}
} public static unsafe void Clear(this string str)
{
fixed (char* ptr = str)
{
int* iptr = (int*)ptr - ;
*iptr = ;
}
}
}

修改text部分

 if (txt.text.Length *  >= _str.len)//c#是ucs2编码,一个字符是2个字节
{
txt.text.Copy(_str);
txt.cachedTextGenerator.Invalidate();
txt.SetVerticesDirty();
}
else
txt.text = _str.ToString();

但是有个坑,lua是utf8编码,而c#里是ucs2b编码,所以需要做个转换,明天补上

需要2个方法,一个是获取utf8编码个数

  public unsafe int ulen()
{
if (len <= ) return ;
if (utf8len > ) return utf8len;
var str = (byte*)(strdata().ToPointer());
int n = ;
for (int i = ; i < len; i++)
{
var c = str[i];
//short c = (short) s;
i += c >= 0xfc ? : (c >= 0xf8 ? : (c >= 0xf0 ? : (c >= 0xe0 ? : (c >= 0xc0 ? : ))));
n++;
}
utf8len = n;
return utf8len;
}

一个是从utf8转到unicode(utf16)

public static unsafe void CopyAnsi(this string str, LuaString luastr)
{
if (luastr.ptr == IntPtr.Zero)
Clear(str);
fixed (char* pstr = str)
{
var dest = (byte*)luastr.strdata().ToPointer();
var len = luastr.len;
var pstrn = ;
int oldlen = *((int*)pstr - );
for (int i = ; i < len; i++)
{
var c = *(dest + i);
if (c >= 0xfc)
{ }
else if (c >= 0xf8)
{ }
else if (c >= 0xf0)
{ }
else if (c >= 0xe0)
{
var c1 = * (dest + i + );
var c2 = * (dest + i + );
*(pstr + pstrn) = (char)(((c & 0x1f) << ) + ((c1 & 0x3f) << )+ (c2 & 0x3f));
i += ;
}
else if (c >= 0xc0)
{
var c1 = *(dest + i + );
*(pstr + pstrn) = (char)(((c & 0x3c) << ) + (c1 & 0x3f));
i++;
}
else
{
* (pstr + pstrn) = (char)c;
}
pstrn++;
}
//*(pstr + pstrn) = (byte)0;
int* strlen = (int*)pstr - ;
*strlen = pstrn;
}
}

修改text部分

 set {
if(!(c is Text) && !(c is InputField)) Log.Assert((c is Text), "the component name {0} is not a Text or InputField", c.name);
if (c is Text)
{
if (_str.ptr == value.str.ptr) return;
unrefString(_str);
_str = value.str;
var txt = (c as Text);
if (txt.text.Length >= _str.ulen())
{
//txt.text.Copy(_str);
txt.text.CopyAnsi(_str);
txt.cachedTextGenerator.Invalidate();
txt.SetVerticesDirty();
}
else
txt.text = _str.ToString();
refString(_str); }
if (c is InputField) (c as InputField).text = (string)value;
}

unity里c# gc优化 -字符串的更多相关文章

  1. Unity中的GC以及优化

    [简介] 常见的 Unity GC 知识点总结出来的思维导图 Unity 官方文档,正巧在博客园发现了已经有位大神(zblade)把原文翻译出来了,而且质量很高~,译文地址 在这里.下面我就可耻地把译 ...

  2. 浅谈Unity中的GC以及优化

    介绍: 在游戏运行的时候,数据主要存储在内存中,当游戏的数据不在需要的时候,存储当前数据的内存就可以被回收再次使用.内存垃圾是指当前废弃数据所占用的内存,垃圾回收(GC)是指将废弃的内存重新回收再次使 ...

  3. Unity下XLua方案的各值类型GC优化深度剖析

    转自:http://gad.qq.com/article/detail/25645 前言 Unity下的C#GC Alloc(下面简称gc)是个大问题,而嵌入一个动态类型的Lua后,它们之间的交互很容 ...

  4. 关于Unity中的UGUI优化,你可能遇到这些问题

    https://blog.uwa4d.com/archives/QA_UGUI-1.html 关于Unity中的UGUI优化,你可能遇到这些问题 作者:admin / 时间:2016年11月08日 / ...

  5. Unity基础-脚本的优化

    脚本的优化 object pool 避免频繁的内存分配和gc噩梦(字符串相加?) 是否有必要都写在update里?分帧? 需要的只取一次 使用editor内赋值,而不是find 复杂的物理 复杂的数学 ...

  6. Entity Framework 实体框架的形成之旅--利用Unity对象依赖注入优化实体框架(2)

    在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...

  7. Unity3D游戏GC优化总结---protobuf-net无GC版本优化实践

    protobuf-net优化效果图 protobuf-net是Unity3D游戏开发中被广泛使用的Google Protocol Buffer库的c#版本,之所以c#版本被广泛使用,是因为c++版本的 ...

  8. 如何降低90%Java垃圾回收时间?以阿里HBase的GC优化实践为例

    过去的一年里,我们准备在Ali-HBase上突破这个被普遍认知的痛点,为此进行了深度分析及全面创新的工作,获得了一些比较好的效果.以蚂蚁风控场景为例,HBase的线上young GC时间从120ms减 ...

  9. JVM:从实际案例聊聊Java应用的GC优化

    原文转载自美团从实际案例聊聊Java应用的GC优化,感谢原作者的贡献 当Java程序性能达不到既定目标,且其他优化手段都已经穷尽时,通常需要调整垃圾回收器来进一步提高性能,称为GC优化.但GC算法复杂 ...

随机推荐

  1. 集成ueditor工具

    摘要: 摘要: 版权声明:本文为博主原创文章,未经博主允许不得转载. UEditor 是百度的一套开源的在线HTML编辑器. 第一步:去官网看官网文档,了解这个工具如何使用以及下载,本人下载的是1.4 ...

  2. shell脚本与mongo交互

    1.mongo提供*.js的方法供linux调用 #!/bin/bash /usr/local/mongodb/bin/mongo hostname/dbname xxx.js xxx.js 内容如下 ...

  3. 动态获取html页面的内容,而且取当中的某块元素的方法

     $.ajax({  url: "http://192.168.1.59:8888/app-tpl-webapp/tpl/design.html",  async:false, ...

  4. yum安装epel后报错

    Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfi ...

  5. TCP通过哪些措施,保证传输可靠

    TCP是通过什么方式来提供可靠传输的 (合理截断数据包,超时重发,校验,失序重新排序,能够丢弃重复数据,TCP可以进行流量控制) TCP提供一种面向连接的.可靠的字节流服务.  面向连接:意味着两个使 ...

  6. jq时间戳转化为可视化时间

    //2016年5月21日 23:12:07 function getDateTimeToDate(dt){ var dateTime = new Date(dt); var date = dateTi ...

  7. abp 列表和明细页面的规则说明

    0>列表区域mainviewbody 1>列表头mainviewheader 名称 2>列表条件mainviewcodtionbody 条件区 3>列表工具栏mainviewt ...

  8. Alluxio部署(local模式)

    下载(pre-build for Hadoop 2.7) http://www.alluxio.org/download` 解压 tar -xvf alluxio-1.3.0-hadoop2.7-bi ...

  9. Vsphere日记02.ESXi5.5.configuration

    2.Vsphere ESXi 5.5 configuration 步骤1:启动 ESXI 服务器 步骤2:按 F2 进入用户登录界面 输入用户名及密码,进入参数设置界面.密码为我安装时候设置的&quo ...

  10. er图 画图工具

    http://www.oschina.net/project/tag/83/db-model