Ok, this is kinda bugging me lately in the regards that I can't use my standard OOP design techniques with unity. How do others deal with complicated class hiearchies with Unity 3 ?
As a concrete example I propose this weapon system:
I have a class called GunData, which holds info about a particular gun, fire rate, recoil, muzzle velocity, etc... Along with modifier data, shoulder state data, and secondary gun id's.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class GunData
{
public string Name = "Unnamed";
public float FireRate = 0.05f;
public float Recoil = 1.0f;
//...
public int GunID;
public Transform GunMesh;
public GunData ShoulderData;
public GunType WeaponType = GunType.Bullet;
public int SecondaryGunID;
public GunAttachment[] AttachmentData;
public enum GunType
{
Rockets,
Energy,
Bullet,
Rail,
Explosives,
Placement
}
}
The first bug I find is that when I have a 2nd class, like GunAttachment with a different enum but a variable with the same name, the editor is using the wrong enum definition for GunData.
As in this does not work:
[System.Serializable]
public class GunAttachment
{
public GunData Modifier;
public AttachmentType WeaponType;
public enum AttachmentType
{
Scope,
Stock,
Handle,
Clip,
Silencer,
Attachment
}
}
But renaming the variable WeaponType to SomethingElse lets it work completely even though the enums are completely different in different classes.
The 2nd issue I am finding is that I can't have a public reference to my own class. In this example the variable for ShoulderData does not show up in the inspector. My guess is that it would cause a stack blow out if unity creates memory for this, but I am intending it to be a reference to a GunData instance.
The real question I have is how do you design classes around this ? Do you make a 2nd class with all the same variables?