Thursday, July 4, 2024
HomeGame Developmentc# - How ought to I implement the thought behind this summary...

c# – How ought to I implement the thought behind this summary class setup in a Unity-Pleasant/Inspector-Usable approach?


First up, the identify of the query is horrible and I’m open to options. For context: I am nonetheless comparatively new to C#/Unity and it is arduous to ask questions correctly after I do not know the terminology. I am moderately skilled in Java/Python/javascript although so I am not new to programming typically. Additionally since I don’t know tips on how to ask this query in a structured approach I am simply going to point out all of the related stuff and hopefully somebody who understands it may put succinctly what I am attempting to ask.

I’ve this struct:

[System.Serializable]
public struct FieldLayer3D {
    public  Vector3        place;
    public  Quaternion     rotation;
    public  Vector3        scale;
    [SerializeReference] public  FieldGeneric3D subject; //that is the place we now have an issue
    public  BlendType      blendType;
    public  float          opacity;
    public  Channel        channel;
    public  float          smoothParameter;
    public  NormaliseType  NormaliseType;
    public  float          bias;
    public  bool           invertLayer;
}

My plan was to make use of arrays of those to outline some procedurally generated content material and let me mix noise and patterns in methods just like graphics packages mix layers of pictures. I want the bigger class that can include an array of FieldLayer3Ds to be serialisable so I can save/load my work. The FieldGeneric3D typed variable “subject” is what defines the kind of noise/sample/no matter and FieldGeneric3D is an summary class outlined as follows:

[System.Serializable]
public summary class FieldGeneric3D {
    //These are the fields for "FieldLayer3D" 
    public summary float FieldAt(Vector3 p);
}

The explanation I outlined it this fashion is as a result of every subject wants its personal set of variables which are totally different from one another, however will all the time worth sorts like Vector3 or float equivalent to in these two examples:

[System.Serializable]
public class FieldSDFBox3D: FieldGeneric3D {
    public Vector3 boxSize;
    public FieldSDFBox3D(Vector3 boxSize) { this.boxSize = boxSize; }
    public override float FieldAt(Vector3 p) { return PatternGenerator.Sample_SDF_Box(p, boxSize); }
}
[System.Serializable]
public class FieldSimplex3D : FieldGeneric3D {
    public int seed;
    public FieldSimplex3D(int seed) { this.seed = seed; }
    public override float FieldAt(Vector3 p) {
        NoiseGenerator noiseGenerator = new NoiseGenerator(seed);
        return noiseGenerator.Sample_Simplex(p);
    }
}

Nevertheless, that is what I see after I use the FieldLayer3D struct in a MonoBehaviour:
Image of the inspector showing that the Field variable has blank space next to it instead of any way to set the variable.

I used to be anticipating one thing analogous to how supplies work that you might drop in one of many script recordsdata that defines the suitable class and I used to be HOPING that after you dragged within the script it might allow you to mess with public variables for the occasion of the article.

Options I’ve thought and the problems with them:

  1. Simply use an enum as an alternative of this class construction factor and let no matter object is controlling the sector technology deal with it. This does not work as a result of I want to have the ability to set variables for every layer and the kinds of variable are totally different relying on the kind of subject.

  2. Make FieldGeneric3D inherit from ScriptableObject and create belongings for each layer I ever wish to make. That is problematic as a result of are you able to think about how messy and clunky issues would get in a program like photoshop if each time you wished so as to add a layer you wanted to reserve it as a brand new seperate file? This simply is not workable, and the identical goes for making it a MonoBehaviour as a result of then I have to fill my scene with dummy GameObjects for each layer of noise. I additionally suppose these could be troublesome to simplify and serialise. It’s potential Sick make the mother or father class that mixes and shops these layers a ScriptableObject as a result of I might like the mix to be a saveable asset with out an excessive amount of hassle however I am leaning in direction of utilizing JSON for the entire saving and loading of layers.

  3. Use the enum strategy however simply embrace a bunch of null variables which are solely ever learn from if the proper kind of layer is used. This is not superb for extensibility sooner or later as a result of I will have to replace issues in a number of locations. It additionally implies that if I add a brand new kind of FieldGeneric3D with new variables I will want so as to add these variables to the FieldLayer3D class which could break saving/loading for all those I’ve already saved. I additionally do not know a lot about writing my very own editor or something so going about and conditionally hiding all of the unused variables feels like one thing that might lead to points if I implement it incorrect. Leaving all of them all the time seen will not be superb as a result of I want different individuals to have the ability to use this software with out an excessive amount of confusion or studying of manuals.

  4. Some fancy customized editor shenanigans. Looks as if it might take a very long time to discover ways to do correctly, so I used to be attempting to simply make easy lessons and hope Unity would perceive my intent.

The last word query is that this I suppose: How do I obtain what I wish to obtain, throughout the Unity ecosystem in such a approach as to be editable within the inspector, save/loadable, comprehensible by different individuals, and with out future additions turning into a significant headache?

Additionally, I’ve seen this How do I expose a subject of an summary kind to the Inspector? query and the one factor I might see there of assist was “Composition over Inheritance” however I am unsure how that may be utilized right here.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments