当前位置:   article > 正文

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

使用@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")
  • 1
  • 2
  • 3

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

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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

效果
在这里插入图片描述

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

样例脚本:

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

@unreal.uclass()
class MyTestPyActor(unreal.Actor):
    TestStruct = unreal.uproperty(MyTestPyStruct)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

5. 使用meta

样例脚本:

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

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

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
  • 1
  • 2

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

  • 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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

参考文档

这个是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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/435223
推荐阅读
相关标签
  

闽ICP备14008679号