Requesting permission in run time in Android

If we are making an app for Android greater than SDK 23, we need to request some sensitive permissions (for example, to write file in external storage) in run time.

Here is a snippet to do so:

if (Build.VERSION.SDK_INT >= 23) {
	if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
			== PackageManager.PERMISSION_GRANTED) {
	} else {

		ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
	}
}

 

If a folder (directory) is not existed, create it – Android Java Snippet

With this snippet, we are able to check if a folder existed in device’s storage or not. If not, then we create it.

Here is the snippet:

File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/afolder");
if (! directory.exists()){
	directory.mkdir();
}

 

Simple PHP read and write to a text file

This code snippet reads a text file that if it is not existed it will create one with some text in it.

Next time this code is executed, if the file existed, PHP echoes the content of the file.

<?php
$settingfile = "settings.txt";
$filelink = fopen($settingfile, "r");
$filecontent = fread($filelink, filesize($settingfile));
echo $filecontent;
fclose($filelink);
$newContents = "Hello world!";
if($filecontent != $newContents){
    $settingfile = "settings.txt";
    $filelink = fopen($settingfile, "w");
    fwrite($filelink, $newContents);
    fclose($filelink);
}
?>

Or this one, the better one:

<?php
$settingfile = "settings.txt";
if(file_exists($settingfile)){
    $filelink = fopen($settingfile, "r");
    $filecontent = fread($filelink, filesize($settingfile));
    echo $filecontent;
    fclose($filelink);
}else{
    $astring = "Hello world!";
    $settingfile = "settings.txt";
    $filelink = fopen($settingfile, "w");
    fwrite($filelink, $astring);
    fclose($filelink);
}
?>

 

Changing the skybox material programmatically in Unity

I’m using this simple script after attaching it to UI Buttons to change my skybox materials. Let’s say we have two skybox materials with two UI Buttons to switch between them.

Here is the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeSky : MonoBehaviour {

	public Material sky1;
	public Material sky2;

	public void SkyOne(){
		RenderSettings.skybox = sky1;
	}

	public void SkyTwo(){
		RenderSettings.skybox = sky2;
	}
}

 

Unity click and drag script to rotate camera

Just googling around to find how to rotate camera with mouse click and drag. Thanks to this link: https://answers.unity.com/questions/1189946/click-and-drag-to-rotate-camera-like-a-pan.html

This code is originally taken from that link:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CamRotate : MonoBehaviour {

	public float speed = 3.5f;
     private float X;
     private float Y;
 
     void Update() {
         if(Input.GetMouseButton(0)) {
             transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") * speed, -Input.GetAxis("Mouse X") * speed, 0));
             X = transform.rotation.eulerAngles.x;
             Y = transform.rotation.eulerAngles.y;
             transform.rotation = Quaternion.Euler(X, Y, 0);
         }
     }
}

Then I made a slight changes to clamp vertical camera rotation:

if(Input.GetMouseButton(0)) {
			transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") * speed, -Input.GetAxis("Mouse X") * speed, 0));
			X = transform.rotation.eulerAngles.x;
			if (X > 60.0f && X < 70.0f)
				X = 60.0f;
			if (X < 300.0f && X > 290.0f)
				X = 300.0f;
			Y = transform.rotation.eulerAngles.y;
			transform.rotation = Quaternion.Euler(X, Y, 0);
		}

 

JavaScript random numbers examples

Generating random numbers is a common thing in programming. Here is some examples in JavaScript:

//Basic Random
Math.random();
//From 0 to specific number
Math.random() * 10;
//Removing decimals
Math.floor(Math.random() * 10);
//Random between 5 and 10
Math.floor(Math.random() * 5) + 5;
//Random between 5 and 10 (included 10)
Math.floor(Math.random() * 5) + 6;