Saturday, December 17, 2016

Control an Animation Transition

This post covers transitioning an animation with a software event.  This post builds upon Importing an Animation into Unity.  The example below is based on having imported a model from Blender that already has two animations embedded in it (swim and idle).

This post will show how to setup a transition condition, and how to trigger it in code.

Looking first at the Animator Controller view, you'll see I've already added transitions between the Swim and Idle state.


Import Blender Animation

This post details how to get an animated model into Unity.  This post is built on post on a different blog which can be found here (if using blender): Multiple Animations in Blender.  Once you've created your model with built in animations, bring it into the Unity project.  In this example my model is called "TestRig".

The first step is to add an  Assets > Create > Animator Controller.  When you double click on it, you'll see a node-editing type view.


Friday, December 16, 2016

Google Cardboard Input

Successfully setting up input for Google Cardboard has a lot to do with applying scripts to the right place.  This post will cover what I've found to be the minimal set to get this working.

Please follow the setup steps in this Post as a prerequisite, as you'll need to have loaded the SDK package into your project, and added a GvrMain to your scene.

Setup the Scene

  1. On the GvrMain
    • Add the GvrPointerManager script
  2. On the Main Camera under the GvrMain
    1. Add the GvrPointerPhysicRaycaster script
    2. Add a GvrReticlePointer prefab as a child of the Main Camera
  3. Add an EventSystem to the scene
    1. Remove the StandaloneInputModule script
    2. Add the GvrPointerInputModule

Tuesday, December 13, 2016

Adding Background Sound (or Music)

Adding background (ambient noise) or perhaps music to your scene is a simple of matter of adding an Audio Source to your Main Camera.

If you camera object does not already have an Audio Listener, then you will need to add one.

In this way the sound will always be prevalent instead of based on how far or near you are to the source.

Add a Spatial Sound

Making use of the Google Cardboard SDK, you can add spatial sounds to your scene that react to head direction changes.

You can add a spatial sound in two ways:

  • Drag a GvrAudioSource prefab into your scene
  • Add the GvrAudioSource script to your object
Ensure that your project settings have the Audio value of Spatializer Plugin set to the GVR Audio Spatializer.

Edit > Project Settings > Audio


You also may have to enable the GvrAudioListener script that resides on the GvrMain > Head > MainCamera game object.  This is provided as a part of the prefab.

At this point, when you play your game, you should hear the sound (assuming the source is in range), and as you turn your head, you should hear the sound move accordingly.

Saturday, December 10, 2016

Google Cardboard Integration

Integration with Google Cardboard was not trivial, and I ran into several hiccups, but here is the process of getting Google Cardboard building and deployed with Unity (5.5.0f3)


  1. First install the Android Studio to get the Android SDK on your computer
  2. Next, download the Google VR SDK for Unity
  3. Create a new Unity Project
  4. Assets > Import Package > Custom Package
    1. Navigate to where you downloaded the Google VR SDK, select package
    2. Deselect the following
      1. Google VR > Demos
      2. Plugins > Android > gvr-permissionsupport-release.aar
      3. Plugins > iOS
    3. Click Import button
    4. Go Ahead with "API Update Required"
  5. There is at least one script with a compilation error
    1. Find the script error in the console, or just open GvrVideoPlayerTexture.cs
    2. Line 595 - add a "yield" before the "return false;"
  6. Allow extra import packages to be loaded "GVRBackwardsCompatibility"
  7. Delete the Main Camera
  8. Add a GvrMain prefab to your scene (this is your VR camera)
    1. Add other elements to your scene as desired
  9. File > Build Settings
    1. Select Android
    2. Player Settings > Identification > Bundle Identifier (update this field)
    3. Build
    4. When asked about Android 6.0 Platform requirements, click Continue
    5. If it complains about "Merging Android Manifests", you'll need to find the gvr-permissionsupport-release.aar file and manually delete it.

Friday, December 9, 2016

Debug and Deploy to Android

I'm excited that Unity's business model changed and now it is free to deploy to Android.

They have some very good Setup Instructions for debugging and deploying.

Some things to note:

  • In installing the Android Studio, my SDK was installing here:
    • C:\Users\**username**\AppData\Local\Android\android-sdk
  • Create a new project
  • Make sure to set the Bundle Identifier in:  
    • Update Player Settings > Other Settings > Bundle Identifier
If you want to Debug with your device, ensure it is connected via USB and click Build & Run.

If you want to deploy your game, just click Build.  This will produce an APK file which you can transfer to your device.

On your device, find the file in a file browser, select it, and when Android asks you if you want to install, say yes.  The game will launch and a generic icon will be added to your apps list.

Saturday, February 6, 2016

MonoDevelop WhiteSpace

White space drives me nuts in an IDE.  Here is how to set the whitespace in Mono:

To change it for all future instances of projects go to  Tools > Options and set the fields below.


To change the whitespace for the current project (if the prior step hasn't been done), go to Project > Solution Options, and set the following fields.


If you want to apply those changes to open files you can go to:  Edit > Format > Format Document, and it should apply the changes to the current document.

Delegates or Callbacks

Here is an example for using Delegates to achieve a callback capability.

In the class that is going to call the callback function:

public class PointBasketManager : MonoBehaviour
{
   public delegate void GameOverEvent ();

   private GameOverEvent game_over;

   public void SetGameOverCallback (GameOverEvent newCallback)
   {
      game_over = newCallback;
   }

   public void BasketTriggered (uint points)
   {
      // trigger game is over
      game_over ();
   }
}

The using class calls the function SetGameOverCallback() and passes in the function that should be called when game_over() is called.

public class GameSystem : MonoBehaviour

{
   void Start()
   {
      point_mgr.SetGameOverCallback(OnGameEnd);
   }

   public void OnGameEnd ()
   {
      // end game stuff

   }
}

Quick Prefab Creation


  1. Create the object in the scene (Hierarchy)
  2. Modify all desired attributes and components
  3. Drag the game object from the Hierarchy view to the Project view

Friday, February 5, 2016

Add Bounce


To add bounce to an object, first you need to create a Physic Material, and this can be done in the Assets > Create > Physic Material menu.  Note that upon creation, you should name the material.

 Set the attributes according to your desired behavior.

Attach the material to the Collider component on your game object.

Pseudo Polymorphism in Inspector

This is the closest I've been able to come in creating polymorphic assignment with game objects in the inspector.

The interface or abstract-base-class would be defined as below.  Note the keyword "virtual" must be used here.

public class IEventTrigger : MonoBehaviour
{
   public virtual void OnEvent()
   {}
}

Next the derived class inherits from this pseudo-interface.  Note in this case, the implementation must be overridden using the "override" keyword.

public class TriggerPrint : IEventTrigger
{
   public override void OnEvent()
   {
      Debug.Log("Event Logged");
   }
}

An example now of a class that takes a game-object that has the TriggerPrint script component.  When eventHandler.OnEvent() is called, it will call the TriggerPrint.OnEvent().

public class InputManager : MonoBehaviour 
{
   [SerializeField]
   private IEventTrigger eventHandler;

   // Use this for initialization
   void Start ()
   {
      eventHandler.OnEvent();
   }
}

Class Fields in Inspector

Examples of making fields available to the inspector

   public int myInt;

or

   [SerializeField]
   private int myInt; 

Unity Install - Conflict with MSVC

Received error code 1603 while trying to install Unity.

Read on a forum this is a non-issue.  Ignored the error, and as far as I can tell, Unity finished installing successfully.

Scene Navigation

F - Center on object

Left-Click - Box Select
Right-Click - Orbit Object
Middle-Click - Pan View
Alt+Right-Click - Zoom