Thursday, June 25, 2015

Things I've learned about networking in Unity 5.1

In Unity 5.1 networking features were added.

In order to use them you'll want to create an empty game object. Next you will add a Network Manager component along with a network HUD component.
For any objects that you want to interact with and have loaded via network like a player you want to add the component Network identity.

This component has two flags, Server Only or Local Player Authority. Server only will have the object only live on the server. Local Player authority will have it be client only.

In your script you want to add a Using statement for Unity.Networking. Next you will want your class to inherit from NetworkBehaviour instead of MonoBehavior.
In your Update method you can have a check for if (!isLocalPlayer){return;} which will exit the update method if it's not the local player. This will prevent you from playing as multiple characters at the same time.


Thursday, June 4, 2015

How to play music in Unity

I added some music to the PH test because I was tired of not having music.

Here is my pause script which switches the tracks playing. The Main Camera object has an AudioSource object (added when you drag and drop an audio file onto it).


using UnityEngine;
using System.Collections;

public class Pause : MonoBehaviour {
    bool paused = false;
    GameObject pauseCanvas, camera;
    AudioSource audioSource;
    public AudioClip pauseClip, musicClip;
    // Use this for initialization
    void Start () {
        pauseCanvas = GameObject.FindGameObjectWithTag("PauseCanvas");
        camera = GameObject.FindGameObjectWithTag("MainCamera");
        audioSource = camera.GetComponentInChildren<AudioSource>();
        pauseCanvas.SetActive(false);
    }
   
    // Update is called once per frame
    void Update () {
        if (Input.GetButtonDown("Cancel")) {
            paused = !paused;
            if (paused) {
                Time.timeScale = 0;
                Debug.Log("Paused");
                pauseCanvas.SetActive(true);
                audioSource.clip = pauseClip;
                audioSource.Play();
            }
            else {
                Time.timeScale = 1;
                Debug.Log("Unpaused");
                pauseCanvas.SetActive(false);
                audioSource.clip = musicClip;
                audioSource.Play();
            }
        }
    }
}

Tuesday, May 26, 2015

Sample website

I've created an Azure website for uploading prototypes to.

http://postmita.azurewebsites.net/

It's good for hosting things out there and gave me some practice with MVC.

Tuesday, April 28, 2015

Load levels using new UI system (Unity 5)

I recently discovered how to load levels using the new UI system in Unity 5.

  • Add a canvas to your scene.
  • Add a button to your canvas.
  • Create a new script as follows:


  • using UnityEngine;
  • using System.Collections;
  • public class ButtonNextLevel : MonoBehaviour
  • {
  • public void NextLevelButton(int index)
  • {
  • Application.LoadLevel(index);
  • }
  • public void NextLevelButton(string levelName)
  • {
  • Application.LoadLevel(levelName);
  • }
  • }
    • Create a new empty game object and attach the script.
    • On the Button (Script) component of the button, select the empty game object under the On Click() box.
    • Select the ButtonnextLevel.NextLevelButton (string) method in the upper right dropdown button.
    • Enter the name of the level you want to load (must be a scene in the build settings). 

    Wednesday, January 28, 2015

    How to get your Unity game running on a retail PS Vita.

    1. Buy a Vita.

    2. Apply for Developer Registration and publisher license here:
     https://psm.playstation.net/portal/en/index.html#register/signin

    3. Build your project in Unity. They have a custom unity editor designed for PSM here:
     https://psm.playstation.net/static/general/all/en/sdk_assets.html
    This page is pretty good and has some of the steps needed. It seems they update the editor pretty frequently. I think the next jump will be to Unity 5.0 according to some forum posts.
    4. Download the PSM Tool set from the same page.
    5. On your PS Vita download the "PSM Development Assistant for Unity" from the PS Store.
    6. Plugin your Vita to your computer and run the PSM Dev Assistant. you will need your publisher license in order for it to fully setup.
    7. In Unity open the build menu under File and select PSM.





    8. Open the Publishing Utility and enter some of the Metadata values in the first tab visible.


    9. You'll need to go to Key Management and hit the Key with the + icon next to it to create a key. Honestly, I don't know much of what I was doing here. I just put in what looked like required fields.


    10. Hit Build and Run. If there were no errors, you should see a progress bar appear saying it's building and deploying to the PSM device. On your vita there should be an install progress bar. Then it will launch the game.

    Running on my retail PS Vita
    My game doesn't really have a quit feature implemented yet so I have to close the app completely to stop it. The games that have been loaded will appear in a list when you launch the PSM for Unity app again on your vita so you can show your friends.