Disclaimer!
This program is not intended for JavaScript developers.
We teach real programming languages…
Not work miracles.
Salutations!
I'm Jesco, you're watching Game Dev Made Easy and the video for today is a Unity specific
video that will be about creating a multi monitor display for a single game.
What I mean by this is take a look at a Nintendo DS, the old Punch Out Arcade cabinet or the
Nintendo 3DS for how a single game can utilize multiple monitors to display information on
one screen and the actual game play on another.
This is actually a recreation of an old video with a much better PC setup and more in-depth
information.
A viewer of mine actually asked me about the video and this is what spawned the idea to
revisit that old video and give a better example of what is happening.
Before we get into it, we first need to understand exactly what we will be doing and how to go
about implementing it.
Multi-Monitor displays refer to having more than one monitor being displayed to simultaneously
while being able to directly control input on one or both monitors.
Generally, in games that incorporate the usage of multi-display monitor modes, what happens
is useful information is displayed in either the top or the bottom monitor, generally the
bottom and the actual gameplay occurs in the opposite monitor, generally the top monitor.
Now, With PCs, the configuration varies greatly.
Some people might have a setup that has a stacked monitor setup and others will have
a configuration where monitors or side by side.
And this is just taking a dual monitor configuration into account.
With all of this out of the way, let's queue the intro animation and music!
I have already created an empty project as we can see here and Unity already has everything
set up for a sample scene.
We will go ahead and expand upon this to build this project.
Now, the first thing we need to do is create a secondary camera.
We need to change the camera mode to be orthographic and set the display to be display 2.
The reason for this is we want this display to only show up on a secondary screen and
not the main display and be drawn over by the main camera.
The next item on the list is to change the camera's position to be absurdly out of
range of anything that could be on the screen at any given time.
The x position will be 25439, the y position will be 16713 and the z position will be -5.45.
This is so no items or effects will bleed over into the secondary screen unless you
want it to.
Next up, we want to create a canvas and for it to be a child of the secondary camera.
This is purely for organizational reasons.
The canvas should be set to render mode Screen Space Camera and make sure the Render Camera
is the secondary camera.
We don't want this to be drawn on the main camera at all.
Create a panel to be a child of the Canvas, this is so we have the option to make the
background color to be different from the main camera's render view.
We can change the color if we want by changing the Panel's Color to being a solid value
for whatever you want.
Next up, create four buttons and set them to be a child of the panel.
The text of each button should be Drop Cylinders, Drop Capsules, Drop Cubes and Drop Spheres
respectively.
This is just to organize and be able to know what each button is supposed to do in this
scenario.
The next step is to create a prefabs folder.
In the Hierarchy Pane, create a cube, capsule, cylinder and sphere.
Make sure all of them have the rigidbody component attached to them.
We want gravity to affect them.
This will be important later.
Drag and drop the cube, capsule, cylinder and sphere into the prefabs folder and delete
them from the hierarchy pane.
Let's create our scripts folder next.
The three scripts we will create will be Destroy by time, drop scripts and Multi display code.
We are going to utilize all of these very soon and as you can see, each of them have
been named according to what their purpose should be.
Drag and drop the Multi Display Code script onto the Main camera.
We could create an empty game object to be our manager item, but we don't really need
it for something like this.
On the Secondary Camera, drag and drop the Drop scripts onto it.
The last things we need to do in the editor for now is to do is go into our prefabs folder
and left click on the cube we created click on add component, highlight scripts and select
the destroy by time script to add the script to the prefab.
We will do this for the capsule, cylinder and sphere as well.
I forgot to add this to the script (because I'm a dumbass), but you also need to add
a rigidbody component to the Sphere, Cube, Cylinder and Capsule prefabs.
Click on add component, select Physics ( not physics 2D) and choose Rigidbody.
Now double click on the multi display code script to open it up in Visual Studio.
(Transition) Delete the comments and the update function
from the multi display code script.
We only need the start method, so we don't really need to have the superfluous code and
comments remaining there.
We will create a for loop within the start method.
For int I is set to zero.
I is less than display dot displays dot length.
I plus plus.
This for loop will loop through all of the active displays that are available on the
system.
Write, display dot displays brackets I dot activate.
This will make sure the displays we have available are now activated if they are needed.
That is all we need to do in the multi display code script, let's move over to the Destroy
by time script to get that out of the way.
Delete the comments and update function from this class as well.
Again, redundant comments and code isn't needed here.
Above the start method write public float life time is set to one point five float.
This will designate how long we want the objects to stay alive in the hierarchy pane for in
game.
Inside of the start method write, destroy parenthesis gameobject comma lifetime.
This will remove the gameobject by the time we specified.
That completes the Destroy by time script.
Now we can move on to the final script which is the Drop script.
Delete the start method, comments and update method.
None of this will be used at all.
The first thing we need to do is to create a public list of gameobjects called drop items.
This will house the sphere, cylinder, capsule and cube from within the editor.
Write private void drop item with the parameters set to be string name.
We are creating this method to do all of the legwork but allow us to call it in public
methods with the name of the item we want to use.
To put it bluntly, it follows the DRY and KISS principles which is Don't Repeat Yourself
and Keep It Simple Stupid.
Next up, create a var called xMinMax and it will be set to Random dot Range with the parameters
being negative ten and positive ten.
This will set the horizontal axis to have a specific range that the items will be able
to spawn in.
Vector3 spawn Position is the next thing to write and that will be set to a new vector3
value of xMinMax, ten and zero float.
This will be the vector position we will utilize for the spawned game objects.
Quaternion spawn rotation is set to quaternion dot identity.
This is for the rotation of our spawned game objects to be set to the identity of the game
objects.
Which just means that the rotation is set to 1.
Next on the list is to create a foreach loop.
Foreach var item in drop items is what we will write.
We are iterating through all of the game objects within the list we created.
Inside of the foreach loop, we will create an if statement.
If item dot name is equal to name.
We want to single out a specific item from within that list, which will be evident why
in the steps following this method.
Inside of that if statement, write.
For int I is set to zero.
If I is less than one hundred.
I plus plus.
We want to create one hundred of the item in question, but you can change the value
to be whatever you want it to be.
Inside of the for loop, write.
Instantiate item comma spawn position comma spawn rotation.
And now we instantiate the items to actually be called in game.
This completes the drop item method.
This method handles the creation and selection backend code that we need to have game objects
spawned in the game.
Now we will create our next method.
Public void Drop Sphere.
This will be called by the button press of the Drop Sphere button.
Inside of this method write, Drop Item with the string value of Sphere being the parameter.
Next up, we will create a public void Drop Cylinder method.
And as you guessed, it will be called by the drop cylinder button.
Write Drop Item with the string value of Cylinder as the parameter.
The third one is public void Drop Cube.
Inside write Drop Item with the string parameter being Cube.
And the final one is public void Drop Capsule.
Inside write Drop Item with the string parameter being Capsule.
You can now see exactly why we had the drop item method have the string name parameter
and the check for the item's name being equal to name.
This drastically reduces the amount of code we would have to write and allows for us to
make modifications for more items or changes in the items in the future.
This is something you should always think about when writing any code.
If you find yourself repeating something, there is smell in your code and you should
rectify that as quickly as possible.
This is a problem that many beginners and a problem that programmers that don't think
about proper coding conventions when writing code.
Look at it from this perspective if you aren't convinced.
The Drop Item method would have about 7 lines of code without the foreach loop or the if
check.
This method would have to be written 3 more times along with the list becoming individual
game objects.
In the GitHub repo that will be linked, I will have a script called Bad Way, which will
showcase the difference.
You will see the same thing being repeated and the total length of 59 lines of code versus
the Drop Script which has 46 lines of code.
Anyways, let's save the scripts and jump back to the Unity Editor.
Click on the secondary Camera and you will see Drop Items in the drop script, the current
size is set to zero, let's change that size to be four.
Drag and drop the prefabs of the capsule, cube, cylinder and sphere into the list.
This will tie the prefabs to the list and not make the values null.
Click on the first button, click on the plus sign under the On Click option.
Drag the Secondary camera onto it.
Click on the functions option, locate Drop Scripts and select Drop Cube.
This will make it so that the drop cubes button will drop the cubes.
Click on the second button, do the same thing only instead of selecting Drop Cube, select
drop sphere.
Click on the third button, do the same steps and select drop sphere.
Click on the fourth button, do the same steps and select drop Capsule.
Click on file and save the scene.
Now for the final step.
Click on file, build settings.
I am building for PC, Mac and Linux Standalone for this example.
Select build.
Create a folder called Build and select that folder.
Unity will build the project.
Once it has finished building, it will open the folder that houses the build files.
Now double click on the exe file for the project and select display 2 when the dialogue box
comes up.
You can change the display size to be whatever you want it to be, and then click to run.
We now have a working example of a multi monitor display enabled Unity game.
This has been Jesco with an update to an older video.
If you like this version over the older one, give this video a thumbs up and share it with
your friends.
I want to give a warm shout out to my patreon supporters whom have chosen to give me some
of their hard earned money for the content I produce.
It means a lot and inspires me to continue to deliver content on YouTube since I don't
have the four thousand watch hours and the one thousand subscriber count to monetize
my videos.
Không có nhận xét nào:
Đăng nhận xét