Search Unity

Resolved Partial Classes In Unity?

Discussion in 'Scripting' started by DTECTOR, Aug 10, 2022.

  1. DTECTOR

    DTECTOR

    Joined:
    Aug 23, 2020
    Posts:
    132
    Are there any hiccups or special requirements with using partial classes in Unity? Particularly when inheriting from MonoBehavior? Or do they work flawlessly without any issues? Does it impact the editor such as with adding a component to a gameobject?

    I am seeing some posts from 2014 saying don't do it as Unity does not behave well with with them, inferring that there may be some unexpected behaviors but no details on the issues and I am curious if this has changed. I also see a few blogs saying they have used partials with great success without any context into the experience or whether it is any different than simply just using a partial.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,648
    I mean, partial classes behave as if it were a single class, I don't see why it would be a problem...

    You just have to make sure you don't mix your files up, as only the file that matches your actual class name should work to make the component.

    Other than that, it should behave pretty much the same.
     
    DTECTOR likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Partials work fine in Unity. Same requirements: one file MUST match the classname precisely, capitalization and all, to function in the editor.

    NOTE: they can clash with asmdefs if there is a mismatch between the namespacing and the asmdef structure.
     
    DTECTOR likes this.
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Partial classes work fine, but C# is capable of way nicer architecture than resorting to 1990s C++
     
    Olleus and DTECTOR like this.
  5. DTECTOR

    DTECTOR

    Joined:
    Aug 23, 2020
    Posts:
    132
    Yeah I think you are right. I fell into the trap of making the class huge, probably bigger than it needs to be. I'm looking at breaking it up instead. I don't consider myself to be a very good programmer, if you have had any ideas off the top your head that I could look into I'd greatly appreciate it.
     
    hippocoder likes this.
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    And if your class does get on the weighty side I'd rather just use
    #Region
    to organise it.
     
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    If its getting that big would find parts of it to rip out to a regular C# class you instance in your component, or just multiple components with smaller roles you reference. Most large problems can be broken down into a bunch of small isolated problems with some thought.
     
    DTECTOR and Sphinks like this.
  8. DTECTOR

    DTECTOR

    Joined:
    Aug 23, 2020
    Posts:
    132
    Thanks guys, based on everything I've been reading, this sounds like the most reasonable long term solution. (breaking up into smaller classes)
     
    Last edited: Aug 11, 2022