Unity Editor Hello, Unity Dev’s I am back again with Part 3 of Creating custom editor window in unity. In today’s blog we will look around: “How to load Textures files from resource folder and create our buttons.” Step1. Gather some images related to the headers of our sub-toolbars or if you are good at photoshop create some, btw I have zero skills when it comes to photoshop so I am goanna gather images from internet. Once we have are images ready drag them into respected folder under root folder Resources. It will look something like this: Step2. Scripting Now we need to create a function which will be called once whenever we will switch from sub-tabs. Hmmmm, so how to do that!!! First create a function and name it as per your want’s I have named mine as: “MakeWindow” Then write down these lines: Create two variables a list: to store our loaded texture...
One can create a game in many ways the way used in
this blog is not absolute.
The purpose of this blog is to let the users
understand unity and coding a bit better than before.
Step1: Images needed for the project.
We will be needing 4 images to create this game.
1-Cross
2-Circle
3-Board
4-Background
Step2: Setting up the Scene.
Set the game view to 2d.
1. MainCamera- Select the main camera and under Camera component it has a property named Projection set it to Orthographic.
2. GameOver- Create an empty gameobject and add sprite renderer to it. Add a sprite image to it by clicking on sprite property. Set the Color to black or any other color as per your preference.
Import the image by simply dragging the script from explorer to unity under project panel. Select the image and change it texture type to sprite 2D and UI.
3. BackGround- Used to create the black background at the back.
4. Create
an empty gameobject and name it Board, Add a sprite renderer to it by simply
clicking on add component. Create an image in any photo editor software like
this:-
5. Now
create an empty gameobject under board gameobject and add box collider and a
spirit renderer to it. After that make 8 more copies of its (Ctrl+D).Name those
in descending order starting from 9 to 1.
Now arrange them accordingly as shown
below.
6. Create
a canvas and inside a canvas create a text as right click UI>Text.
7. Create
a UI Button same as created the text. Name the Button as New game and the text
under button as BttnTxt. Naming them correct is important as
we are going to use them in scripts.
8.Arrange
all the items And at the end your Scene will look like this:
Step3: Writing the script.
Step3.1:-Onclick on a box the spirit of
zero or cross must be assigned to that box.
To do that first we have to know the
box we hit and then change its spirit only once so that once a box spirit is
assigned to zero/cross should not change on second click on it.
Select all the box collider under
board and add a tag on them naming “Boxes”.
Now write the following script-
In this script first we will create a
list which will find all the boxes in board and add them to the list. Secondly,
we want an array to store are 2 cross/zero spirit.
Now in start function the script will
find all the gameobjects with tag “Boxes” and will store them into the list.
In update function firstly we check
if a user clicks the let mouse button a ray from the main camera is shooted
towards mouse position and if the mouse is on any box the ray hits the
collider.
If the hit.collider is not null than
boxcollider it hits was store in a local variable box and is disabled to ensure
that user cannot click on same box twice.
Secondly the spirit is assigned to
the gameobject.
Right now it is only adding zero in
all boxes.
Step 3.2: Making zero/Cross appear
alternatively.
To do this I have used a Boolean
variable to make it false/true.
On true zero will appear and on false
cross will appear.
Script-
Step3.3: Checking the patterns to generate the result who
wins or is match a draw.
This can be done in several ways.
Either we can check the spirit names or can change the
gameobject names or can change the name of gameobjects stored in our list.
I will do this by checking are list as it will save
the extra coding required to call all the spirits.
First onclick on gameobject we have to change its name
as well as update our list.
Secondly we have to check that all the Boxes stored in
our list are in order.
Script-
Step3.4: Check for the pattern and activating our
gameover gameobject showing text who wins and new game button to refresh/start
a new game.
Checking a pattern is quite easy all we have to do is
read the gameobject names and if gameobject names matches horizontally,
vertically or diagonally the user wins.
Script-
Now some additional thing added in code are the
newbttn variable to assign new button, gameover variable to assign gameover
gameobject .You have to also add UI library to your script so we can use text
properties.
Assign gameover and new button to your script in the
inspector.Play the game and we are done with the Tic Tac Toe
game.
Only thing left is to refresh the game when click on
new button.
Step4; Making refresh script to refresh all things
changed in game to start state in play mode.
Create another script name refresh and attach to
board.
Than select your NewGameBttn and
add the script to its onclick event.
Script-
At last add a line in your zeroKhata Script.
At each place where we are checking the pattern.
Thanks for the support till now. The post is open to
suggestion and edit. Do comments your opinions.
Till than Happy Coding.
Having the code as text for copy pasting would generally be pretty helpful. Besides that the code seems way too long for what it does. Some simple things like
ReplyDeletehit.collider.gameObject.GetComponent().sprite = CircleCross[Turn ? 1 : 0];
would make things a bit more concise in my opinion, but that's a matter of taste.
What's not a matter of taste is the GameOver method which has way too much copy pasting and I don't consider it a good example of how to do things.
First, create a method:
bool Win(string name);
And call this as:
if (Win("Cross"))
{
// Show X wins
}
else if (Win("Circle"))
{
// Show O wins
}
// Handle draw
This reuses the code for each side.
Then, add:
bool Line(string name, int index0, int index1, int index2)
{
if (Boxes[index0].gameObject.name != name) return false;
if (Boxes[index1].gameObject.name != name) return false;
if (Boxes[index2].gameObject.name != name) return false;
return true;
}
You can now define Win as:
bool Win(string name)
{
if (Line(name, 0, 1, 2)) return true;
if (Line(name, 3, 4, 5)) return true;
if (Line(name, 6, 7, 8)) return true;
if (Line(name, 0, 3, 6)) return true;
if (Line(name, 1, 4, 7)) return true;
if (Line(name, 2, 5, 8)) return true;
if (Line(name, 0, 4, 8)) return true;
if (Line(name, 2, 4, 6)) return true;
return false;
}
Surly try that looks more tidy than my way.
DeleteThis comment has been removed by a blog administrator.
ReplyDelete