# ue5使用UnrealSharp插件编译c#代码
听说黑神话悟空开发语言是C++; C#和蓝图,以下是我的UE入门学习步骤。由于有3年unity的基础,玩起UE5的C#和蓝图也就痛苦两个周吧。至于C++就不知道要花多少时间了。
## 一、安装插件
### 1.先决条件
安装 .NET 8.0 SDK,官网下载地址:
https://dotnet.microsoft.com/en-us/download/dotnet
### 2.将 UnrealSharp 安装到您的项目中
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.右键解决方案资源管理器的你的项目名创建自己写的类
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