Unity C#中多类型员工变量的优化使用方案咨询
Hey there! Let's fix that messy variable setup—having separate fields for every employee type is a nightmare to maintain, especially if you add more roles down the line. Here's a cleaner, more scalable approach tailored for your Unity business tycoon game:
Step 1: Create a Struct to Encapsulate Staff Data
First, we'll bundle the Free, Used, and calculated Total values into a single reusable structure. Making it serializable lets you tweak initial values directly in the Unity Inspector, which is perfect for testing.
[System.Serializable] public struct StaffPool { public int Free; public int Used; // Auto-calculate total staff instead of storing it manually (avoids sync issues!) public int Total => Free + Used; }
Step 2: Define Employee Types with an Enum
This eliminates typos and makes it easy to add new roles later without rewriting tons of code:
public enum EmployeeRole { Labor, RnD, Accounting, Technic, Sales, Market // Add new roles here when you need them! }
Step 3: Implement a Staff Manager Class
Choose one of these two approaches based on your needs:
Option 1: Inspector-Friendly (Great for Debugging)
If you want to set initial staff counts visually in Unity, declare serialized StaffPool variables for each role:
public class StaffManager : MonoBehaviour { [SerializeField] private StaffPool _laborStaff; [SerializeField] private StaffPool _rndStaff; [SerializeField] private StaffPool _accountingStaff; [SerializeField] private StaffPool _technicStaff; [SerializeField] private StaffPool _salesStaff; [SerializeField] private StaffPool _marketStaff; // Calculate total company-wide staff automatically public int TotalStaff => _laborStaff.Total + _rndStaff.Total + _accountingStaff.Total + _technicStaff.Total + _salesStaff.Total + _marketStaff.Total; // Call this from your "Hire X" buttons public void HireEmployee(EmployeeRole role) { switch (role) { case EmployeeRole.Labor: _laborStaff.Free++; break; case EmployeeRole.RnD: _rndStaff.Free++; break; case EmployeeRole.Accounting: _accountingStaff.Free++; break; case EmployeeRole.Technic: _technicStaff.Free++; break; case EmployeeRole.Sales: _salesStaff.Free++; break; case EmployeeRole.Market: _marketStaff.Free++; break; } } // Call this when assigning staff to a project (returns true if successful) public bool AssignStaffToProject(EmployeeRole role, int requiredStaff) { switch (role) { case EmployeeRole.Labor: if (_laborStaff.Free >= requiredStaff) { _laborStaff.Used += requiredStaff; _laborStaff.Free -= requiredStaff; return true; } break; case EmployeeRole.RnD: if (_rndStaff.Free >= requiredStaff) { _rndStaff.Used += requiredStaff; _rndStaff.Free -= requiredStaff; return true; } break; // Repeat logic for other roles } return false; // Return false if not enough free staff } }
Option 2: Dynamic Dictionary (Great for Scalability)
If you plan to add many roles or want more concise code, use a dictionary to manage all staff pools:
public class StaffManager : MonoBehaviour { private Dictionary<EmployeeRole, StaffPool> _staffPools; private void Awake() { // Initialize all roles with empty staff pools _staffPools = new Dictionary<EmployeeRole, StaffPool>(); foreach (EmployeeRole role in Enum.GetValues(typeof(EmployeeRole))) { _staffPools[role] = new StaffPool(); } } public int TotalStaff => _staffPools.Values.Sum(pool => pool.Total); public void HireEmployee(EmployeeRole role) { if (_staffPools.TryGetValue(role, out var pool)) { pool.Free++; _staffPools[role] = pool; // Structs are value types, so we need to reassign } } public bool AssignStaffToProject(EmployeeRole role, int requiredStaff) { if (_staffPools.TryGetValue(role, out var pool)) { if (pool.Free >= requiredStaff) { pool.Used += requiredStaff; pool.Free -= requiredStaff; _staffPools[role] = pool; return true; } } return false; } }
How to Use This in Your Game
- Hire Buttons: For your "Hire Labor" button, add an
OnClickevent that callsStaffManager.HireEmployee(EmployeeRole.Labor). Repeat this for other hire buttons with their respective roles. - Project Assignment: When starting a project (like developing a motherboard), call
StaffManager.AssignStaffToProject(EmployeeRole.RnD, ProjectManager.requiredstaff). Check the return value to handle cases where there aren't enough free staff (e.g., show a warning to the player).
This approach cuts down on repetitive code, reduces bugs from manual total calculations, and makes it trivial to add new employee types later.
内容的提问来源于stack exchange,提问作者blake janssen

