cache cursor texture pointer to improve performance.

This commit is contained in:
hecomi
2016-11-20 15:41:47 +09:00
parent 306a656c4e
commit fc81892917

View File

@@ -15,7 +15,18 @@ public class Cursor : MonoBehaviour
private Texture uddTexture_; private Texture uddTexture_;
private Monitor monitor { get { return uddTexture_.monitor; } } private Monitor monitor { get { return uddTexture_.monitor; } }
private Dictionary<int, Texture2D> textures_ = new Dictionary<int, Texture2D>();
struct TextureInfo
{
public Texture2D texture;
public System.IntPtr ptr;
public TextureInfo(Texture2D texture)
{
this.texture = texture;
this.ptr = texture.GetNativeTexturePtr();
}
}
private Dictionary<int, TextureInfo> textures_ = new Dictionary<int, TextureInfo>();
void Start() void Start()
{ {
@@ -50,13 +61,13 @@ public class Cursor : MonoBehaviour
if (!textures_.ContainsKey(key)) { if (!textures_.ContainsKey(key)) {
var texture = new Texture2D(w, h, TextureFormat.BGRA32, false); var texture = new Texture2D(w, h, TextureFormat.BGRA32, false);
texture.wrapMode = TextureWrapMode.Clamp; texture.wrapMode = TextureWrapMode.Clamp;
textures_.Add(key, texture); textures_.Add(key, new TextureInfo(texture));
} }
var cursorTexture = textures_[key]; var cursorTexture = textures_[key];
Assert.IsNotNull(cursorTexture); Assert.IsNotNull(cursorTexture.texture);
monitor.GetCursorTexture(cursorTexture.GetNativeTexturePtr()); monitor.GetCursorTexture(cursorTexture.ptr);
uddTexture_.material.SetTexture("_CursorTex", cursorTexture); uddTexture_.material.SetTexture("_CursorTex", cursorTexture.texture);
} }
void UpdateMaterial() void UpdateMaterial()