I have multiple GameObjects that need a class (inherited from MonoBehaviour) added to them as component.
Instead of doing:
{
MyClass myclass = go.Addcomponent<MyClass>();
myclass.value = 10;
}```
Can i create and edit the class, *before* copying it to the GameObjects, like this:
```MyClass myclass = new MyClass();
myclass.value = 10;
foreach(GameObject go in list)
{
add a copy of myclass to "go" as a component here
}
No, you can’t instantiate MonoBehaviours with new. Can you not use some Initialize function instead? Or use prefabs instead of relying on AddComponent.
While it’s not possible out of the gate, it can be achieved by creating a thin wrapper component to hold the instance.
This technique can be used to implement the flyweight pattern, to make unit testing the wrapped class faster and easier, or to make it possible to use the functionality the class provides with or without attaching it to a GameObject first.
Right, a wrapper is always an option that hides / seperates the gameobject and engine interactions from the actual game logic that the class should represent. It reminds me of Battlestar Galactica Online which was an MMO that was made with Unity. Unfortunately the servers has been shut down in 2019. Some polish fans have actually partially created a replacement server with only the fundamental mechanics up and running. They got permission to do that as long as they stay non-profit.
Anyways, back then I looked through the code and they actually went fully OOP. Literally everything is pure C# completely disconnected from Unity’s gameobject and behaviour stuff. They just have a couple of components which serve as a mediator layer between their game state model and Unity. They loaded a lot of data dynamically at runtime and the few MonoBehaviours just had a single field of an interface type. They have a few components that did event management (so passing around collision events or such things). It was really impressive and a huge code base. Though I’m sure they actually had unit tests for most of their stuff.
Unfortunately BigPoint no longer offers any official download as far as I know. The group that runs the replacement server do have a msi package which seems to contain the original client, though that installer is not signed, so there’s a bit of a risk. You get the download link from their discord server. The starting point would be this video. The actual msi installer can be found here:
https://ftp.bsgo.fun/downloads/SetupLauncher.msi
I specifically did not include the link as a actual link since as I said, I can not verify the validity of the installer.
Though I still have the client installed on one of my old harddrives. This (hosted on a free hoster) is a direct zip of the full client folder. I didn’t zip the launcher and patcher as they may contain account data. The client is just the Unity application. I do not share decompiled code of copyrighted compiled code. Since the client was available for free, I think it’s not a problem when I share it. You can use ILSpy to look into the various assemblies in the “managed” folder. Specifically the “Assembly-CSharp.dll”. Though there’s also the “Bigpoint.Core.Mvc.dll” which seems to be some sort of in house MVC framework which is used to handle network messages.