# ue5使用UnrealSharp插件编译c#代码
听说黑神话悟空开发语言是C++; C#和蓝图,以下是我的UE入门学习步骤。由于有3年unity的基础,玩起UE5的C#和蓝图也就痛苦两个周吧。至于C++就不知道要花多少时间了。
## 一、安装插件
### 1.先决条件
安装 .NET 9.0 SDK,适用ue5.6
本人安装 .NET 8.0 SDK,适用ue5.5官网下载地址:
https://dotnet.microsoft.com/en-us/download/dotnet
备用下载地址:https://www.aigei.com/item/dotnet_sdk_8.html
### 2.将 UnrealSharp 安装到您的项目中
插件下载地址:https://www.aigei.com/item/ue5_c_cha_jian.html
a.克隆此存储库
https://github.com/UnrealSharp/UnrealSharp
b.或者下载插件压缩包
在这里插入图片描述

### 3.将 UnrealSharp插件解压包 放在 Unreal Engine 项目的 ProjectRootDirectory/Plugins 文件夹(如果不存在,请创建 Plugins 文件夹)中。
在这里插入图片描述

### 4.启动 UnrealEngine
重新打开您的项目,编译 UnrealSharp。
使用您选择的 IDE 像任何其他 Unreal Engine 插件一样编译插件。
在这里插入图片描述
查看插件:
在这里插入图片描述
在这里插入图片描述
恭喜你成功安装了插件!!!!!!!!

## 二、编写C#脚本

### 6.Unreal Engine 完全打开后,看 ProjectRootDirectory/Script 目录,现在将有一个 C# 项目,它应该如下所示:
在这里插入图片描述
在这里插入图片描述
### 7.开始编写脚本
用vs2022打开 ManagedYourProjectName.sln 文件并创建一个 C# 类。
a.右键解决方案资源管理器的你的项目名创建自己写的类
右键ManagedProject02
在这里插入图片描述
b.把下面C#代码复制到你写的类中,注意修改成自己写的项目名和类名。
1,2分别是项目名、类名
2,3保持一致,并且前面加大写字母A,原因是继承AActor
在这里插入图片描述


using UnrealSharp.Attributes;
using UnrealSharp.Engine;

namespace ManagedProject02;

//Currently UClass attribute is a must for the editor to recognize the class
[UClass]
public class AMyFirstC_Sharp : AActor
{

    //Optional constructor
    protected AMyFirstC_Sharp()
    {

    }

    protected override void BeginPlay()
    {
        PrintString("Hello from C#!");
        base.BeginPlay();
    }

    [UProperty(PropertyFlags.BlueprintReadOnly)]
    public int MyInt { get; set; }

    [UProperty(PropertyFlags.BlueprintReadOnly)]
    public float MyFloat { get; set; }

    [UProperty(PropertyFlags.BlueprintReadOnly)]
    public string MyString { get; set; }

    [UProperty(DefaultComponent = true, RootComponent = true)]
    public UStaticMeshComponent MyRootMesh { get; set; }

    [UProperty(DefaultComponent = true)]
    public UStaticMeshComponent MyOtherMesh { get; set; }

    [UProperty(DefaultComponent = true, AttachmentComponent = nameof(MyRootMesh))]
    public UStaticMeshComponent MyMeshAttachedToRoot { get; set; }

    [UFunction(FunctionFlags.BlueprintCallable)]
    public void MyFunction(bool myBool, int MyInt)
    {
        PrintString("Hello from MyFunction!");
    }
}

### 8.在Unreal Engine中找到自己的类
现在回到 Unreal Engine,编译你的代码,并且你的类应该能够在编辑器中找到。
在这里插入图片描述
恭喜你完成了用C#写的类!

备注:C#和蓝图联动,在屏幕输出看下文
https://blog.csdn.net/weixin_43834973/article/details/141934075?spm=1001.2014.3001.5502

更新2025年9月1日
使用UE5.6导入新版UnrealSharp插件更方便了
插件使用教程
https://www.unrealsharp.com/getting-started/your-first-script
编写C#创建一个静态网格体旋转代码如下:

using UnrealSharp.Engine;
//using UnrealSharp.Engine.Core.Modules;
using UnrealSharp.Attributes;
using UnrealSharp.CoreUObject;



namespace MyFirstCSharpClass;
[UClass]
public class AMyFirstCSharpClass : AActor
{
    //定义声明组件
    [UProperty(DefaultComponent = true, RootComponent = true)]
    public USceneComponent MySceneComponent { get; set; }

    [UProperty(DefaultComponent = true, AttachmentComponent = nameof(MySceneComponent))]
    public UStaticMeshComponent MyStaticMeshComponent { get; set; }

    [UProperty(DefaultComponent = true, AttachmentComponent = nameof(MyStaticMeshComponent))]
    public UBoxComponent MyBoxComponent { get; set; }



    
    /// <summary>
    /// 变量、组件初始化
    /// </summary>
    public override void ConstructionScript()
    {
       //int boxSize;
        FVector min, max;
        MyStaticMeshComponent.GetLocalBounds(out min, out max);//获取MyStaticMeshComponent的边界大小
        FVector sizeOfBoxCollider = max - min;
        MyBoxComponent.SetBoxExtent(sizeOfBoxCollider / 2);
        MyBoxComponent.SetHiddenInGame(false);//设置隐藏

        base.ConstructionScript();
    }

    //开始执行
    protected override void BeginPlay()
    {
        //开始时在屏幕输出"Hello world form C#!"
        PrintString("Hello world form C#!");

        base.BeginPlay();
    }

   //每一帧执行静态网格体绕Y轴旋转
    public override void Tick(float deltaSeconds)
    {

        FRotator deltaRotation = new FRotator(0, 120 * deltaSeconds, 0);
        FHitResult hitResult;
        AddActorWorldRotation(deltaRotation,false,out hitResult,true);

        base.Tick(deltaSeconds);
    }


    //可以在蓝图调用的自定义函数方法
    [UFunction (FunctionFlags.BlueprintCallable)]
    public void MyCs_Function(bool Mybool)
    {
       
        PrintString("Hello from MyFunction!");
        PrintString("Bool is" + Mybool);
    }

}
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐