unity3d

Unity 3D Game Engine – Get OS Language

Unity 3D Game Engine – Get OS Language

Inside Hierarchy create a scene with:

1. Main Camera

2. GUI Text, attach the script ‘GetLanguage.js’

GetLanguage.js


#pragma strict

function Start () {
	// Prints to a guiText the actual language that the system is using.
	guiText.text = Application.systemLanguage.ToString();
}

function Update () {

}

NOTICE ToString(); function that converts value to string.

The final result is: Italian

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Get OS Language

Unity 3D Game Engine – Get Screenshot – Desktop

Unity 3D Game Engine – Get Screenshot – Desktop

How to get a in-game Screenshot with Unity 3D

Method 1

Application.CaptureScreenshot

NOTICE: CaptureScreenshot is asynchronous. This is because capturing and saving the screen can take awhile.

Inside Hierarchy create a structure with:

1. Some Objects

2. Main Camera, attach the script ‘GetScreenShot.js’

GetScreenShot.js


#pragma strict

function Start() {
}

function OnGUI()
{
  if (GUI.Button(Rect(10,10,200,50),"Capture Screenshot"))
  { 
    print (Application. persistentDataPath);
    //static function CaptureScreenshot(filename: string, superSize: int = 0): void;
    Application.CaptureScreenshot("Pictures/Screenshot.png");
  }
}


To test it, inside your Project folder create the subfolder /Pictures

Play it, you will find yourProjectFolder/Pictures/Screenshot.png

The console will print: ‘C:/Users/yourusername/AppData/LocalLow/DefaultCompany/Touch_Move’ on Win 7

You can Use persistentDataPath.
The Application.persistentDataPath is different for various platforms (iOS vs Android), and is the /Documents directory, a read-write path where you can put your downloaded stuff

Use the code:


#pragma strict

function Start() {
}

function OnGUI()
{
  if (GUI.Button(Rect(10,10,200,50),"Capture Screenshot"))
  { 
    print (Application. persistentDataPath);
    //static function CaptureScreenshot(filename: string, superSize: int = 0): void;
    Application.CaptureScreenshot(Application.persistentDataPath+"/Frame.png");
  }
}

It will save the image into:
C:/Users/yourusername/AppData/LocalLow/DefaultCompany/Touch_Move/Frame.png

See also:

Method 2

Texture2D.EncodeToPNG

Reference: http://docs.unity3d.com/Documentation/ScriptReference/Texture2D.EncodeToPNG.html

Encodes this texture into PNG format.


        // Saves screenshot as PNG file.
	import System.IO;
	// Take a shot immediately
	function Start () {
		UploadPNG ();
	}
	function UploadPNG () {
		// We should only read the screen buffer after rendering is complete
		yield WaitForEndOfFrame();
		// Create a texture the size of the screen, RGB24 format
		var width = Screen.width;
		var height = Screen.height;
		var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
		// Read screen contents into the texture
		tex.ReadPixels (Rect(0, 0, width, height), 0, 0);
		tex.Apply ();
		// Encode texture into PNG
		var bytes = tex.EncodeToPNG();
		Destroy (tex);
		// For testing purposes, also write to a file in the project folder
		// File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);

		// Create a Web Form
		var form = new WWWForm();
		form.AddField("frameCount", Time.frameCount.ToString());
		form.AddBinaryData("fileUpload",bytes);
		// Upload to a cgi script
		var w = WWW("http:/localhostcgi-bin/env.cgi?post", form);
		yield w;
		if (w.error != null) {
			print(w.error);
		} else {
			print("Finished Uploading Screenshot");
		}
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Get Screenshot – Desktop

Unity 3D Game Engine – JavaScript – Point and Click Movement – No NavMesh

Unity 3D Game Engine – JavaScript – Point and Click Movement – No NavMesh

– UNTESTED –

Create a Sphere (the player) and assign:

PropertiesAndCoroutines.js


#pragma strict

public var smoothing : float = 7f;
private var target : Vector3;

function  SetTarget(value : Vector3)
{
    target = value;
        
    StopCoroutine("Movement");
    StartCoroutine("Movement", target);
}

function Movement (target : Vector3)
{
    while(Vector3.Distance(transform.position, target) > 0.05f)
    {
        transform.position = Vector3.Lerp(transform.position, target, smoothing * Time.deltaTime);
        
        yield;
    }
}

Create a Plane (the ground) and assign:

ClickSetPosition.js


#pragma strict

public var coroutineScript : PropertiesAndCoroutines; // richiama lo script sopra
    

function OnMouseDown ()
{
    var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit : RaycastHit;
    
    Physics.Raycast(ray, hit);
    
    if(hit.collider.gameObject == gameObject)
    {
        var newTarget : Vector3 = hit.point;
        // invia il parametro target allo script PropertiesAndCoroutines        
        coroutineScript.SetTarget(newTarget); 
    }
}

Come funziona?

1. ClickSetPosition.js
– al click il RayCast, calcolato da Camera.main definisce un valore Vector3 XYZ di posizione
– il valore viene mandato alla Coroutine con coroutineScript.SetTarget(newTarget)

2. PropertiesAndCoroutines.js
– riceve il valore XYZ
– ferma la Couroutine, se l’oggetto si sta muovendo viene arrestato
– avvia di nuovo la Courotine, l’oggetto si sposterà verso la nuova destinazione.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – JavaScript – Point and Click Movement – No NavMesh

Unity 3D Game Engine – Orbit Around an Object

Unity 3D Game Engine – Orbit Around an Object

Gravity – Orbit

Inside Hierarchy create:

1. Sphere

2. Cube and attach GravityScript.js


#pragma strict

public var target : Transform;
    
    
function Update () 
{
    var relativePos : Vector3 = (target.position + new Vector3(0, 1.5f, 0)) - transform.position;
    var rotation : Quaternion = Quaternion.LookRotation(relativePos);
    
    var current : Quaternion = transform.localRotation;
    
    // Spherical Linear Interpolation
    transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime);
    transform.Translate(0, 0, 3 * Time.deltaTime);
}
 

Inspector> GravityScript.js> DRAG AND DROP Sphere over var target

3. Play> the Cube will rotate around the Sphere as a planet around the sun.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Orbit Around an Object

Unity 3D Game Engine – LookAt Object

Unity 3D Game Engine – LookAt Object

Inside Hierarchy create:

1. Sphere and attach MotionScript.js


#pragma strict

public var speed : float = 3f;


function Update () 
{
    transform.Translate(-Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0);
}

2. Cube and attach LookAtScript.js


#pragma strict

public var target : Transform;

function Update () 
{
    var relativePos : Vector3 = target.position - transform.position;
    transform.rotation = Quaternion.LookRotation(relativePos);
}

Inspector> LookAtScript.js> DRAG AND DROP Sphere over var target

3. Move the Sphere using keyboard arrow, the Cube will look at the Sphere

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – LookAt Object