Archive for the ‘ Unity3D ’ Category

SoundCanvas Introduction – Managing Audio in Unity5

ScreenShot

During the production of our internal game project at DreamSocket, I started creating a tool to bridge the gap between the new audio Mixer technology in Unity5 and the audio clips themselves.  The mixer and snapshots features of Unity5 are brilliant for designing your sound in a scene – everything you could ever want to really bring the sound design of your game to life.

The problem is, and has been for along time with Unity, how to manage a scene’s audio clips?  These are clips that *have* to be added to an object or scene physically rather than programmatically.  I say *have* to, but that’s really not the case from a developers point of view.  We can add sound and design the sound programmatically to our hearts content.

The problem is, we’re the ONLY ones who can work with the sound in a scene at that point and a sound designer is essentially at our mercy to implement his work accurately.  And to be honest, doing sound design with a “listen/stop/adjust code/play/repeat” model isn’t going to probably yield the best results.  Being able to mix and manage at design time in the editor is by far a much better solution.

This is where SoundCanvas comes in.

SoundCanvas is not only an audio API for controlling your game’s sound, but a Unity editor extension that allows you (or anyone else on your team) to design your scene’s audio in a central, easy to use, interface.

Some of the features:

  • Easy to use API for controlling sound playback, use snapshots for transitions and globally manage your games audio
  • Assign single audio clips to multiple game objects using tags
  • Assign multiple audio clips to single or multiple game objects
  • An easy way to marry audio clips with mixer groups
  • Change the target of an audio clip by simply changing what tags it targets
  • Creates and manages it’s own enum file for easy of use with the sound API

Currently, SoundCanvas is in development, but our goal is to release it via the Unity Asset Store as a free Editor Extension.  So until then, we’d love to hear feedback and reaction to what we have so far.

Have a bandit day!

Advertisements

Showing camera view frustrum in Scene view

We’ve been working on a 2D game in Unity3D and I’d written this a while back to show the view frustrum of the camera in the scene view and had been meaning to post it:

Yellow wireframe outlines view frustrum. Top is scene view, bottom is game view

Create a new c# script in Unity and call it “DrawCameraViewFrustrum.cs”.  Copy/paste the code below and attach it to your camera object in your scene.

using UnityEngine;
using System.Collections;

public class DrawCameraViewFrustrum : MonoBehaviour
{	
	public Color clr = new Color(0.1f, 0.14f, 0.8f, 0.5f);
	public void OnDrawGizmos()
    {	
		Gizmos.color = clr;
		Gizmos.DrawWireCube(transform.position, new Vector3((camera.orthographicSize*camera.aspect)*2, camera.orthographicSize*2, 10));
	}
}


UniSWF to Unity3D – Character Animation

This is a short tutorial I did for our internal group at DreamSocket on how to use a MovieClip with timeline animations (in this case, a character animation), with UniSWF, in Unity3D.  Thanks to Trevor Van Meter for the character animation for this demo!

How to split animations via script in Unity3D Editor

If you’ve purchased a set of 3D models (let’s say, a pack of zombie models) that come with an animation list, then you know what a pain it is to sit and add each animation name/start/end frame in the editor.  It’s not always necessary, but in this particular occasion, I had to do it for each model.

Thanks to someone over at Automation Systems Group, I was able to use one of their scripts ( with a slight edit for an error due to legacy code ) in the editor to add the animation clips via a text file.

The code below explains everything essentially, including how to format the txt file:

 

// FbxAnimListPostprocessor.cs : Use an external text file to import a list of

// splitted animations for FBX 3D models.
//
// Put this script in your "Assets/Editor" directory. When Importing or
// Reimporting a FBX file, the script will search a text file with the
// same name and the ".txt" extension.
// File format: one line per animation clip "firstFrame-lastFrame loopFlag animationName"
// The keyworks "loop" or "noloop" are optional.
// Example:
// 0-50 loop Move forward
// 100-190 die

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System;

public class FbxAnimListPostprocessor : AssetPostprocessor
{
 public void OnPreprocessModel()
 {
 if (Path.GetExtension(assetPath).ToLower() == ".fbx"
 && !assetPath.Contains("@"))
 {
 try
 {
 // Remove 6 chars because dataPath and assetPath both contain "assets" directory
 string fileAnim = Application.dataPath + Path.ChangeExtension(assetPath, ".txt").Substring(6);
 StreamReader file = new StreamReader(fileAnim);

 string sAnimList = file.ReadToEnd();
 file.Close();

 if (EditorUtility.DisplayDialog("FBX Animation Import from file",
 fileAnim, "Import", "Cancel"))
 {
 System.Collections.ArrayList List = new ArrayList();
 ParseAnimFile(sAnimList, ref List);

 ModelImporter modelImporter = assetImporter as ModelImporter;
 modelImporter.clipAnimations = (ModelImporterClipAnimation[])
 List.ToArray(typeof(ModelImporterClipAnimation));

 EditorUtility.DisplayDialog("Imported animations",
 "Number of imported clips: "
 + modelImporter.clipAnimations.GetLength(0).ToString(), "OK");
 }
 }
 catch {}
 // (Exception e) { EditorUtility.DisplayDialog("Imported animations", e.Message, "OK"); }
 }
 }

 void ParseAnimFile(string sAnimList, ref System.Collections.ArrayList List)
 {
 Regex regexString = new Regex(" *(?<firstFrame>[0-9]+) *- *(?<lastFrame>[0-9]+) *(?<loop>(loop|noloop| )) *(?<name>[^\r^\n]*[^\r^\n^ ])",
 RegexOptions.Compiled | RegexOptions.ExplicitCapture);

 Match match = regexString.Match(sAnimList, 0);
 while (match.Success)
 {
 ModelImporterClipAnimation clip = new ModelImporterClipAnimation();

 if (match.Groups["firstFrame"].Success)
 {
 clip.firstFrame = System.Convert.ToInt32(match.Groups["firstFrame"].Value, 10);
 }
 if (match.Groups["lastFrame"].Success)
 {
 clip.lastFrame = System.Convert.ToInt32(match.Groups["lastFrame"].Value, 10);
 }
 if (match.Groups["loop"].Success)
 {
 clip.loop = match.Groups["loop"].Value == "loop";
 }
 if (match.Groups["name"].Success)
 {
 clip.name = match.Groups["name"].Value;
 }

 List.Add(clip);

 match = regexString.Match(sAnimList, match.Index + match.Length);
 }
 }
}

NGUI for Unity

After interviewing with a few unity3D shops,I was hit with the same question every single time: “do you have NGUI experience?” – to which I replied “no, but I’m sure I can pick it up”. They didn’t share my optimism 🙂 Enter in NGUI for Unity!

What I love about e-books these days is their size and how concise they can be, and this book delivers not only on both counts, but sets you in motion with a very real world example project that covers a great portion of the capabilities of NGUI. Everything from the wizards to widgets to creating atlas’ and fonts is covered. The book isn’t exhaustive in its explanations but IS thorough in pointing out the features and properties of all the components it covers – which makes it easy to follow along with in the examples.

The author gets right to the meat of the matter immediately walking you step by step through the sample project. It helps to have Unity3D experience and you’ll have to understand the basics of how unity works with prefabs and code as this book is targeting unity devs looking for a solid UI framework. It IS written in a very practical manner so even a complete beginner could get something out of it.

Now, in thinking back about my response to potential clients about just “picking NGUI up”, I see how deep the framework really is. It’s much more than just a set of UI components, and this book really brought me up to speed in a very short amount if time – excellent work!

Find more information here

Starting New!

PinkyIconWell, it’s time to start fresh on a few fronts!

First, I’m now looking for a new position in Unity3D with another shop.  My time with Infrared5 has finally concluded after 6+ wonderful years there!  So, if you know of any shops looking for Unity3D developers or have need of a contractor, please drop me an email!

Second, Save Pinky!  has received MANY updates and is in for review by Apple right now [update: it’s just been approved and is live in the app store now!]- I’m hoping it’ll go live by mid January at the latest.  But the downloads continue to astonish me!  We’re getting well over 150 a day most days and the GameCenter leader board count is well over 12,000!  What’s even more, there are TWO players who have posted scores over 1000!!!!  what?!  Incredible!!  My high score is 635, and I can’t imagine hanging on for another 800 holes!!  But I’m so glad people have enjoyed it like I have, and with the new ramps and balls, they should have even more fun with it.

Third, I updated my blog to a new more clean look and cleaned up some of the images/icons on the right menu for my iOS game titles, and I have made a commitment to start adding more content related to C#, Unity and Flash this coming 2014!

Enjoy the screenshots from my work in the past 6+ years with Infrared5!

This slideshow requires JavaScript.

GetRandomInDonut() – Random location in a donut area

[tweetmeme source=”neoRiley” only_single=false]The scenario is: you’ve got a player object, and you want to spawn enemy characters away from the player, but within a range.  Basically, within a donut area, right?

using UnityEngine;
using System.Collections;

public class Utils
{
    public static Vector3 GetRandomInDonut(float min, float max)
    {
        float rot = Random.Range(1f, 360f);
        Vector3 direction = Quaternion.AngleAxis(rot, Vector3.up) * Vector3.forward;
        Ray ray = new Ray(Vector3.zero, direction);

        return ray.GetPoint( Random.Range(min, max) );
    }
}

To use it, just call it with the minimum and maximum distance away you want your new position to be. Then add it to the transform of where your player’s spawn point is:

public void LoadCharacters()
{
    // add the transform's position since the returned position is from a location of Vector3.zero
    Vector3 pos = transform.position + GetRandomInDonut(20f, 50f);
}