那么,使用使用不安全代码的版本将是什么样子呢?事实上非常简单,代码如下:
private unsafe void FillTextureUnsafe(Texture t, bool random){ SurfaceDescription s = t.GetLevelDescription(0); Random r = new Random(); uint* pData = (uint*)t.LockRectangle(0, LockFlags.None).InternalData.ToPointer();
for (int i = 0; i < s.Width; i++) { for (int j = 0; j < s.Height; j++) { if (random) { *pData = (uint)Color.FromArgb( r.Next(byte.MaxValue), r.Next(byte.MaxValue), r.Next(byte.MaxValue)).ToArgb(); } else { *pData = texColor++;