Tuesday, October 31, 2017

Different ways of enabling mouse click as event trigger in Unity via scripting

For a 2D object/sprite:
Option 1:
1. In the script file, include using UnityEngine.EventSystems; at the top.
2. Implement the PointerClick interface 
3. Attach the script to the 2D sprite and make sure that a 2D collider (e..g box collider 2D) is attached to it.
4. Select the camera and attach Physics 2D raycaster to it.

using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;

public class clickTester : MonoBehaviour, IPointerClickHandler  
 {  
      public void OnPointerClick (PointerEventData eventData)  
      {  
           //do something here, e.g. change colour of sprite.  
           GetComponent<SpriteRenderer>().color = new Color(1f,0.3f, 0.30196078f);  
           print ("Pointer click");  
      }  
 }  

Source: https://stackoverflow.com/questions/37452457/unity-3d-enabling-mouse-pointer-as-a-event-triggers-pointer-enter-exit/37456107#37456107


Option 2:
1. Ensure the 2D sprites have a 2D collider attached to them.
2. Attach a script file to the camera with the code below.
 using System.Collections;  
 using UnityEngine;  

 public class clickTester: MonoBehaviour {  
   void Update()  
   {  
     if (Input.GetMouseButtonDown(0)) //This is left mouse button click 
     {  
       //Converting Mouse Pos to 2D (vector2) World Pos  
       Vector2 rayPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);  
       RaycastHit2D hit = Physics2D.Raycast(rayPos, Vector2.zero, 0f);  
       if (hit)  
       {  
       //do something here, e.g. change colour of sprite.   
       GetComponent<SpriteRenderer>().color = new Color(1f,0.3f, 0.30196078f);   
       print ("Pointer click");   
       }  
     }  
   }  
 }  




For a UI button:
1. In the script file, include using UnityEngine.UI; at the top.
2. Make a public void method and give it a name
3. In the button, add an Event Trigger component -> Add New Event Type -> Pointer Click.
4. Drag the gameobject the script is attached to into the pointer click.
5. Choose the public void method created.
 using System.Collections;  
 using System.Collections.Generic;  
 using UnityEngine;  
 using UnityEngine.UI;  

 public class button : MonoBehaviour  
 {  
      public void changeColour(){  
           //Do something here, e.g. change colour of button.  
           GetComponent<Image>().color = Color.green;  
           print ("testing changeColour");  
      }  
 }  

Monday, October 16, 2017

Using Git

- Install Git from https://git-scm.com/download/win
- Navigate to the folder where you want to create the repository and type:
git init

- Set your name and address:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

- Set your text editor. E.g. If your editor is sublime text, type:
git config --global core.editor "'C:/Program Files/Sublime Text/sublime_text.exe' -multiInst -nosession"

Common commands:

git status
Shows what files have changed since the last commit.

git log
Shows commit log

git add .
Adds all modified files to staging area

git commit
Commit/records changes to the repository. This will open up an associated editor (e.g. notepad, sublime text, etc).

git checkout -b [name_of_new_branch]
Create a new branch

git branch -d [name_of_branch]
Delete a branch

git ls-tree -r master --name-only
Shows a list of all files currently being tracked under the branch master.

git ls-tree -r HEAD --name-only
Shows a list of all files currently being tracked under the current branch.

git add .
git stash
git checkout master
This is to switch back to the head if you have checked out an earlier commit.

Other:
To ignore directories, i.e. directories you don't want to commit, edit the .git/info/exclude file and add the directory you want to ignore, e.g. if the folder you want to ignore is called Temp, add
Temp/ to the exclude file.

Sunday, October 15, 2017

Different ways of disabling game object/component in Unity via scripting

Example: HingeJoint2D. When the spacebar is pressed, the object/component is disabled.

1. To disable the entire game object, you can use Destroy. Make sure to tag the appropriate game object.
      private GameObject joint;  
   
      void Start(){  
           joint = GameObject.FindWithTag("joint");  
      }  
   
      void Update() {  
           if (Input.GetKeyDown ("space")) {  
                Destroy(joint);
           }  
      }  

2. To deactivate the game object, use SetActive(false)
      private GameObject joint;  
   
      void Start(){  
           joint = GameObject.FindWithTag("joint");  
      }  
   
      void Update() {  
           if (Input.GetKeyDown ("space")) {  
                joint.SetActive(false);
           }  
      }  

3. Use GetComponent to disable only the HingeJoint2D component, and not the entire game object.
      private HingeJoint2D hinge;  
   
      void Start(){  
           hinge = gameObject.GetComponent(typeof(HingeJoint2D)) as HingeJoint2D;  
      }  
   
      void Update() {  
           if (Input.GetKeyDown ("space")) {  
                hinge.enabled=false;  
           }  
      }