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. Android Serialization序列化

    Android Serialization 目的: 为了方便測试传感器数据处理算法,Android程序的採集数据.序列化保存为文件.pc程序再通过反序列化读入对象,在PC上測试算法. Java 序列化 ...

  2. Log4net 根据日志类型输出日志

    第一步:引入Log4net.dll 文件的引用 第二步:添加LogHelper类,代码如下 using System; using System.Collections.Generic; using ...

  3. pandas时间序列滑窗

    时间序列数据统计-滑动窗口 窗口函数 import pandas as pd import numpy as np ser_obj = pd.Series(np.random.randn(1000), ...

  4. POJ 3278: Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 44613   Accepted: 13946 ...

  5. linux学习一个服务(未完)

    学习一个服务的过程 1.了解服务的作用:名字 功能,特点 2.安装 3.配置文件位置,端口 4.服务启动关闭的脚本 5.此服务的使用方法 6.修改配置文件,实战举例 7.排错(从下到上,从内到外)

  6. 一篇关于arc下内存管理的老文章,包含各种冷门修饰符(关于内存),写的较好,mark

    http://blog.csdn.net/zhibudefeng/article/details/7746201

  7. sql语句优化技巧

    1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  8. 微信小程序注册开发流程

    开篇: 微信小程序 很多刚学的同学都不太清楚如何去申请这个小程序的appid 现在呢我就一步步的告诉大家这个流程: 首先第1步,百度搜索:微信公众平台-点击右上角的立即注册 第2步:可以看到有4大注册 ...

  9. HDU - 5017 Ellipsoid(模拟退火法)

    Problem Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance bet ...

  10. Audiosink design

    Audiosink的设计,需要满足下列一些需求: 良好的chain_based 支持.绝大多数简单playback pipelines都是将音频数据从decoder直接push给audiosink; ...