简体   繁体   English

MonoGame-处理Texture2D

[英]MonoGame - Dispose Texture2D

A few days ago I played with the MonoGame Framework. 几天前,我玩了MonoGame框架。 I construct follow class: 我构造跟随类:

public Texture2D Texture2D { get; set; }
public Vector2 Vector2;

public Point(Texture2D texture2D, Vector2 vector2)
{
    Texture2D = texture2D;
    Vector2 = vector2;
}

public void SetX()
{
    if(Vector2.X > -50)
    {
        Vector2.X = Vector2.X-2;
    }
}

Then I created more than 2 objects and added them to a list: 然后,我创建了两个以上的对象并将它们添加到列表中:

_points.Add(new Models.Point(Content.Load<Texture2D>("Point"), new Vector2(50, 50));

When I dispos one of them, all textures will be disposed. 当我处理其中之一时,将处理所有纹理。 How can I dispose only one of the two objects? 如何仅处置两个对象之一?

As a general rule the class responsible for disposing something should be the same class that created it. 通常,负责处理某事的类应与创建它的类相同。 There are exceptions to this rule but generally if it holds true it'll make problems like this a lot easier to reason about. 该规则有一些例外,但是通常,如果它成立,它将使诸如此类的问题更容易推论。

In this case specifically, it seems you've misunderstood how textures are normally disposed in MonoGame. 特别是在这种情况下,您似乎误解了MonoGame中通常如何处理纹理。 There's basically 2 options: 基本上有2种选择:

  1. Textures created with the content manager using Content.Load<Texture2D> are disposed by the content manager (in other words, you shouldn't call Dispose on these textures directly) 使用Content.Load<Texture2D>使用内容管理器创建的纹理由内容管理器处理(换句话说,您不应直接在这些纹理上调用Dispose
  2. Textures created by yourself using new Texture2D are your responisibilty to dispose (in this case you should call Dispose directly) 您自己负责使用new Texture2D创建的纹理进行处理(在这种情况下, 直接调用Dispose )。

Another thing to note is that calling Content.Load<Texture2D>("Point") more than once will actually return a reference to the same texture. 要注意的另一件事是,多次调用Content.Load<Texture2D>("Point")实际上会返回对相同纹理的引用。 This is generally considered a good thing because it will only load the texture into memory once. 通常认为这是一件好事,因为它只会将纹理加载到内存一次。

If you actually do want the same texture loaded into memory more than once you'll need to handle this yourself. 如果您确实希望多次将相同的纹理加载到内存中,则需要自己进行处理。 Although, it's rare that you'll want to do this so make sure you understand the implications first. 虽然,很少会这样做,所以请确保先了解其含义。

To demonstrate the point consider the following code: 为了说明这一点,请考虑以下代码:

var texture1 = Content.Load<Texture2D>("Point"); // load texture into memory
var texture2 = Content.Load<Texture2D>("Point"); // get a reference to the same texture

At this point texture1 and texture2 will be the same instance. 此时, texture1texture2将是同一实例。 If you change one of them, you're changing both. 如果更改其中之一,则同时更改。

Later on when your game shuts down somewhere deep in the bowels of the Game class the Content manager will Dispose . 稍后,当您的游戏在Game类的深处关闭时, Content管理器将Dispose This is when the texture will be released. 这是纹理将被释放的时间。 You don't need to do this yourself. 您无需自己执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM