Aryan Bhasin
  • Blog
  • IB Resources
  • Standardised Testing
    • ACT Resources
    • SAT Resources

THE THOUGHT CATALOGUE

An organised entropy of ideas

Accessing serial ports in Unity

5/31/2017

0 Comments

 
The .NET framework offers a way to access and use Serial Ports connected to your system. This can be done by importing System.IO.Ports​

    
After importing, you can access devices connected to Virtual Serial Ports and use them to retrieve or send data. However, using the current framework in Unity will throw an error of not recognising System.IO.Ports. This is because Unity operates on the .NET 2.0 subset framework on default 
In order to fix this, you need to change settings to .NET 2.0
Click on Edit in the menu toolbar. Head over to Project Settings, and select Player. On the inspector window, click on Other Settings. Scroll Down until you find the Api Compatibility Level under Configuration. Change this from subset to the .NET 2.0 framework.
Now, you need to define your serial port and give arguments in the Start() function, and access data in the Update() function. Declare your serial port outside the functions using the command 'SerialPort <name>;'  
This code sample defines a port named sampleport using the SerialPort() function. It defines the properties of PortName and BaudRate for the gyroscope. The Open() command opens the virtual serial port for use. You can also close the port with Close().

    
The final step involves using the Update() function to continuously read the data from the gyroscope. The code below reads the data into a String called read-data, and prints it to the console. You can split the output, or print them all line-by-line using the for-each loop.

    
 You can now access the gyroscope's data in Unity.
0 Comments

Reduce delays in serial port communication in Unity

3/5/2017

2 Comments

 
Some operations in Unity, placed in the Update() function, can be computationally expensive, and the frame-rate of the function might be lower than you need for an operation, leading to a lag time, or a latency, for the operation. An example would be connecting Arduino with Unity, and noticing a delay during the reading and printing of data to the console. This is due to operations that require a higher frame-rate than the update function is set at, such as the ReadLine operation for an Arduino. To solve this, you can initialise a thread in the Start() function, run your operation in another function, and simply print the computed output in Update(). To start off, you will need to import the threading library and declare the function where your operation will occur:
Code Editor

    
Next, you will need to start off a new thread in the Start() function, and reference sampleFunction() so the thread knows which function to execute:

    
Finally, you can use the output from the ReadLine operation example above in Update(), and perform any other normal operations too:

    
This drastically reduces the lag time delay that would otherwise have happened had you executed ReadLine() or any other expensive operation in Update(). Remember to reference the alternate function you declared in the ThreadStart parameters [new ThreadStart(function)]. You can also close the thread any time you want using sampleThread.Close()
For additional information, use this link
2 Comments

[C#] Saving array of numbers as text file

11/17/2016

0 Comments

 
The procedure below will show how to convert an array of numbers to a text file that is saved in a specified location. For the sake of depth, I will use a stack of numbers, converted to an array and then saved. 
The starter code below is for declaring a stack and saving some random values.
Code Editor

    
The command below converts the stack to an array, and then converts the array of objects to an array of string using Select(). This final list is stored as an array of string.

    
Finally, you can store this array of numbers in a text file, located on the Desktop:
Code Editor

    
0 Comments

Unity Video Player Scripting

11/3/2016

0 Comments

 
Picture
The new VideoPlayer feature in Unity 5.6 allows you to import and play videos without converting them to another format and attaching them as textures to an object (although you can still attach the videos to objects). You can add a videoplayer by going to GameObject --> Video --> Video Player.
This adds a VideoPlayer component to an empty object, as shown to the left. You can attach a video clip, add audio sources, change playback speed, and even add mesh renderers to play videos on curves and meshes.

Using scripts to manipulate videos is easier with the introduction of the Video Player feature. First of all, you need to import the UnityEngine.Video library, and then declare the GameObject which has the video, and its Video Player component itself:

    
Next, in your Start() function, you need to assign the two variables you declared above to the GameObject and its Video Player component.

    
You can now easily manipulate the video by accessing the class and methods of videofile. Here are some examples of commands:

    
You can learn more about the Video Player and its functions and methods here
0 Comments

Scripting Start and Exit menus in Unity [C#]

9/25/2016

0 Comments

 
This post notes the code you need to write to create your Start and Exit scenes for your game. The concept is simple. You'll need to attach buttons for every action, and link them via script. When clicked, the buttons need to open different scenes. For the Quit button, you need to quit the application.
To start, create both scenes and add your buttons. The image below is a very basic example of an exit scene with two buttons placed together in a panel (UI).
Picture
To write your exit script, create an empty object and attach a new script to it, your quitting script. For this script, remove Start() and Update() functions, and add your own function to quit the application:

    
Picture
Inside your function, use the Application.Quit() method, which will close the game.
Click on your button in Scene mode, and in the OnClick section, click on +  and choose the empty object earlier having the script attached. Next to it, choose your exit script and your exit function. The OnClick section should look like the image shown to the left, with the exit.QuitGame function selected.

Do the same thing for your start scene. For the start scene, you need to use the SceneManager.LoadScene() method to change scenes. You need to import the UnityEngine.SceneManagement library. As before, remove all functions, and add your own function. In this case, it is LevelManager(name), with name being an argument for the scene you want to switch to. 

    
Picture
Create a new object in the Start scene and attach this script to it. Again, add a reference to this function in the OnClick section of your button. In the text box appearing below your chosen function, enter the name of your scene. An example is shown to the left. In the text box, the word "Start" refers to a scene named "Start". Upon clicking the button, Unity will take you to this scene.

0 Comments

Custom Domain for github pages

9/21/2016

0 Comments

 
This post is meant for all those who have a Github repository containing the code for a website, and have purchased a domain name via GoDaddy, but not the hosting, and want to use the hosting of Github Pages to serve their website. Or in other words, host your website for free using Github Pages.

The post shows how to link your Github Project Repository to a domain you purchased on GoDaddy. Essentially, this means that if you visit your GoDaddy domain, you will see the code of the Github Repository as shown on its gh-pages site, but with your own domain. 
Although the demonstration is for GoDaddy, the steps below can be applied to any other web registrar as well.

Read More
0 Comments
    Picture

    Aryan Bhasin

    Categories

    All
    Computer Science
    Economics
    Literature
    Opinions
    Reflective
    Reviews
    Television

    Two roads diverged in a wood, and I--
    I took the one less travelled by
    ​

    And that has made all the difference

    Archives

    November 2017
    October 2017
    July 2017
    June 2017
    May 2017
    March 2017
    February 2017
    November 2016
    September 2016

  • Blog
  • IB Resources
  • Standardised Testing
    • ACT Resources
    • SAT Resources