Ue4反射使用

// Fill out your copyright notice in the Description page of Project Settings.


#include "HelloGameModeBase.h"
#include"MyObject.h"
#include "MiddleStudent.h"
#include "UObject/UObjectHash.h"
#include "UObject/UObjectIterator.h"
AHelloGameModeBase::AHelloGameModeBase()
{
	UE_LOG(LogTemp, Warning, TEXT("ddddddddddddddddddddddddd"));

	//1.1 创建MyObject对象
	UMyObject* MyObject = NewObject<UMyObject>();
	//2.1 得到MyObject对象的UClass,并且得到类的名称
	UClass* ObjectClass = MyObject->GetClass();
	FString ClassName = ObjectClass->GetName();
	
	//1.2 通过对象得到对象的名称
	FString ObjectName = MyObject->GetName();
	UE_LOG(LogTemp, Warning, TEXT("MyObjectname..%s"),*ObjectName);
	UE_LOG(LogTemp, Warning, TEXT("classname..%s"),*ClassName);

	//2.2 获取父类的名称
	UMiddleStudent* middleStudent = NewObject<UMiddleStudent>();
	UClass* parentClass = middleStudent->GetClass()->GetSuperClass();
	FString parentClassName = parentClass->GetName();
	UE_LOG(LogTemp, Warning, TEXT("parentClass name;::%s"), *parentClassName);

	//2.3.1 获取类(string)属性
	for (UProperty* Property = ObjectClass->PropertyLink; Property; Property = Property->PropertyLinkNext) {
		FString propertyName = Property->GetName();
		FString propertyType = Property->GetCPPType();
		if (propertyType == "FString") {
			UStrProperty* StringProperty = Cast<UStrProperty>(Property);
			void* addr = StringProperty->ContainerPtrToValuePtr<void>(MyObject);
			FString PropertyValue = StringProperty->GetPropertyValue(addr);
			UE_LOG(LogTemp, Warning, TEXT("myObject has type is %s,name is %s,Value is %s"), 
				*propertyType,*propertyName ,*PropertyValue);
	//2.3.2 设置类(string)属性				
			StringProperty->SetPropertyValue(addr, "lisi");
			FString afterValue = StringProperty->GetPropertyValue(addr);
			UE_LOG(LogTemp, Warning, TEXT("after set value:::%s"), *afterValue);
		}
	}
	//2.4 获取类的方法
	for (TFieldIterator<UFunction> iterOfFunc(ObjectClass); iterOfFunc; ++iterOfFunc) {
		UFunction* function = *iterOfFunc;
		FString funcName = function->GetName();
		UE_LOG(LogTemp, Warning, TEXT("MyObject function:::%s"), *funcName);
	}
	//2.4.2通过名称获取方法
	UFunction* funPtr = UMyObject::StaticClass()->FindFunctionByName(TEXT("Study"), EIncludeSuperFlag::ExcludeSuper);
	if (funPtr) {
		FString studyFucName = funPtr->GetName();
		UE_LOG(LogTemp, Warning, TEXT("find study function name:::%s"), *studyFucName);

	//2.4.3 调用函数
		//1.给参数分配空间
		uint8* params = static_cast<uint8*>(FMemory_Alloca(funPtr->ParmsSize));
		//2.参数赋值
		for (TFieldIterator<UProperty> iterOfParam(funPtr); iterOfParam; ++iterOfParam) {
			UProperty* fucParam = *iterOfParam;
			FString pName = fucParam->GetName();
			if (pName == FString("InGame"))
			{
				*fucParam->ContainerPtrToValuePtr<FString>(params) = "ball";
			}
		}
		//3.调用
		MyObject->ProcessEvent(funPtr, params);
	}

	//2.5 查找特定类的所有子类
	TArray<UClass*> derivedClassArr;
	GetDerivedClasses(UMyObject::StaticClass(), derivedClassArr, true);
	for (int32 index = 0; index < derivedClassArr.Num(); index++)
	{
		UClass* curDerClass = derivedClassArr[index];
		FString curDerClassName = curDerClass->GetName();
		UE_LOG(LogTemp, Warning, TEXT("derived class name:::%s"), *curDerClassName);
	}

	//2.6 查找由指定类生成出的所有对象
	TArray<UObject*> objectArr;
	GetObjectsOfClass(UMyObject::StaticClass(), objectArr, true);
	for (int32 index = 0; index < objectArr.Num(); index++) {
		FString curObjName = objectArr[index]->GetName();
		UE_LOG(LogTemp, Warning, TEXT("curObjName::::%s"), *curObjName);
	}

	//2.7 通过名称查找类
	UClass* findedClass = FindObject<UClass>(ANY_PACKAGE, *FString("MyObject"), true);
	if (findedClass)
	{
		UE_LOG(LogTemp, Warning, TEXT("find class::::%s"), *findedClass->GetName());
	}

	//2.8遍历所有类
	/*for (TObjectIterator<UClass>cIt; cIt; cIt++) {
		FString cName = cIt->GetName();
		UE_LOG(LogTemp, Warning, TEXT("all class::::%s"), *cName);
	}*/

	//3.1 根据名称查enum
	UEnum* findedEnum = FindObject<UEnum>(ANY_PACKAGE, *FString("MyEnum"), true);
	if (findedEnum)
	{
		UE_LOG(LogTemp, Warning, TEXT("find class::::MyEnum"));
	}










	//100.判断是否是父类
	UClass* class1 = UMiddleStudent::StaticClass();
	UClass* class2 = UMyObject::StaticClass();
	UClass* class3 = AActor::StaticClass();

	bool ischild = class1->IsChildOf(class2);
	UE_LOG(LogTemp, Warning, TEXT("IsChildOf ::%d"), ischild);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值