Skip to content

Scene Setup In Unity

canab edited this page Oct 1, 2014 · 1 revision

This page describes a process of setting up a Unity scene that renders exported Flash content.

Initial Setup

  • Create empty Unity scene and save it.
  • Create empty gameObject and add FlashStage component to it. You will see a rectangle representing you canvas in Scene windows.
  • Adjust the size of your canvas by setting Width and Height properties in FlashStage editor.

  • Set your camera projection mode to Orthographic and adjust it size and position so that FlashStage canvas is in camera's field of view.

  • Add FlashDebugOptions component to the same gameObjects with FlashStage.

Now we are ready to display our content.

Creating Scene Component

NOTE: Make sure you have all your content exported as described in ProjectRoot/Assets/Resources/FlashBundles/<BundleName>/ directory. In my case it looks like this:

If everything is fine you should be able to see your content by selecting in Preview property of FlashStage component.

Now we need to create MonoBehaviour script that loads required bundle. Let's name it SampleFlashSceneController.cs. SceneBundle is a script generated by Flash Exporter tool.

using UnityEngine;
using Flunity;
using FlashBundles;

[RequireComponent(typeof(FlashStage))]
public class SampleSceneController : MonoBehaviour
{
	private FlashStage _stage;
	
	bool readyFlag = false; // For live reloading
	
	void OnEnable()
	{
		readyFlag = true;
	}
	
	void Update()
	{
		// Initialize here for dynamyc scene creation
		// when unity does recompiles scripts.
		if (readyFlag)
		{
			readyFlag = false;
			LoadResources();
		}
	}

	// Handle loading resources
	void LoadResources()
	{
		SceneBundle.instance.Loaded += it => OnResourcesLoaded();
		SceneBundle.instance.Load();
	}

	void OnResourcesLoaded ()
	{
		_stage = GetComponent<FlashStage>();
		
		// in case when flash resources has been updated in runtime
		// it is necessary remove all previous content
		_stage.root.RemoveChildren();
		
		CreateScene();
	}

	void CreateScene ()
	{
		var sprite = SceneBundle.circle_sprite.CreateInstance(); 
		_stage.root.AddChild(sprite);
	}
}

The result of the script below is a sprite rendered in the top left corner of canvas.

To unload resources:

void OnDestroy()
{
	SceneBundle.instance.Loaded -= it => OnResourcesLoaded();
	SceneBundle.instance.Unload();
}

Now you can begin adding your custom logic to your scene.