Showing posts with label Unity. Show all posts
Showing posts with label Unity. Show all posts

Tuesday, May 7, 2013

Xbox Controller Input Wrapper for Unity




After setting up Gamepad input in Unity for around the 5th time, I decided to write a wrapper class to avoid future suffering. The class mimics the Input.GetButton() and Input.GetButtonDown() functions.

I also added a GetState() function, similar to XNA, which returns a snapshot of the controller's state.

Caveats:
  • You will need to replace the InputManager.asset file located in the ProjectSettings folder with the one contained in the package.
  • GetButtonDown() and GetButton() cannot be used to check the up/down/left/right buttons because Unity treats the dpad as an axis.


Thursday, April 25, 2013

Simple C# Unity Serializer

I wrote this class to serializes common unity types into and out of a byte array. Its nothing fancy, but its handy for networking player state.

     //--- Serialize ---  
     UnitySerializer srlzr = new UnitySerializer();  
     srlzr.Serialize(new Vector3(4, 8, 6));  
     byte[] byteArray = srlzr.ByteArray;  
   
     // send byte array somewhere..  
   
     //--- Deserialize ---  
     UnitySerializer dsrlzr = new UnitySerializer(byteArray);  
     Vector3 point3 = dsrlzr.DeserializeVector3();  

It supports Vector3, Vector2 and Quaternions, as well as the common primitives and its easy enough to extend to extra types. Just be sure to deserialize the data in the same order it was serialized. Check out the Example() function at the bottom to see how it works.