C# 在我的游戏中使用图形模块的更好方法是什么
我正在重构游戏中的很多东西, 通过我的理解,我把图形模块完全弄错了。我的意思是让所有模块都知道UI,而不是让UI知道所有模块 顺便说一句:课程显然还没有结束,它有一些我检查过的旧东西,比如Texture2D PlayerHealthTexture等等 目前,我的UI是一个静态类,如下所示:C# 在我的游戏中使用图形模块的更好方法是什么,c#,.net,monogame,C#,.net,Monogame,我正在重构游戏中的很多东西, 通过我的理解,我把图形模块完全弄错了。我的意思是让所有模块都知道UI,而不是让UI知道所有模块 顺便说一句:课程显然还没有结束,它有一些我检查过的旧东西,比如Texture2D PlayerHealthTexture等等 目前,我的UI是一个静态类,如下所示: using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace ModuloFramework.UI
{
public delegate void UIDrawEventHandler(SpriteBatch spriteBatch);
public static class UI
{
private static Dictionary<Color, Texture2D> ColorTextureRepository { get; set; }
public static Texture2D GetColorTexture(Color color, GraphicsDevice graphicsDevice)
{
if (ColorTextureRepository.ContainsKey(color))
return ColorTextureRepository[color];
Texture2D tex = new Texture2D(graphicsDevice, 1, 1);
tex.SetData<Color>(new Color[] { color });
ColorTextureRepository[color] = tex;
return tex;
}
public static SpriteFont Font { get; set; }
public static Texture2D PlayerHealthTexture { get; set; }
public static event UIDrawEventHandler DrawingEvent;
public static void SubscribeToUIDraw(UIDrawEventHandler handler)
{
DrawingEvent += handler;
}
public static void UnsubscribeFromUIDraw(UIDrawEventHandler handler)
{
DrawingEvent -= handler;
}
public static void Initialize(GraphicsDevice graphicsDevice)
{
ColorTextureRepository = new Dictionary<Color, Texture2D>();
}
public static void LoadContent(ContentManager manager)
{
Font = manager.Load<SpriteFont>("UI/MainFont");
}
public static void Draw(SpriteBatch spriteBatch)
{
if (spriteBatch != null)
DrawingEvent?.Invoke(spriteBatch);
}
}
}
public ItemBehavior()
{
UI.SubscribeToUIDraw(PrintUi);
isDrawn = false;
}
protected override void BehaviorImplementation(IUnit destinationPlayer)
{
Debug.Print("Behavior test print");
isDrawn = !isDrawn;
}
private void PrintUi(SpriteBatch spriteBatch)
{
if (!isDrawn) return;
spriteBatch.DrawString(UI.Font, string.Format("Test"), new Vector2(20, 50),
Color.Black);
}
我想知道做UI模块的更好方法是什么。
例如,如果我有一个Inventory类,它应该在UI上具有可视化表示,那么我是在Inventory类中创建Draw方法,还是在UI类中创建DrawInventoryInventory方法
另外,我是否应该按照CodeReview文章中的建议,制作一个IDrawingEngine接口,并让UI成为实现该接口的单体?
如果我这样做,并且说我遵循第二种方法,让UI了解其他模块,那么除了UI类本身之外,该接口对我有什么用处
谢谢你的帮助 这是一个非常开放的问题,我的第一个建议是去看看家庭WPF和Winforms的渲染。我还建议您选择一份Unreal Engine SDK的副本,看看他们是如何构建UI的。有一些有趣的想法 基调 这不是一个新问题。人们已经花了很多时间来解决这个问题。从他们的结果中得到启发!有没有可以集成的开源库 当前的问题 屏幕上的每个项目都应能够单独运行,并具有不同的图形表示。将不同的UI元素划分为类。您可能需要一个基本UI支持的基类-呈现等。您的清单应该是它自己的类
您可以创建一个界面的原因是,如果您愿意,可以在以后更改UI。也许您有两个版本的UI,并且您希望将它们进行交换以进行性能测试!静态的东西阻止你这么做。我已经在使用一个库了,它是单游戏。它有XNA的库。我只想有一些对我的需要足够一般,对我的游戏本身足够具体的东西,但为此我需要知道如何做具体的东西。