- 枚举
//Bitflags元标记.h
UENUM(Meta=(Bitflags))
enum class EColorBits
{
BCB_Red,
BCB_Green,
BCB_Blue
};
//调用时,需要使用“BitmaskEnum”元标记来引用.cpp
UPROPERTY(EditAnywhere,Meta=(Bitmask,BitmaskEnum="EColorbits"))
int32 ColorFlags;
//第一种 UENUM()
namespace EWeapon
{
enum Type
{
EW_Handgun,
EW_shotgun
};
}
//第二种
UENUM(BlueprintType)
enum class EWeapon:uint8 //如果想要显示中文,需要设置编码格式为UTF-8
{
EW Handgun UMETA(DisplayName="Handgun"),
EW shotgun UMETA(DisplayName="shotgun"),
};
//如果枚举索引不想从0开始的话,使用下面的格式:
.h
UENUM(BlueprintType)//蓝图中可见
enum class EWeapon:uint8
{
EWHandgun=1 UMETA(DisplayName="Handun")
EW_shotgun UMETA(DisplayName="shotgun),
}
UCLASS()
class MYPROJECT APIAMyPlayerController:publicAPlaverController
{
GENERATED BODY()
public:
UPROPERTY(EditAnywhereBlueprintReadWriteCategory=weapon” )
EWeapon weaponType;
};
.cpp
//构造函数初始化
weaponType = EWeapon::EW_shotgun;
//获取它的Name,枚举类中的成员名
UEnum* enumPtr=FindObject(ANY_PACKAGE,TEXT(“EWeapon”),true);
FName curWeaponStr=enumPtr->GetNameByValue((int32)weaponType);
UE_LOG(LogTemp, Warning,TEXT(“curWeaponStr is%s),*curWeaponStrToString());
// 通过名字获取索引
int32 weaponIndex=enumPtr->GetIndexByName(FName(curWeaponStr)):
UE LOG(LogTemp,WarningTEXT(“curWeapon indexis%d),weaponIndex);
// 通过索引获取值
int weaponValue=enumPtr->GetValueByIndex(weaponIndex);
UE LOG(LogTemp,WarningTEXT(“curWeapon value is%d”),weaponValue);
// 通过索引获取DisplayName
FText curWeaponText=enumPtr->GetDisplayNameTextByIndex(weaponIndex);
UE LOG(LogTemp,WarningTEXT(“curWeaponStr is%s”),*curWeaponTextToString());
- FText
FText通常用于本地化,本地化拾荒将所有的FText收集起来,所以文本不要以FString存放,使用FString存放转换为FText的文本是无法在本地计划时收集的
格式转换
FText::FromName() FText::FromString()
- 数字转FString
//float-> FString
FString::SanitizeFloat(FloatVariable);
//int->FString
FString::FromInt(IntVariable)
//bool -> FString
InBool ?TEXT("true"): TEXT("false")
//FVector -> FString
VectorVariable.ToString();
//UObject -> FString
(InObj!=NULL)? InObj->GetName():FString(TEXT("None))
- FString转其他变量
//FString -> bool TestHUDStringToBool(); //FString -> int FCString::Atoi(*TestHUDString); //FString -> float FCString::Atof(*TestHUDString);



