Skip to main content

Posts

Showing posts from August, 2013

After editing a Textfield, how to trigger an event using Return/Enter key in Unity

function OnGUI() { chatInput = GUILayout.TextField(chatInput);
if (GUI.changed) { Debug.Log("Text field has changed.");             activeReturnKey = true; } if(activeReturnKey) {             if(Event.current.isKey && Event.current.keyCode ==           KeyCode.Return) //Input.GetKeyDown(KeyCode.Return))                       {                                   print ("ReturnPressed");                                   activeReturnKey = false;                                   SendChat(chatInput);                       }
} }
function SendChat( info : String) {           print(info); }

How to setup Photon Networking in unity so we can use it from UnityScript ( Unity js )

When i first try to convert this simple c# code to unity js or unityscript, I got this error :

The name 'PhotonStream' does not denote a valid type ('not found')
The name 'PhotonMessageInfo' does not denote a valid type ('not found')

here is the c# version that i found on Photon Networking Example.
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    { if (stream.isWriting)         { //We own this player: send the others our data             stream.SendNext(transform.position);             stream.SendNext(transform.rotation);          } else         { //Network player, receive data             correctPlayerPos = (Vector3)stream.ReceiveNext();             correctPlayerRot = (Quaternion)stream.ReceiveNext();         }     }
And here is the JS version function OnPhotonSerializeView(stream : PhotonStream, info : PhotonMessageInfo) { if (stream.isWriting)     { //We own this player: send the others our data         stream.SendNext(transform.posit…

Simple Up, Down, Left, Right Swipe controls for touch devices( Iphone and Android) in unity

Note: There are more than 50 comments in this post, So, if your comment is not appearing on the first page than' 'don't get panic', there is aLoad Morebutton at the bottom of the page.



Here is a UnityScript (javascript version)

#pragma strict
var player : Transform;  // Drag your player here
privatevar fp : Vector2;  // first finger position
privatevar lp : Vector2;  // last finger position

function Update()
{
for (var touch : Touchin Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
lp = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{

if((fp.x - lp.x) > 80) // left swipe
{

player.Rotate(0,-90,0);

}
else if((fp.x - lp.x) < -80) // right swipe
{
player.Rotate(0,90,0);
}
else if((fp.y - lp.y) < -80 ) // up swipe
{
// add your jumping code here
}
}
}


}


add this script to any empty gameObject or your player than drag your player to player variable in editor now build a .apk or .ipa and check the re…