UE4源码中不常见但很有用的API(包含slate常用方法)(持续更新)

1.SNew

该函数在Slate层中经常使用,常用于创建一个Slate控件,相当于UObject层中的NewObject<>();

具体代码如下所示,后面.HAlign(HAlign_Right)这种都是设置这个Slate控件的样式,比如该Box是水平居中还是居右,比如Padding是什么比例等等

SNew(SBox)
		.HAlign(HAlign_Right)
			[
				SNew(SHorizontalBox)
				+SHorizontalBox::Slot()
				.Padding(FMargin(5.0f, 0.0f))
				.AutoWidth()
				[
					SNew(STextBlock)
					.Font(FEditorStyle::GetFontStyle("TinyText"))
					.Text(this, &FPaperTileMapDetailsCustomization::GetLayerSettingsHeadingText)
					.ToolTipText(LOCTEXT("LayerSettingsTooltip", "Properties specific to the currently selected layer"))
					]
				]

 

2.  TSharedRef<SWidget>  UWidget::TakeWidget();

是将UWidget转成SWidget的一种方式,SWidget在Slate层,UWidget在UObject层,UWidget套了一层SWidget,可以理解为UWidget只是个壳,平时跟一下源码就可以发现,在U开头的控件中,一般在构造函数等中通过SNew()方式创建Slate层中的控件,然后将该引用放在Private中

该函数的作用是:做了一个UMG通向Slate控件的桥梁,我们自定义的UMG控件可以通过该函数转成Slate中的SWidget

官方解释如下:

	/**
	 * Gets the underlying slate widget or constructs it if it doesn't exist.  If you're looking to replace
	 * what slate widget gets constructed look for RebuildWidget.  For extremely special cases where you actually
	 * need to change the the GC Root widget of the constructed User Widget - you need to use TakeDerivedWidget
	 * you must also take care to not call TakeWidget before calling TakeDerivedWidget, as that would put the wrong
	 * expected wrapper around the resulting widget being constructed.
	 */
	TSharedRef<SWidget> TakeWidget();

如何向Swidget转成UUserWidget?

 bool UMyBlueprintFunctionLibrary::TestUMGWidget(UUserWidget* InWidget)
 {
 
     TSharedRef<SWidget> Widget = InWidget->TakeWidget();
     
     TSharedRef<SObjectWidget> ObjWidget = StaticCastSharedRef<SObjectWidget>(Widget);
 
     return ObjWidget->GetWidgetObject() == InWidget;
 
 }

 

3.FSlateTexture2DRHIRef* FAssetThumbnailPool::AccessTexture( const FAssetData& AssetData, uint32 Width, uint32 Height )

该函数作用:设置好的AssetData和图片长宽高,用于将渲染好的缩略图数据返回成FSlateTexture2DRHIRef*

	/**
	 * Accesses the texture for an object.  If a thumbnail was recently rendered this function simply returns the thumbnail.  If it was not, it requests a new one be generated
	 * No assumptions should be made about whether or not it was rendered
	 *
	 * @param Asset The asset to get the thumbnail for
	 * @param Width	The width of the thumbnail
	 * @param Height The height of the thumbnail
	 * @return The thumbnail for the asset or NULL if one could not be produced
	 */
	FSlateTexture2DRHIRef* AccessTexture( const FAssetData& AssetData, uint32 Width, uint32 Height );

 

4.

const FObjectThumbnail* ThumbnailTools::FindCachedThumbnail( const FString& InFullName )

FObjectThumbnail* ThumbnailTools::GetThumbnailForObject( UObject* InObject )

传入UObject的引用或路径就可以拿到内存中渲染好的FObjectThumbnail

/** Searches for an object's thumbnail in memory and returns it if found */
    const FObjectThumbnail* FindCachedThumbnail( const FString& InFullName )

/** Returns the thumbnail for the specified object or NULL if one doesn't exist yet */
	FObjectThumbnail* GetThumbnailForObject( UObject* InObject )

 

5 ObjectType* TSharedPtr::Get()

在Slate层经常用TSharedPtr保存指针,但是如果函数返回一个TSharedPtr是不行的,因为它是只读的,不能赋值,我们可以通过Get()方法返回该对象的引用

	/**
	 * Returns the object referenced by this pointer, or nullptr if no object is reference
	 *
	 * @return  The object owned by this shared pointer, or nullptr
	 */
	FORCEINLINE ObjectType* Get() const
	{
		return Object;
	}

 

UTexture2D* FImageUtils::CreateTexture2D

(int32 SrcWidth, int32 SrcHeight, const TArray<FColor> &SrcData, UObject* Outer, const FString& Name, const EObjectFlags &Flags, const FCreateTexture2DParameters& InParams)

在FimageUtils类中封装了一些创建Texture2D,导入导出的函数

	/**
	 * Creates a 2D texture from a array of raw color data.
	 *
	 * @param SrcWidth		Source image width.
	 * @param SrcHeight		Source image height.
	 * @param SrcData		Source image data.
	 * @param Outer			Outer for the texture object.
	 * @param Name			Name for the texture object.
	 * @param Flags			Object flags for the texture object.
	 * @param InParams		Params about how to set up the texture.
	 * @return				Returns a pointer to the constructed 2D texture object.
	 *
	 */
	ENGINE_API static UTexture2D* CreateTexture2D(int32 SrcWidth, int32 SrcHeight, const TArray<FColor> &SrcData, UObject* Outer, const FString& Name, const EObjectFlags &Flags, const FCreateTexture2DParameters& InParams);

 

7 FString UObjectBaseUtility::GetPathName(  ) const

获取UObject中路径,UObjectBaseUtility是UObject的父类

/**
 * Returns the fully qualified pathname for this object, in the format:
 * 'Outermost.[Outer:]Name'
 *
 * @param	StopOuter	if specified, indicates that the output string should be
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值