获取用作纹理的文件名

Gle*_*rse 3 delphi delphi-xe2 firemonkey

我试图得到用作材质纹理的jpg的名称.但不知道如何把它作为一个字符串.

我有这个注意:CreateCube [1]是一个数组中的TCube组件

CreateCube[1].Material.Texture.CreateFromFile(gamedir+'\pics\'+blocktype);
Run Code Online (Sandbox Code Playgroud)

blocktype将在哪里

grass.jpg
dirt.jpg
snow.jpg
stone.jpg
Run Code Online (Sandbox Code Playgroud)

等...但是一旦它被分配后如何获得类型?目前我texture在记录中有一个字符串cube.

cube.texture := createCube[i].Material.Texture.?????
Run Code Online (Sandbox Code Playgroud)

什么属性将名称作为字符串?

J..*_*... 7

跟进评论中的讨论,这里有一个如何执行此操作的示例 - 创建一个新组件并将其添加到您的项目中.在这里我调用它TextureCube(右键单击包 - >安装.要更改,请右键单击 - >卸载,进行更改,然后右键单击 - >再次安装):

在此输入图像描述

unit TextureCube;

interface

uses
  System.SysUtils, System.Classes, FMX.Types, FMX.Types3D, FMX.Objects3D,
  Generics.Collections;

type
  TCubeTexture = (ctGrass, ctDirt, ctSnow, ctStone);

  TTextureSource = Class(TComponent)
    private
      FSelectedTexture : TCubeTexture;
      FTextures : TDictionary<TCubeTexture, TBitmap>;
      function GetTexture : TBitmap;
      procedure SetTexture(setTex : TBitmap);
    public
      constructor Create(AOwner : TComponent); override;
      destructor Destroy; override;
      property Textures : TDictionary<TCubeTexture, TBitmap> read FTextures;
    published
      property SelectedTexture : TCubeTexture read FSelectedTexture write FSelectedTexture;
      property Texture : TBitmap read GetTexture write SetTexture;

  End;

  TTextureCube = class(TCube)
  private
    FType : TCubeTexture;
    FTextureSource : TTextureSource;
    procedure SetCubeTexture(cubeTex : TCubeTexture);
    procedure SetTextureSource(texSource : TTextureSource);
  published
    property CubeType : TCubeTexture read FType write SetCubeTexture;
    property TextureSource : TTextureSource read FTextureSource write SetTextureSource;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TTextureCube]);
  RegisterComponents('Samples', [TTextureSource]);
end;

constructor TTextureSource.Create(AOwner : TComponent);
begin
  inherited;
  FTextures := TDictionary<TCubeTexture, TBitmap>.Create();
end;

destructor TTextureSource.Destroy;
begin
  FTextures.Free;
  inherited;
end;

function TTextureSource.GetTexture : TBitmap;
var tex : TBitmap;
begin
  if FTextures.TryGetValue(FSelectedTexture, tex) then
    result := tex
  else begin
    tex := TBitmap.Create(1,1);
    FTextures.AddOrSetValue(FSelectedTexture, tex);
    result := tex;
  end;
end;

procedure TTextureSource.SetTexture(setTex : TBitmap);
begin
  FTextures.AddOrSetValue(FSelectedTexture, setTex);
end;

procedure TTextureCube.SetCubeTexture(cubeTex: TCubeTexture);
var tex : TBitmap;    
begin
  FType := cubeTex;

  if Assigned(FTextureSource) and
     (FTextureSource.Textures.TryGetValue(cubeTex, tex)) then
    self.Material.Texture := tex
  else
    self.Material.Texture.Clear(0);
end;

procedure TTextureCube.SetTextureSource(texSource: TTextureSource);
begin
  FTextureSource := texSource;
  SetCubeTexture(FType);
end;   

end.
Run Code Online (Sandbox Code Playgroud)

这提供了一个TTextureCube继承自的新多维数据集类TCube- 多维数据集添加枚举类型,并且可以链接到TTextureSource为每种类型提供纹理的多维数据集类.在这里,我已将这些添加到Samples组件工具栏中的部分,您可以将它们放在任何您想要的位置.将TTextureSource和TTexture多维数据集放到表单上然后离开.这显然可以通过在表单上TTextureCube自动关联TTextureSources 来改进- 现在只需关联多维数据集和源:

在此输入图像描述

我不打算在这里制作自定义编辑器/查看器 - TTextureSource您可以选择任何类型并为该类型设置纹理.TTextureSource将保存与每个立方体类型关联的纹理库:

在此输入图像描述

然后对于每个TTextureCube你只需要更改立方体类型,它将从以下内容中获取相关的纹理TTextureSource:

在此输入图像描述

作为一个完整的谨慎 - 我在周日的无聊中很快写下了这个例子,作为如何开始的例子,也许是如何融入更优雅的设计.显然我可能错过了很多东西,可能没有正确清理等等.我不会在生产代码中使用它而没有一个好的一次性,一些添加的异常处理,错误检查和整洁.

  • 此外,谁知道这个块可能最终会采用什么其他功能 - 从如何自定义组件的教学示例的第一步开始,它显然可以增加更多.我对格伦的游戏一无所知,但我对游戏的了解已经足够多了,他们怀疑游戏中的游戏块最终会产生更多功能,属性等,而不仅仅是那些描述其纹理类型的游戏. (2认同)