get_basename
2026/4/14大约 3 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — get_basename
get_basename
定义
get_basename 用来从一个文件路径中提取"基础名"——也就是去掉最后的扩展名(后缀)之后的部分。你可以把它想象成"剥掉文件的外衣":"player.png" 剥掉扩展名后就是 "player"。
这个方法在处理文件路径时特别有用,比如你想加载同名但不同格式的资源文件时。
函数签名
C#
// Godot.StringExtensions 方法
public static string GetBaseName(this string instance)GDScript
func get_basename() -> String参数说明
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
| 无 | — | — | 这是一个无参数方法,直接对字符串本身操作 |
返回值
类型: string / String
返回去掉最后一个 . 及其后面扩展名部分之后的字符串。如果字符串中没有 .,则原样返回。
代码示例
基础用法
C#
using Godot;
public partial class GetBasenameExample : Node
{
public override void _Ready()
{
string path1 = "res://assets/player.png";
GD.Print(path1.GetBaseName());
// 运行结果: res://assets/player
string path2 = "scene.tscn";
GD.Print(path2.GetBaseName());
// 运行结果: scene
string noDot = "README";
GD.Print(noDot.GetBaseName());
// 运行结果: README
}
}GDScript
extends Node
func _ready():
var path1 := "res://assets/player.png"
print(path1.get_basename())
# 运行结果: res://assets/player
var path2 := "scene.tscn"
print(path2.get_basename())
# 运行结果: scene
var no_dot := "README"
print(no_dot.get_basename())
# 运行结果: README实际场景:加载同名不同格式的资源
C#
using Godot;
public partial class ResourceLoader : Node
{
public override void _Ready()
{
// 用户选择了一个 PNG 图片,我们想自动找对应的 .import 或 .tres 文件
string selectedImage = "res://textures/ground_grass.png";
string baseName = selectedImage.GetBaseName();
GD.Print($"基础名: {baseName}");
// 用基础名拼接其他格式的路径
string normalMapPath = baseName + "_normal.png";
string textureResPath = baseName + ".tres";
GD.Print($"法线贴图路径: {normalMapPath}");
GD.Print($"纹理资源路径: {textureResPath}");
// 运行结果:
// 基础名: res://textures/ground_grass
// 法线贴图路径: res://textures/ground_grass_normal.png
// 纹理资源路径: res://textures/ground_grass.tres
}
}GDScript
extends Node
func _ready():
# 用户选择了一个 PNG 图片,我们想自动找对应的 .import 或 .tres 文件
var selected_image := "res://textures/ground_grass.png"
var base_name := selected_image.get_basename()
print("基础名: " + base_name)
# 用基础名拼接其他格式的路径
var normal_map_path := base_name + "_normal.png"
var texture_res_path := base_name + ".tres"
print("法线贴图路径: " + normal_map_path)
print("纹理资源路径: " + texture_res_path)
# 运行结果:
# 基础名: res://textures/ground_grass
# 法线贴图路径: res://textures/ground_grass_normal.png
# 纹理资源路径: res://textures/ground_grass.tres进阶用法:处理多个点号的文件名
C#
using Godot;
public partial class GetBasenameAdvanced : Node
{
public override void _Ready()
{
// 只去掉最后一个扩展名
string file1 = "player.anim.backup";
GD.Print(file1.GetBaseName());
// 运行结果: player.anim
// 隐藏文件(以点开头的文件名)
string file2 = ".hidden_file";
GD.Print(file2.GetBaseName());
// 运行结果: (空字符串,因为点前面没有内容)
// 路径中包含多个点号
string file3 = "res://v1.2.0/data.config.json";
GD.Print(file3.GetBaseName());
// 运行结果: res://v1.2.0/data.config
}
}GDScript
extends Node
func _ready():
# 只去掉最后一个扩展名
var file1 := "player.anim.backup"
print(file1.get_basename())
# 运行结果: player.anim
# 隐藏文件(以点开头的文件名)
var file2 := ".hidden_file"
print(file2.get_basename())
# 运行结果: (空字符串,因为点前面没有内容)
# 路径中包含多个点号
var file3 := "res://v1.2.0/data.config.json"
print(file3.get_basename())
# 运行结果: res://v1.2.0/data.config注意事项
- 只去掉最后一个扩展名:如果文件名有多个点号(如
file.tar.gz),get_basename只去掉最后的.gz,变成file.tar。如果你需要去掉所有扩展名,需要多次调用或自己写逻辑。 - 路径分隔符不受影响:
get_basename只关心最后一个.的位置,不会处理路径中的/或\。如果只想获取不含路径的文件名,请先用GetFile(GDScript 中get_file)。 - 以点开头的文件:像
".gitignore"这样的文件,get_basename会返回空字符串,因为点之前没有内容。 - C# 的替代方案:在 C# 中也可以用
System.IO.Path.GetFileNameWithoutExtension(),但GetBaseName保留完整路径前缀,这在 Godot 的res://路径下更方便。
