使用@unreal.uclass()在UE中通过Python定义UObject

目标

学习在Python中使用 @unreal.uclass()@unreal.ufunction()unreal.uproperty()等定义UE的Object系统能识别的UObject,并验证效果。

Python装饰器

@符号是python中“装饰器”的意思,函数装饰器的用法可以参考:Python 函数装饰器 | 菜鸟教程

不过本篇不需要对它有准确的理解,只需要把它加在类/函数前一行即可。

1. 使用 @unreal.uclass() 定义UObject

可使用@unreal.uclass()定义Actor等UObject类。

样例脚本:

@unreal.uclass()
class MyTestPyActor(unreal.Actor):
	print("define MyTestPyActor")

效果:
在这里插入图片描述

2. 使用 unreal.uproperty 定义属性

(要注意,这个不是装饰器,没有@符号)

样例脚本:

@unreal.uclass()
class MyTestPyActor(unreal.Actor):
    TestFloat = unreal.uproperty(float)
    TestString = unreal.uproperty(str)
    TestStringArray = unreal.uproperty(unreal.Array(str))
    TestMap = unreal.uproperty(unreal.Map(str,int))

效果
在这里插入图片描述

3. 使用 @unreal.ustruct() 定义UStruct

样例脚本:

@unreal.ustruct()
class MyTestPyStruct(unreal.StructBase):
    Test = unreal.uproperty(int)

@unreal.uclass()
class MyTestPyActor(unreal.Actor):
    TestStruct = unreal.uproperty(MyTestPyStruct)

效果:
在这里插入图片描述

4. 使用 @unreal.uenum() 定义枚举

样例脚本:

@unreal.uenum()
class MyTestPyEnum(unreal.EnumBase):
    apple = unreal.uvalue(0)
    banana = unreal.uvalue(1)
    pear = unreal.uvalue(2)

@unreal.uclass()
class MyTestPyActor(unreal.Actor):
    Fruit = unreal.uproperty(MyTestPyEnum)

效果:
在这里插入图片描述

5. 使用meta

样例脚本:

@unreal.uclass()
class MyTestPyActor(unreal.Actor):
    TestPropA = unreal.uproperty(int,meta=dict(Category="CategoryAAA"))
    TestPropB = unreal.uproperty(float,meta=dict(Category="CategoryBBB"))

效果:
在这里插入图片描述

6. 使用 @unreal.ufunction 定义函数

官方文档中定义:

unreal.ufunction(meta=None, ret=None, params=None, override=None, static=None, pure=None, getter=None, setter=None)
decorator used to define UFunction fields from Python

下面根据参考文档给出其中重要的部分:

  • ret=<type> :指定函数的返回类型。带有 ufunction() 装饰器的 Python 函数通常只有一个返回值。必须指定,除非override=True。
  • params=[<type>,...]:指定函数参数的类型。必须指定,除非override=True。
  • override=True:指定此函数重写父类的虚UFunction,并且应继承其类型说明符。如果使用了此参数,则不再需要 retparams
  • meta=dict():meta。
  • static=True:指定此函数为static函数(用于蓝图库中的函数)

样例脚本:

@unreal.uclass()
class MyTestPyActor(unreal.Actor):
    TestPropA = unreal.uproperty(int)
    
    @unreal.ufunction(ret=bool,params=[str,int])
    def test_func(self,parm_str,parm_int):
        print(self.TestPropA)
        for i in range(parm_int+self.TestPropA):
            print(parm_str)
        return True 

效果:
使用后可以看到此函数:
在这里插入图片描述
触发后可以调用到此函数:
在这里插入图片描述

参考文档

这个是UE官方PythonAPI文档,这里面包含所有相关的装饰器和函数:
unreal — Unreal Python 4.27 (Experimental) documentation

此处有基础概念以及 @unreal.ufunction() 相关的补充:
UClass Decorators (Python) | Unreal Engine Community Wiki

此处有很多范例:
Python in Unreal Engine — The undocumented parts | by Filip Sivák | Medium

此处是一个创建蓝图库的范例:
Building UE4 Blueprint Function Libraries in Python | by Joe Graf | Medium

此处有在定义property使用meta的范例:
Create Blueprint Accessible Node in Python | Epic Developer Community

相关推荐

  1. 如何Python创建和使用定义模块

    2023-12-16 15:10:02       11 阅读
  2. 如何Python定义异常?

    2023-12-16 15:10:02       9 阅读
  3. UEUObject写到Package,加载Package获取UObject

    2023-12-16 15:10:02       11 阅读
  4. Python的函数定义使用

    2023-12-16 15:10:02       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-16 15:10:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-16 15:10:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-16 15:10:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-16 15:10:02       18 阅读

热门阅读

  1. 【C++】特殊类设计及单例模式

    2023-12-16 15:10:02       36 阅读
  2. 区间dp(刷表法转移):P5336

    2023-12-16 15:10:02       44 阅读
  3. 深度学习以CPU方式读入模型参数

    2023-12-16 15:10:02       42 阅读
  4. animate.css

    2023-12-16 15:10:02       44 阅读
  5. centos nginx 安装 stream 模块

    2023-12-16 15:10:02       36 阅读
  6. shell(49) : 多个服务器批量设置相互免密

    2023-12-16 15:10:02       39 阅读