如何为Unity创建一个可以在每个脚本中使用的类?

huangapple go评论53阅读模式
英文:

How to create a class for unity that can be used for every script?

问题

我在Unity中正在开发一个游戏,其中包含一些可购买的技能。已购买的技能将被存储在外部保存文件中,然后转换为名为PlayerData的类。我想让其他脚本能够访问这个类,而不需要在那些脚本中再次输入相同的类。

是否有一种方法可以创建一个类似全局类的东西?可以从其他脚本中访问吗?

英文:

I'm building a game in Unity which has some buyable skills in it. The bought skills are going to be stored in an external save file. Then being converted to a class called PlayerData. I want to make this class can be accessable from other scripts without typing the same class again in that script.

Is there a way to make like a global class? That can be accessed from other scripts?

答案1

得分: 0

我不确定你的意思是什么,但如果你指的是在脚本中不需要再次输入相同的类名,那么可能是你在另一个类中定义了这个类?或者你是指无法直接访问方法/变量而需要指定“类的哪个实例”,在这种情况下,你可以使用静态类:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members

英文:

I'm not sure exactly what you mean by

> without typing the same class again in that script.

But if you mean you can't access the class in other scripts, maybe you defined the class in another class?

Or do you mean you can't access methods/variables directly without stating "of which instance of the class", in that case, you can use static class:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members

huangapple
  • 本文由 发表于 2023年2月6日 14:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定