How to design your code architecture in unity?
来源:互联网 发布:windows运行放在哪里 编辑:程序博客网 时间:2024/05/19 23:15
I've not been programming Unity long, though I've run into many of the same frustrations. It seems like the Unity library is constructed to facilitate a lot of cowboy coding practices, with little or no real encapsulation. The tutorials also tend to have everything as a MonoBehavior or ScriptableObject, where it is often entirely possible to sequester code into a standard C# object without involving UnityEngine, which can help to keep things cleaner.
It seems like the tutorials also use SendMessage() where it isn't really needed - they could have precached a reference to the object/component they're trying to interact with, and use that directly. And it seems like they pretty much never use packages at all, everything shares one big happy namespace.
I understand the general mindset seems to be "Unity for Dummies", making it seem as straightforward as possible to write something that will operate correctly, with no concern for code quality, reusability, maintainability, testability, etc. If you follow the tutorials, and lean on the UnityEngine classes, you'll wind up with a tangled mess of code before you know it. Keeping code clean and well-organized is a struggle you have to put more effort into with Unity than you might with other platforms/frameworks.
I have found a few things to be very helpful:
- Minimize your MonoBehaviors. If you can make it a plain C# class, do that. If you can encapsulate functionality into a plain class and reference that from your MonoBehavior, do that. Try to avoid using UnityEngine in your plain C# classes when you can.
- Singleton MonoBehaviors can be achieved with a static singleton reference and a check in your Start method: if the static singleton reference is null, set to self, if it's not null, throw an Exception. The only way this would happen is if you have more than one instance in your Scene, so an exception will tell you (the developer) that you've done something you shouldn't have.
- When planning your architecture, try to decide ahead of time what you want to be editable in the Unity inspector. Those fields will need to be part of a MonoBehavior or ScriptableObject. Everything else is fair game to place a MB/SO or into a plain C# class.
- Likewise, any class that has to run code during a *Update or On* call should generally go into a MB, though you can also define this code in a normal class and call it from MB. The thing to note here is, if you end up having to reference Unity functionality like GameObject from your code anyway, there's less point in encapsulating it in a normal C# class.
- Subclassing/inheritance seem to give Unity a migraine - or, at least, using them with Unity gives me a migraine. Any time I try to work with an object hierarchy that I've created with MonoBehavior or ScriptableObject at the top, I wind up wanting to shoot myself. But that may just be due to lack of experience with Unity and/or C#.
I would agree that, in general, the tutorials seem to just cram code in wherever it's most convenient for expository purposes, because nobody has to maintain that tutorial code later. It's also hard to provide general guidelines as every game is different; for example, my current project encapsulates a lot of input handling into it's HUD class, but that might not be appropriate for your game; you might want to encapsulate it into a Controller class or some such. A point-and-click UI will be organized in code very differently compared to a WASD-style shooter, as a matter of course.
- How to design your code architecture in unity?
- How to design your model
- how to install your kernel code in FC6
- How to use another indicator in your code?
- How to design your first programs
- how to manually traverse the ZODB tree in your code to locate objects by their path?
- 10 Tips to improve your Software Architecture Design
- 二 How to encryption your code
- how to debug in unity
- How to Success in Your Company
- How to Backup Your System in linux
- How To Invest In Your Time
- How to use Log4net in your project
- How to reset your password in Ubuntu
- KB: Sample Code - How to add video to your UI
- How to design your own extension-point for Eclipse
- How to: Create a Binding in Code
- How to play XM Music From your own code
- [java]关于java正则
- offsetof字段偏移惯用法
- 优秀博客
- Unity3d json 数据库
- 赢在数据<R+Hadoop>群电子刊物《数据赢家》第二期出炉
- How to design your code architecture in unity?
- 手机支付宝密码存储机制分析
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL原因和解决方案
- ubuntu 安装sourceinsight
- Windows phone开发入门:技术资料索引库
- unity3d 调用加密狗的dll
- 虚拟串口工具Virtual Serial Port Driver(VSPD)
- 快捷键
- 缩短eclipse的启动时间的JVM优化