Search Unity

UML in Unity

Discussion in 'General Discussion' started by codebeans, Apr 16, 2015.

  1. codebeans

    codebeans

    Joined:
    Apr 7, 2014
    Posts:
    79
    Hey folks,
    I am an IT student with a big interest in Software Engineering.
    So writing games, assets and a few prototpyes for a couple of years in unity, I kind of miss the usual approach to model the software first (UML, Requirements specificaiton, code generation...) in Unity.

    Now, I know that things are a bit different in Unity, such as instantiating GameObjects, but still...
    I think a nice toolset would be great.

    Not only should it speed up coding, but also try to get the programmer some best practices, since most people I get in contact with, who are now pretty good programmers, started to get in touch with coding via games and game development.
    And lets be honest the first code is always a mess.


    I'm thinking about this now for about a week and would like to get some feedback on what you think about unity+uml.

    Ideas,
    Nice-to-have tools,
    experiences with other modelling tools and Unity (MS Visio, Freemind, StarUML,....)
    or whatever comes to your mind.
     
    MaxFrax96 likes this.
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,692
    I think you'll find that, as with software development in general, some Unity developers follow formal software engineering practices and some don't. Game development attracts more hobbyists, so the balance skews more toward off-the-cuff hacking, but I'd wager that a lot of large studios and professional developers use decent engineering practices. There's nothing stopping you from doing UML with Visual Studio Ultimate, or using Unity's Test Tools (with a nice tutorial now), etc.
     
    angrypenguin likes this.
  3. codebeans

    codebeans

    Joined:
    Apr 7, 2014
    Posts:
    79
    "Game development attracts more hobbyists, so the balance skews more toward off-the-cuff hacking"

    That's what I mean. I imagine a toolset inside Unity to help you out with some guidlines.
    Patterns that fit well with Unity (Singleton, Observer, ...). No complete drag-and-drop scripting, but rather some good concepts . When to use arrays, when to use Lists, Dictionarys etc, good libraries, sample snippets for various use cases.

    Something that's kind of unity specific, the RequireComponent instead of just extending a class.
    Or a completely new diagramm type for Prefabs and their components.

    Of course you can use external software. But code generaters and patterns turned out to be useless for me, since it's all a little bit different inside unity.

    I think gathering those best practices and unity patterns might be worth to build in an asset that fits well within unity itself rather than some external software.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,692
    Clippy for Unity. ;)

    This actually sounds like a great idea. Maybe sort of an expert system or wizard that can handle the basic questions that come up frequently in the Scripting forum -- How do I create a singleton and when should I? How do I create a scriptable object to hold my game data? How do I create an observer? etc. And maybe even provide some template code, nicely formatted and with stub comments to get coders into the habit of documenting their code. This could be enormously useful to the hundreds of thousands of hobbyists for whom Unity is their first foray into programming.
     
    GradGuy908 and MaxFrax96 like this.
  5. codebeans

    codebeans

    Joined:
    Apr 7, 2014
    Posts:
    79
    That's what I am thinking of. Some starting point for Beginners and code management.
    Whenever I come up with a new functionality, rather then proper restructuring, I think "nah, just add another script"
    10 scripts later it's a mess.

    Do you know any good sources for beginner problems other than the unitywiki.
    Unitywiki has some nice code snippets and good practises. Anything more?
     
    MaxFrax96 likes this.
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,692
    The code's a bit dated on the wiki. You might just skim through the Scripting forum. A lot of really good, generous coders help out beginners, and just about every page has a gem in it somewhere.
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    What kind of new functionality are you talking about adding? If you need to re-structure to stop 10 bits of functionality from turning your project into a mess then I think you're taking a sub-optimal approach to begin with. If I had to go back to UML every time a customer requested a change I'd rarely get anything done.

    That doesn't mean you shouldn't design things, of course. It means you should factor a high amount of flexibility into that design.

    Out of curiosity, do you primarily stick to inheritance-based hierarchical design?
     
    Kiwasi likes this.
  8. codebeans

    codebeans

    Joined:
    Apr 7, 2014
    Posts:
    79
    For small prototypes/projects, I just code without any design first, to get things done quickly.

    But when it seems to get bigger. I at least outline the code structure (not not necessarily the whole implementation).

    And yes I try to use inheritance whenever possible to avoid writing my code multiple times and get a decent structure. It's just what I am used to do besides writing code in unity.

    How about you? I often end up with the problem to get a mix of composition and inheritance rather than a clean separation. (Is this even a problem?, idk)
    So basically:

    vs. , and I think unity prefers composition.



    maybe this was a bit exaggerating, what I meant was you start with this:



    which is nice ( e.g. prefabs are just a conglomaration of those loose coupled components which form a prototype),
    but you might end up with a lot of code for so little use. Dozens of loose coupled components with just a few lines of code in them.

    I don't know if this is a problem at all, since Unity devs have put a lot of heavy thinking into their product and I guess they are far more experienced than I am...

    Anyways what I miss is the possibility to structure my (code)-ideas and use basics of UML or a derivate that goes nicely with Unity.
    Maybe some prototype-code for beginners for general game types would help a lot of people, concepts that work well ( patterns, singleton as a great example) and a code generation tool for both ways (code <=> modelling)

    So far I found visual scripting tools which abstract programming as well and they are pretty successful,
    even though they cost ~90$.
    I would argue that there is a need for proper modelling as well.
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    Well, inheritance is just one tool in our arsenal. In the Apple example an Apple is a Fruit, so inheritance seems like a good default. An apple doesn't have a Fruit, so composition doesn't seem to fit.

    Composition and inheritance apply best do different types of situations. When something is a something else, that's inheritance. When something has something else, that's composition. Continuing from that, when designing with composition I find it's best to think about how different sets of components can represent my desired objects, rather than looking for concrete classes to do it. Applying that to fruit, rather than having Apple derived from Fruit, I'd probably consider something like a Fruit object which has a Skin component with properties representing that it's green and crisp, a Flesh component with properties representing that it's crunchy, a bunch of Seed components, a Stem component, etc. I could then use a different set of components with different properties to represent a Banana.

    Also, while good use of inheritance does indeed save you from re-writing some code, remember that its actual purpose is to provide a relationship between two types, so that should be the primary reason for using it.
     
    Kiwasi and codebeans like this.
  10. codebeans

    codebeans

    Joined:
    Apr 7, 2014
    Posts:
    79
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    I'm not sure why you see using UML (or any other software design tool) is any more or less applicable for Unity than it would be anywhere else.

    The presence of a nice scene editor and GameObject/Component model and some 3D rendering doesn't change the fact that we're developing software, and should approach our projects accordingly.
     
  12. codebeans

    codebeans

    Joined:
    Apr 7, 2014
    Posts:
    79
    The problems with existing tools I have are:

    While the concepts of patterns are the same, the implementation is different in unity.
    Imagine prefabs be generated from your model, given it does support constraints, such as ocl. test code generation ( using unity test tools)
    imagine a tool generate your "codeframe" ready for use ( with unity specifics: start,stop, awake, callbacks, ...)
    imagine for tutorials sharing models to show your point. Or having common implementation for games. (Towerdefense, board games, turn based games, ...)
    Imagine a tool to get you the concepts of lists, common algorithms, C# delegates, etc. From the very first beginning of writing code.
    Stuff you would otherwise get in contact with when your start to get professional.

    Simplifying writing games by giving standards and standard implementations to new users and proper modelling tools for experienced users to structure big projects without using third party software and being constrained to their implementations, not to mention their costs.
     
    Last edited: Apr 17, 2015
  13. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,692
    Look over Code Control, which is on sale on the Asset Store today. It's not exactly what you're describing, but it's along similar lines. Might be some good food for thought.
     
  14. Nyt0x

    Nyt0x

    Joined:
    Feb 3, 2014
    Posts:
    18
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I've only ever occasionally touched a UML. Then again, I fall squarely in the hobbyist bracket.

    That said, it seldom takes me rewriting ten scripts each time I make a change. Occasionally I decide the structure is completely wrong and refractor everything. But that's rare, and still generally only involves a single system at a time.

    My approach to an apple would be completely different. My code base would never (touch wood) include a class like Apple. Rather I look at what an apple does, and how it behaves. You'd likely end up with components for selectable, eatable, renderer, rigidbody, colliders, and probably a disable handler. Apple would only exist as the name of the prefab.

    To me considering what an object does and how it behaves, instead of what an object is, is best practice in Unity. This makes inheritance more limited in use too. Eatable might inherit from Usable. But that's about it.

    Keeping you systems seperate on each GameObject makes you code cleaner. Managing this interaction using interfaces and abstract base classes keeps everything loosely coupled. This approach does lead to five line classes and dozens of scripts on each GameObject. But that's not a problem, indeed, you could say that's a desired outcome.
     
  16. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    As to the actual tool I'm not sure what you are asking for. Sounds like a mind mapping + code generation + template + documentation + best practise guide. Making a tool that's all things too all people is a bad idea.
     
    angrypenguin likes this.
  17. codebeans

    codebeans

    Joined:
    Apr 7, 2014
    Posts:
    79
    Thanks for all that feedback.
    I took a look into CodeControl, and I like the messaging system.

    Now for the UML tool, I guess I am myself not 100% sure what I want to end up with.

    I didnt think about programming in Unity like this, but I see the advantage.

    I guess I will take some time read more about composition, mess around with custom editors and hack a few things together to get more clear about this.
     
  18. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Warning brain dump

    I find inheritance very annoying in Unity. Mainly because Unities serialization just doesn't support having a list containing multiple object types that inherit from the same base class. It just sees the base class. (I think ScriptableObject might behave differently though)

    That said, there is one thing that I really do find useful - GetComponent<BaseClass>(); Will return any components that are inherited from BaseClass. I often do this, for example I might have a BaseAI class - and have many different kinds of AI that do stuff differently I just do GetComponent<BaseAI>.AttackPlayer(); - As long as there is a component which has inherited from BaseAI this works like a dream.

    Unity does seem to favor composition an I can see why, inheritance when over used can lead to some hideous chains of classes with little functionality.

    It is a challenge to find a good compromise - but for me, in Unity I often use inheritance for:

    AI - BaseAI -> ArcherAI, MeleeAI etc.
    Character Motors (moving characters) CharacterMotorBase -> NaveAgentMotor, AStarMotor, RaycastMotor
     
  19. YesNoKonrad

    YesNoKonrad

    Joined:
    Feb 17, 2015
    Posts:
    4
    I think a good UML modeling tool already exists with NClass.

    For example, I started to flash out a movemensystem, which uses kind of a mix betweeen StateMachine- and Template- Pattern. This is a fast flashed out version, just to give you an idea:
    NClass.PNG


    The only thing which is missing is code generation specific to Unity, i.e. if the empty classes with their functions (which are still missing obviously) are generated, MovementSystem becomes a Monobehaviour and for all classes the necessary libraries are added at the top, namespaces, etc.

    Now the author of this a view month old post wants, that taken this program, you could klick on a created class and have a selection of the default methods from unity, assuming that it inherits from Monobehaviour, like Update(), FixedUpdate() and especially OnCollisionEnter(Collider col) and OnCollisionEnter2D(Collision col)... or was it the other way round. I NEVER can remember. It would be nice to have this, because no editor actually has codecompletion for methods, but only for methodcalls etc.

    Now you maybe can see, why such a tool would really help you out.
     
    codebeans likes this.
  20. codebeans

    codebeans

    Joined:
    Apr 7, 2014
    Posts:
    79
    In addition to what @YesNoKonrad said I would like to see code generation on patterns that were somewhat adapted for programming in Unity. People shouldn't write a object pooling script or singleton or observer pattern over and over again. I'd rather like to see template classes for these common features in a drag and drop UI.
    Just like nobody writes a list class by himself...
    Why not extend this on features that are written over and over again.

    I do know that these scripts are available in the Unity wiki, but still. It should be at one place right in Unity.
    Editor extension maybe.
     
  21. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you want it you can write it. For just framework stuff there are things like @lordofduct's space puppy floating around.