Unity 3D Game Engine – JS – Camera – Swap Multi Cameras – On Click

Unity 3D Game Engine – JS – Camera – Swap Multi Cameras – On Click

1. Create in the Hierarchy

– CameraTarget1 (Empty object)
– CameraTarget2 (Empty object)

– Main Camera> attach ‘CameraControl.js’

CameraControl.js:


#pragma strict

// ########################################################################################
// This complete script can be attached to a camera to change his Position and his Target
// You will move the camera by clicking GUI.Button CAMERA 1 and GUI.Button CAMERA 2
// ########################################################################################

// The relative speed at which the camera will catch up
// Small value = more steps to reach the new position
public var smooth : float = 1.5f;  

// Camera positions START ##################################################################
public var newPosCam1 : Vector3;   // Camera Position 1 
public var newPosCam2 : Vector3;   // Camera Position 2 
// Camera positions END ####################################################################

// Camera targets START ####################################################################
// The target variable shows up as a property in the inspector. 
// Drag another object onto it to make the camera look at it.
var target1 : Transform; 
var target2 : Transform;
// Camera targets END ######################################################################

function Start () {

}


function Update() {
	
}

// NOTICE: function OnGUI is outside others functions
// Move the Camera START ###################################################################	
function OnGUI () {
    // Button color and opacity setup START ################################################
    // 0.0f rende il bottone completamente trasparente, il tocco funziona ugualmente
    GUI.color = new Color(1,1,1,0.5f); 
    // Button color and opacity setup END ##################################################
    
    // Insert 8 pixels of space between the 2 buttons.
	GUILayout.Space (8);

   if (GUI.Button (Rect (600,10,200,200), "CAMERA 1")) {
        transform.position = Vector3.Lerp(transform.position, newPosCam1, smooth * Time.deltaTime);
        transform.LookAt(target2);
    }
   if (GUI.Button (Rect (600,250,200,200), "CAMERA 2")) {
        transform.position = Vector3.Lerp(transform.position, newPosCam2, smooth * Time.deltaTime);
        transform.LookAt(target1);
    }
}
// Move the Camera END #####################################################################

Main Camera> CameraControl.js:

– var Smoot: Small value = more steps to reach the new position

– New Pos Cam 1: first position of camera
– New Pos Cam 2: second position of camera

– Target 1: target of first position -> DRAG AND DROP from Hierarchy to Inspector CameraTarget1 (Empty object)
– Target 2: target of second position -> DRAG AND DROP from Hierarchy to Inspector CameraTarget2 (Empty object)

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – JS – Camera – Swap Multi Cameras – On Click

Unity 3D Game Engine – Get Mouse – Draw Line

Unity 3D Game Engine – Get Mouse – Draw Line

1. Main Camera attach the script LineMouse.js

LineMouse.js


#pragma strict

// It automatically adds Add Component> Effects> Line Renderer
@script RequireComponent(LineRenderer)

var lineRenderer : LineRenderer;
var myPoints : Vector3[];

function Start () {
    // Setup of LineRenderer Component
    lineRenderer = GetComponent(LineRenderer);
    lineRenderer.SetWidth(0.2,0.2);
}

function Update () {

    if(myPoints){

        lineRenderer.SetVertexCount(myPoints.Length);
        for(var i = 0;i<myPoints.Length;i++){
            lineRenderer.SetPosition(i,myPoints[i]);    
        }
    }
    else
    lineRenderer.SetVertexCount(0);
    
    if(Input.GetMouseButtonDown(0)){
        // it is only drawing 10 points per second, as per the line
        InvokeRepeating("AddPoint",.1,.1);
    } 
     if(Input.GetMouseButtonUp(0)){
        CancelInvoke();
        myPoints = null;
    }

}

function AddPoint(){

   Debug.Log("Add");

    var tempPoints : Vector3[];

    if(!myPoints)
        tempPoints = new Vector3[1];
    else{
        tempPoints = new Vector3[myPoints.Length+1];
       
    for(var j = 0; j < myPoints.Length; j++)
        tempPoints[j] = myPoints[j];
    
   }   
     var tempPos : Vector3 = Input.mousePosition;
    tempPos.z = 10;
    
    tempPoints[j] = camera.ScreenToWorldPoint(tempPos);
   myPoints = new Vector3[tempPoints.Length];
   for(j=0; j< myPoints.Length; j++) 
   myPoints[j] = tempPoints[j];
}

NOTICE: it seems to be a little sluggish.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Get Mouse – Draw Line

Unity 3D Game Engine – Build – Android – Windows OS

Unity 3D Game Engine – Build – Android – Windows OS

There are some steps you must follow before you can build and run any code on your Android device.

Tested on Unity 4.3f – Win 7 64 bit – Android 4.2

1. Java JDK

Install Java JDK, if you have JRE is not enought!

This is because if you want to run Java programs, but not develop them, download the Java Runtime Environment, or JRETM, if you want to develop applications for Java, download the Java Development Kit, or JDKTM. The JDK includes the JRE, so you do not have to download both separately.

You need Java JDK 6 – Windows x86 (not 64 bit becuase Unity 4.x is a 32 bit software) or over from here:

http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

2. Android SDK

2. Download the Android SDK from:
https://developer.android.com/sdk/index.html#ExistingIDE

It includes the essential Android SDK components and a version of the Eclipse IDE with built-in ADT (Android Developer Tools) to streamline your Android app development.

a. Unzip the ZIP file and copy it wherever you want, example c:\adt-bundle-windows-x86-

NOTICE: YOU HAVE NOT TO RENAME OR MOVE THE FOLDER AFTER UNZIP IT OR YOU WILL GET AN ERROR!

b. Run SDK Manager to complete the installation, this will take a lot of minutes.

c. Installing the SDK be sure to add at least one Android platform with API level equal to or higher than 9 (Platform 2.3 or greater), the Platform Tools, and the USB drivers if you’re using Windows

– If the Android device is automatically recognized by the system you still might need to update the drivers with the ones that came with the Android SDK. This is done through the Windows Device Manager.

– If the device is not recognized automatically use the drivers from the Android SDK, or any specific drivers provided by the manufacturer. Additional info can be found here: http://developer.android.com/sdk/win-usb.html

— use the Android SDK Manager tool that is included with the Android SDK.
— Launch the Android SDK Manager by double-clicking SDK Manager.exe, at the root of your SDK directory.
— Expand Extras
— Check Google USB Driver package and click Install
— Proceed to install the package.
When done, the driver files are downloaded into the \extras\google\usb_driver\ directory

—————————–
OR YOU CAN USE ANDROID STUDIO
—————————–

2. Android Studio

2. Download the Android Studio Bundle from:
http://developer.android.com/sdk/installing/studio.html#Updating

a. Launch the downloaded EXE file, android-studio-bundle-.exe

b. In step 4 of Installing the SDK be sure to add at least one Android platform with API level equal to or higher than 9 (Platform 2.3 or greater), the Platform Tools, and the USB drivers if you’re using Windows.

– If the Android device is automatically recognized by the system you still might need to update the drivers with the ones that came with the Android SDK. This is done through the Windows Device Manager.

– If the device is not recognized automatically use the drivers from the Android SDK, or any specific drivers provided by the manufacturer. Additional info can be found here: http://developer.android.com/sdk/win-usb.html

It will be installed at:
Windows: \Users\\AppData\Local\Android\android-studio\sdk\

c. Setup Windows variables

On some Windows systems, the launcher script does not find where Java is installed. If you encounter this problem, you need to set an environment variable indicating the correct location.

Select Start menu > Computer > System Properties > Advanced System Properties. Then open Advanced tab > Environment Variables and add a new system variable JAVA_HOME that points to your JDK folder, for example C:\Program Files\Java\jdk1.7.0_21.

3. Android Device

a. Turn on “USB Debugging” on your device.
Go to Settings -> Developer options, then enable USB debugging. As of Android Jelly Bean 4.2 the Developer options are hidden by default. To enable them tap on Settings -> About Phone -> Build Version multiple times. Then you will be able to access the Settings -> Developer options.

b. Install drivers

– Connect your Android-powered device to your computer’s USB port.
– Right-click on Computer from your desktop or Windows Explorer, and select Manage.
– Select Devices in the left pane.
– Locate and expand Other device in the right pane.
– Right-click the device name (such as Nexus S) and select Update Driver Software. This will launch the Hardware Update Wizard.
– Select Browse my computer for driver software and click Next.
– Click Browse and locate the USB driver folder. (The Google USB Driver is located in \extras\google\usb_driver\.)
– Click Next to install the driver.

4. Unity

Unity will ask to locate the folder where you installed the Android SDK, setup at:
Unity MAIN TOP MENU > Edit> Preferences> External Tools> Android SDK Location

MAIN TOP MENU> File> Buil Settings…> Platform> Android> ‘Switch platform’

MAIN TOP MENU> File> Buil Settings…> ‘Player Settings’> Inspector> setup parameters

MAIN TOP MENU> File> Buil Settings…> ‘Build’

Official Unity docs:
https://docs.unity3d.com/Documentation/Manual/android-sdksetup.html

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Build – Android – Windows OS

Unity – Open URL – OnClick

Unity – Open URL – OnClick

1. MAIN TOO MENU> GameObject> Create Other> Cube> Inspector> ‘Add Component’> New Script> Javascript> OpenUrl.js

OpenUrl.js


#pragma strict

function Update() {
}

function OnMouseDown ()
{
        // When you click over the object the web browser will start
        Application.OpenURL ("http://lucedigitale.com/"); 
}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity – Open URL – OnClick

Unity – WWW – Get Audio from Web

Unity – WWW – Get Audio from Web

1. MAIN TOP MENU> File> New Project…
2. MAIN TOP MENU> GameObject> Create Empty, name it ‘Music Player’
3. Hierarchy> select ‘Music Player’> Inspector> ‘Add component’> Audio Source check ‘Play On Awake’, check ‘Loop’
4. Hierarchy> select ‘Music Player’> Inspector> ‘Add component’> New Script> JavaScript> name it LoadMusic.js
5. LoadMusic.js

#pragma strict


function Start () {
    // Get Audio from Web
    var www = new WWW('http://www.lucedigitale.com/blog/wp-content/gimme-code/css/images-gimme-code-2013-0016/bgmusic.ogg');
    audio.clip = www.audioClip;
}

function Update () {
    if(!audio.isPlaying && audio.clip.isReadyToPlay)
        audio.Play();
}

6. Build and Play the build to hear the audio.
Sometimes you need wait for a few minutes if the web connection will be slow.

WARNING: If you use TOP Play button to preview the game you will get the console message:
‘Exception: Expected root element to be . found html instead’
It is a security layer applied to the Web Player, you have to Build and Play the final HTML file to hear audio.

Reference:
http://docs.unity3d.com/Documentation/ScriptReference/WWW.html

By |Unity3D, Video Games Development|Commenti disabilitati su Unity – WWW – Get Audio from Web