如何将编码类导入C#脚本?

vic*_*cky 3 c# unity-game-engine

static byte[] GetData()
{
string s = "this is some text";
byte[] data = Encoding.ASCII.GetBytes(s);
return data;
}
Run Code Online (Sandbox Code Playgroud)

我发现这段代码很有用,但我遇到了GetData()方法的问题.编译代码后,我得到消息说..

错误:当前上下文中不存在名称"编码".

那么如何将Encoding类导入C#脚本?

Dam*_*Arh 6

您有两种选择:

  1. 导入脚本文件顶部的命名空间:

    using System.Text;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用完全限定的类名:

    byte[] data = System.Text.Encoding.ASCII.GetBytes(s);
    
    Run Code Online (Sandbox Code Playgroud)