Skip to main content

Unity Editor Window (Part 3)

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 textures an

Special Blog- Tic Tac Toe Game in Unity.



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.

 And we are done.
Thanks for the support till now. The post is open to suggestion and edit. Do comments your opinions.
Till than Happy Coding.

Comments

  1. 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
    hit.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;
    }

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

Creating Gamobject, Adding Components and Removing Components using script.

Today we are going to learn about how we can create a gameobject add components to it or remove components from it using script. Open Unity >>Create new project>>create new scene. 1.Creating Gameobject using script Step1.In your scene hierarchy add empty gameobject and name it ScriptManager. Step2. Create a script name it as per your wants, and attach the script to ScriptManager gameobject. Step3. Write the following code:- This will create a gameobject in the hierarchy with the name specified In ” ”. In our case it’s a cube. As you can see the above script creates a gameobject and add transform component when the object is created as all gameobject have transform component attached to it from the start (e.g. Empty gameobject). In next few lines we added a box collider to our object cube, a mesh renderer and a mesh filter. Save the script and run the project. You will see a gameobject is cr

Unity Editor Window

Editor Window (Part 1) In unity, Editor windows are windows which are created by developers to be used by developers in Editor, it’s more like a plugin for Unity editor made by You. By learning Editor Window scripting, one is able to create any type of windows to ease their work while working in editor. Official Definition: “ Create your own custom editor window that can float free or be docked as a tab, just like the native windows in the Unity interface. ” I am going to start a series which will cover almost all the topics related to EDITOR WINDOW. In this series I am going to make a “LevelDesigner” plugin dittoing the zoo tycoon GUI and functionalities. Fortunately, we will learn about: Creating Editor window, creating all types of GUI, Modifying GUI Skin, Instantiating prefabs loaded from resources, and lots more…. So, tag along with me till the end of this series and let’s see How far we can Go. I have also just started learning Unity Editor Scripting so

Graphic Raycaster in Unity

Ever wonder how one can click on GUI elements without using any event script on GUI components. On button click, On value change are some event driven functions of GUI components but what if I want to perform any action without using these event driven function. Well there are plenty of ways of doing it but the one I am going to discuss today is GRAPHIC RAYCASTER. Scene Setup Step1. Create two images, add any sprite to it and change their name to specify them later on. (For this tutorial purpose let’s assume penguin and polar bear) Put the images adjacent to each other. Script Step2. Create a script and attach it to your canvas. Write down the following code. Make sure to call all the libraries. Next is our global variables Graphical raycaster is used to call the raycaster attach to your canvas, Eventsystem is also used to call the event system of canvas. PointereventData is used to kno