UE杂项C++

1.double转字符串可自定义保留位数

参考链接

.h

/// <summary>
	/// double转字符串可自定义保留位数。(	sprintf_s(buffer, "%.9lf", a);9 = 保留位数)
	/// https://deepinout.com/cpp/cpp-examples/g_cpp-program-for-double-to-string-conversion.html
	/// </summary>
	UFUNCTION(BlueprintCallable, Category = "Vince|Math")
		static FString DoubleToString();
.cpp

FString UMyBlueprintFunctionLibrary::DoubleToString()
{
	double a = 3.59564597891546;
	char buffer[100];
	sprintf_s(buffer, "%.9lf", a);
	FString ac = buffer;
	return ac;
}

2.系统/UTC时间戳转换+获取星期几

.h

UENUM(BlueprintType)
enum class EDAYOFWEEK :uint8
{


	//星期一
	Monday = 0,

	//星期二
	Tuesday,

	//星期三
	Wednesday,

	//星期四
	Thursday,

	//星期五
	Friday,

	//星期六
	Saturday,

	//星期天
	Sunday

};


	/// <summary>
	/// 获取星期几
	/// </summary>
	UFUNCTION(BlueprintCallable, Category = "Vince|Math")
		static void GetDayOfWeek(int32 year, int32 month, int32 day, int32& weekInt, EDAYOFWEEK& week);

	// 日期长度判断
	static FString DateLength(int32 Date);

	// 获取当前时间戳
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static int64 GetTimestamp();

// 获取选择时间戳
	UFUNCTION(BlueprintPure, Category = "Vince|Mathe")
		static int64 GetCheckTimestamp(FDateTime checkTime);


	// 时间戳转日期
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FString TimestampToDatetime(int64 UnixTime, bool bIsLocal = true);

	// 时间戳转小时
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FString TimestampToHour(int64 UnixTime, bool bIsLocal = true);

	// 时间戳转小时和分钟
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FString TimestampToHourAndMinute(int64 UnixTime, bool bIsLocal = true);

	// 时间戳转系统时间另一个写法。返回FDataTime
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FDateTime   ConvertTimestampToDateTime(int64 Timestamp);

	//  时间戳转系统时间另一个写法。把FDataTime转换成Text返回
	UFUNCTION(BlueprintPure, Category = "Vince|Math")
		static FText ConvertTimestampToSystemTime(int64 Timestamp);
.cpp

void UMyBlueprintFunctionLibrary::GetDayOfWeek(int32 year, int32 month, int32 day, int32& weekInt, EDAYOFWEEK& week)
{

	FDateTime newDataTime = FDateTime(year, month, day);

	weekInt = int(newDataTime.GetDayOfWeek());

	week = EDAYOFWEEK(weekInt);

}

FString UMyBlueprintFunctionLibrary::DateLength(int32 Date)
{
	FString DateString;
	DateString = FString::FromInt(Date);
	if (Date < 10)
	{
		DateString = "0" + DateString;
	}
	return DateString;
}

int64 UMyBlueprintFunctionLibrary::GetTimestamp()
{
	return FDateTime::UtcNow().ToUnixTimestamp();
}

int64 UMSBlueprintFunctionLibrary::GetCheckTimestamp(FDateTime checkTime)
{
	int64 newTimestamp = checkTime.ToUnixTimestamp();

	int64 newDifference = FDateTime::Now().ToUnixTimestamp() - FDateTime::UtcNow().ToUnixTimestamp();

	newTimestamp -= newDifference;
	return newTimestamp;

}


FString UMyBlueprintFunctionLibrary::TimestampToDatetime(int64 UnixTime, bool bIsLocal /*= true*/)
{

	FString Date;

	if (bIsLocal)
	{
		struct tm* timeinfo = new struct tm;
		localtime_s(timeinfo, &UnixTime);
		int year = timeinfo->tm_year + 1900; // years since 1900
		int month = timeinfo->tm_mon + 1; // monthes since January - [0, 11]
		int day = timeinfo->tm_mday;
		int date = year * 10000 + month * 100 + day;
		int hour = timeinfo->tm_hour;
		int minute = timeinfo->tm_min;
		int second = timeinfo->tm_sec;

		Date = DateLength(year) + "-" + DateLength(month) + "-" + DateLength(day) + " " + DateLength(hour) + ":" + DateLength(minute) + ":" + DateLength(second);
		return Date;
	}

	FDateTime Time = FDateTime::FromUnixTimestamp(UnixTime);
	Date = DateLength(Time.GetYear()) + "-" + DateLength(Time.GetMonth()) + "-" + DateLength(Time.GetDay()) + " " + DateLength(Time.GetHour()) + ":" + DateLength(Time.GetMinute()) + ":" + DateLength(Time.GetSecond());
	return Date;
}



FString UMyBlueprintFunctionLibrary::TimestampToHour(int64 UnixTime, bool bIsLocal /*= true*/)
{


	if (bIsLocal)
	{
		struct tm* timeinfo = new struct tm;
		localtime_s(timeinfo, &UnixTime);
		int hour = timeinfo->tm_hour;
		int minute = timeinfo->tm_min;
		return DateLength(hour);

	}

	FDateTime Time = FDateTime::FromUnixTimestamp(UnixTime);
	return DateLength(Time.GetHour());
}

FString UMyBlueprintFunctionLibrary::TimestampToHourAndMinute(int64 UnixTime, bool bIsLocal)
{


	if (bIsLocal)
	{
		struct tm* timeinfo = new struct tm;
		localtime_s(timeinfo, &UnixTime);
		int hour = timeinfo->tm_hour;
		int minute = timeinfo->tm_min;
		return DateLength(hour) + ":" + DateLength(minute);

	}

	FDateTime Time = FDateTime::FromUnixTimestamp(UnixTime);
	return DateLength(Time.GetHour()) + ":" + DateLength(Time.GetMinute());
}


FDateTime  UMyBlueprintFunctionLibrary::ConvertTimestampToDateTime(int64 Timestamp)
{
	time_t RawTime = static_cast<time_t>(Timestamp);
	struct tm LocalTime;
	_tzset();
	_localtime64_s(&LocalTime, &RawTime);

	FDateTime DateTime(LocalTime.tm_year + 1900, LocalTime.tm_mon + 1, LocalTime.tm_mday, LocalTime.tm_hour, LocalTime.tm_min, LocalTime.tm_sec);
	return DateTime;

}

FText UMyBlueprintFunctionLibrary::ConvertTimestampToSystemTime(int64 Timestamp)
{
	FDateTime DateTime = ConvertTimestampToDateTime(Timestamp);
	FText SystemTimeText = FText::FromString(DateTime.ToString());
	UE_LOG(LogTemp, Warning, TEXT("System Time: %s"), *SystemTimeText.ToString());
	return SystemTimeText;
}

3.获取屏幕分辨率

c++获取屏幕分辨率

UE C++ 正确加windows头文件


.h

//获取屏幕分辨率
UFUNCTION(BlueprintPure, Category = "Vince", meta = (WorldContext = "WorldContextObject", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"))
	static void GetScreenResolution(float& client_width, float& client_height);
.cpp

#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/PreWindowsApi.h"
 
//此处写要调用的windows的头文件
#include <Windows.h>
 
#include "Windows/PostWindowsApi.h"
#include "Windows/HideWindowsPlatformTypes.h"



void UMyBlueprintFunctionLibrary::GetScreenResolution(float& client_width, float& client_height)
{
	HDC hdc = GetDC(NULL);                           // 得到屏幕DC  
	client_width = GetDeviceCaps(hdc, HORZRES);      // 宽  
	client_height = GetDeviceCaps(hdc, VERTRES);     // 高
	ReleaseDC(NULL, hdc);                            // 释放DC
}

4.调用外部exe

UE打开外部.exe文件、安卓app应用、IOS应用等格式软件 三种方式总结_uec++打开exe-CSDN博客

UE4中调用外部exe_ue4打开exe程序节点-CSDN博客

.cpp


void UEarthLibrary::OpenRunexe(FString exePath)
{
	FPlatformProcess::CreateProc(*exePath, NULL, true, false, false, nullptr, -1, nullptr, nullptr);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值