简体   繁体   English

使用大量变量的更好方法C#Unity

[英]Better way to use lots of variables C# Unity

I am working on a game at the moment in Unity, it is a 2D Based game with certain components in 3D. 我目前正在Unity中开发一款游戏,它是一款基于2D的游戏,带有某些3D组件。 It is a Business Tycoon Game. 这是一个商业大亨游戏。 So I'm using C# for my scripts and I currently have different staff types and need 3 variables for each staff type. 因此,我在脚本中使用C#,目前我拥有不同的人员类型,并且每种人员类型都需要3个变量。 XStaffTotal, XStaffFree, XStaffUsed and finally TotalStaff Which is the Sum of all XStaffTotal Variables. XStaffTotal,XStaffFree,XStaffUsed,最后是TotalStaff,这是所有XStaffTotal变量的总和。 My question is what would be a good or the best way to use the variables. 我的问题是使用变量的最佳或最佳方法是什么。 This is what I chose first, I think there is a better way but I can not figure it out. 这是我首先选择的,我认为有更好的方法,但我无法弄清楚。

public int TotalStaff;
public int LaborStaffFree;
public int LaborStaffUsed;
public int TotalLaborStaff;
public int RnDStaffFree;
public int RnDStaffUsed;
public int TotalRnDStaff;
public int AccountingStaffFree;
public int AccountingStaffUsed;
public int TotalAccountingStaff;
public int TechnicStaffFree;
public int TechnicStaffUsed;
public int TotalTechnicStaff;
public int SalesStaffFree;
public int SalesStaffUsed;
public int TotalSalesStaff;
public int MarketStaffFree;
public int MarketStaffUsed;
public int TotalMarketStaff;

Edit 编辑

More context coming up. 即将出现更多上下文。 So basically I have 9 Buttons, Each time you click on one of the buttons, be it Hire Labor or Hire RnD it will +1 to the respective XStaffFree, so if I click Hire Labor. 因此,基本上我有9个按钮,每次您单击一个按钮时,无论是Hire Labor还是Hire RnD,它都会+1到相应的XStaffFree,因此,如果我单击Hire Labor。

LaborStaffFree += 1;

Then when you undertake a project in the game which requires staff to be assigned say researching motherboards, The project will have a set int of required staff, so 然后,当您在游戏中进行一个需要分配人员(例如研究主板)的项目时,该项目将具有一组所需的人员,因此

LaborStaffUsed += ProjectManager.requiredstaff;
LaborStaffFree -= LaborStaffUsed;

With ProjectManager being another c# script and requiredstaff being the aforementioned int. 其中ProjectManager是另一个c#脚本,而requiredstaff是上述int。

PS. PS。 This is all in Unity 这一切都在Unity中

You can do this by creating some classes and structs. 您可以通过创建一些类和结构来实现。 For example in your case, first create a simple struct for staff info: 例如,在您的情况下,首先为员工信息创建一个简单的结构:

public struct StaffInfo
{
     public int StaffFree;
     public int StaffUsed;
     public int TotalStaff;
}

And then use a class or struct that contains different property of StaffInfo type. 然后使用包含StaffInfo类型的不同属性的类或结构。

public class Staffs
{
      public StaffInfo LaborStaff {get;set;}
      public StaffInfo RnDStaff {get;set;}
      public StaffInfo AccountingStaff {get;set;}
      public StaffInfo TechnicStaff {get;set;}
      public StaffInfo SalesStaff {get;set;}
      public StaffInfo MarketStaff {get;set;}
      public int TotalStaff {get;set;}
}

And if TotalStaff is always equalled to used + free you can use read-only property with getter only. 而且,如果TotalStaff始终等于used + free,则可以将只读属性与getter一起使用。

public int TotalStaff => StaffFree + StaffUsed;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM