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. Hybird App(一)----第一次接触

    App你知道多少 一 什么是Native App 长处 缺点 二 什么是Web App 长处 缺点 三 什么是Hybrid App 长处 缺点 四 Web AppHybrid AppNative Ap ...

  2. Android Shape 详解

    1 http://blog.csdn.net/feng88724/article/details/6398193 2 <shape xmlns:android="http://sche ...

  3. 深入PHP中慎用双等于(==)的详解

    PHP比较运算符出现的频率实在是太高了,尤其是 ==if(a == b){// do something}但是,你真的掌握了 == 了吗?细节很重要!来看下面的代码,说出你认为正确的答案var_dum ...

  4. Codeforces Round #244 (Div. 2)——Checkposts

    题目链接 题意: 给定n个点,每一个点有一个权值的有向图.如今须要选定一些点,使得这些点权值和最小.且满足:假设i能到达j且j能到达i,那么i.j能够仅仅选一个 分析: 强联通模板题 //使用时仅仅更 ...

  5. Atitit. visual studio vs2003 vs2005 vs2008  VS2010 vs2012 vs2015新特性 新功能.doc

    Atitit. visual studio vs2003 vs2005 vs2008  VS2010 vs2012 vs2015新特性 新功能.doc 1.1. Visual Studio2 1.2. ...

  6. HTTP解读

    使用Telnet工具访问web资源 Windows中没有telnet这一工具,下面在Linux下演示: telnet www.baidu.com 80 Trying 61.135.169.125... ...

  7. java的集合层次图

  8. ConcurrentHashMap的JDK1.8实现

    今天我们介绍一下ConcurrentHashMap在JDK1.8中的实现.基本结构 ConcurrentHashMap在1.8中的实现,相比于1.7的版本基本上全部都变掉了.首先,取消了Segment ...

  9. Boolean 转 string

    boolean b=false; String b= String.valueOf(b);

  10. spring cloud与K8S

    链接:https://www.zhihu.com/question/50806354/answer/139653085 spring cloud +docker 当然没有问题,只是当我们搭建集群实现高 ...