赞
踩
// 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); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。