management of tools

management of tools

……

……

When playing games.

Based on the object-oriented principle, the responsibility of the class should be simplified as much as possible.

Therefore, many tools will be stripped out naturally.

These tool classes do not need to be hung on the required game objects, but wait for other classes to be called.

original.

I was wondering if I could manage tool classes like the factory pattern. 

But after trying several times, I had no choice but to give up.

The factory pattern is only suitable for creating objects, not for managing tools.

There is no way, out of bad policy, it can only be done like this.

Create a folder Tools separately, and then put all the tool classes in it.

Just open a tool class and take a look.

 

As you can see, in this tool class, a static method is encapsulated.

In fact, in each tool class, I have encapsulated one or more static methods, which is very convenient to call in other classes. (You can call it directly with the class name without creating an object)

but.

When I first started using static methods to call, I encountered a problem.

In static classes, only static variables can be used.

Explain here, why is this a hassle?

In the image above, I need Instantiate to generate the prefab.

It is of course necessary to inherit MonoBehaviour to generate Instantiate.

But here comes the problem, I want to drag the prefab directly into the variable in unity, as shown in the figure below.

 

This is where the problem arises.

Static static variables cannot be displayed in unity, because static variables belong to classes, so objects created in unity cannot be displayed.

Then I can only use a tricky way.

As shown below.

 

In the code, I first create a public, so that I can drag and drop it to the prefab in unity.

Then I also created a static s_prefab. In Start, I assigned the public drag-and-drop reference to the static s_prefab. In this way, the drag-and-drop prefab can be used to implement the static method.

and.

Note the third image from the top.

In order to enable the tool class to implement the drag and drop function.

So create an empty object specially used to hold tools - tool management class.

Thus.

Management tools are more convenient.

Of course, this is just my current thinking, and there may be many flaws in this management method, but it is enough for the small game I am currently making.

Guess you like

Origin blog.csdn.net/oyqho/article/details/129942075