英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论