Archive for the ‘ Unity Editor ’ 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
{</pre>
#if UNITY_EDITOR
	public static Vector2 GetMainGameViewSize()
	{
		System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
		System.Reflection.MethodInfo GetSizeOfMainGameView = T.GetMethod("GetSizeOfMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
		System.Object Res = GetSizeOfMainGameView.Invoke(null,null);
		return (Vector2)Res;
	}
	#endif

	public static DrawCameraCull instance;
	public Color color = new Color(0.1f, 0.14f, 0.8f, 0.5f);
	public bool isZoomed = false;

	protected float unitsWidth = 8.95f;
	public float zoomedOrthoSize = 2f;

	protected float camVertExtent;
	protected float camHorzExtent;

	protected Transform playerTransform;
	protected Camera cam;
	protected Vector2 topRightCorner;
	protected Vector2 screenExtents;

	void Awake(){
		instance = this;
	}

	public void Start(){

		cam = GetComponent&lt;Camera&gt;();

		topRightCorner = new Vector2(1, 1);
		screenExtents = cam.ViewportToWorldPoint(topRightCorner);

		CheckCameraView();
	}

	public void ToggleZoom(){
		isZoomed = !isZoomed;

		if( isZoomed ) {
			cam.orthographicSize = zoomedOrthoSize;
			UpdateExtents();
		} else {
			cam.transform.position = new Vector3(0,0,-10);
			CheckCameraView();
		}
	}

	public void Reset(){
		isZoomed = false;
		cam.transform.position = new Vector3(0,0,-10);
		CheckCameraView();
	}

	protected float camX;
	protected float camY;
	protected float leftBound;
	protected float rightBound;
	protected float bottomBound;
	protected float topBound;
	public void Update(){

	}

	#if UNITY_EDITOR
	public virtual void OnDrawGizmos()
	{	
		//if( Application.isPlaying ) return;

		CheckCameraView();

		Gizmos.color = color;

		Matrix4x4 temp = Gizmos.matrix;
		Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
		if (GetComponent&lt;Camera&gt;().orthographic) {
			float spread = GetComponent&lt;Camera&gt;().farClipPlane - GetComponent&lt;Camera&gt;().nearClipPlane;
			float center = (GetComponent&lt;Camera&gt;().farClipPlane + GetComponent&lt;Camera&gt;().nearClipPlane)*0.5f;
			Gizmos.DrawWireCube(new Vector3(0,0,center), new Vector3(GetComponent&lt;Camera&gt;().orthographicSize*2*GetComponent&lt;Camera&gt;().aspect, GetComponent&lt;Camera&gt;().orthographicSize*2, spread));
		} else {
			Gizmos.DrawFrustum(Vector3.zero, GetComponent&lt;Camera&gt;().fieldOfView, GetComponent&lt;Camera&gt;().farClipPlane, GetComponent&lt;Camera&gt;().nearClipPlane, GetComponent&lt;Camera&gt;().aspect);
		}
		Gizmos.matrix = temp;
	}

	#endif

	protected void OnLevelWasLoaded(int level) {
		CheckCameraView();
	}

	protected void CheckCameraView(){

	}

	protected void UpdateExtents(){
		//if( !Application.isPlaying || Application.loadedLevel == 0 ) return;

		// update the extents
		camVertExtent = cam.orthographicSize;
		camHorzExtent = cam.aspect * camVertExtent;

		leftBound   = -screenExtents.x + camHorzExtent;
		rightBound  = screenExtents.x - camHorzExtent;
		bottomBound = -screenExtents.y + camVertExtent;
		topBound    = screenExtents.y - camVertExtent;
	}
<pre>}

Advertisements