Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Automation hook: prepares and opens the game scene, then enters play mode, so the
// WebRTC stream can be tested without the editor window:
//
// Unity.exe -batchmode -projectPath <project> -executeMethod AutoPlaySmokeTest.Run
//
// (no -quit: the editor keeps playing until the process is stopped). Safe to delete.
using UnityEditor;
using UnityEditor.SceneManagement;

public static class AutoPlaySmokeTest
{
public static void Run()
{
SIPSorcerySceneSetup.EnsureScene();
EditorSceneManager.OpenScene(SIPSorcerySceneSetup.ScenePath);
EditorApplication.EnterPlaymode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//-----------------------------------------------------------------------------
// Filename: SIPSorcerySceneSetup.cs
//
// Description: Prepares the "Get Started With Unity" template's scene for
// streaming. The template ships GetStarted_Scene without the player robot
// (placing it is one of the tutorial's steps) and the robot carries the camera
// and the AudioListener, so an untouched template project has nothing to stream
// and its Game view says "No cameras rendering". This places the PlayerRobot and
// the template's background music in the scene and saves it.
//
// It runs once by itself, the first time the editor loads the project after
// setup.ps1 copied it in, and again on demand from the menu
// SIPSorcery > Add Robot And Music To Scene.
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------

using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

[InitializeOnLoad]
public static class SIPSorcerySceneSetup
{
public const string ScenePath = "Assets/Scenes/GetStarted_Scene.unity";
private const string RobotPrefabPath = "Assets/Prefabs/PlayerRobot.prefab";
private const string MusicPrefabPath = "Assets/Audio/Background_Music_Ethereal.prefab";

// Where the robot stood in the original working project of this example.
private static readonly Vector3 RobotPosition = new Vector3(10f, 5f, 0f);

// Recorded once the template's scene has been checked, so the automatic run never
// re-adds a robot that was removed on purpose. UserSettings is per project and per user.
private const string MarkerPath = "UserSettings/SIPSorcerySceneSetup.txt";

static SIPSorcerySceneSetup()
{
if (!File.Exists(MarkerPath))
{
EditorApplication.delayCall += AutoSetup;
}
}

private static void AutoSetup()
{
if (EditorApplication.isPlayingOrWillChangePlaymode || File.Exists(MarkerPath))
{
return;
}
if (EditorApplication.isCompiling || EditorApplication.isUpdating)
{
EditorApplication.delayCall += AutoSetup;
return;
}
EnsureScene(fromMenu: false);
}

[MenuItem("SIPSorcery/Add Robot And Music To Scene")]
private static void AddFromMenu() => EnsureScene(fromMenu: true);

/// <summary>
/// Adds the PlayerRobot and the background music to GetStarted_Scene if they are
/// missing, and saves the scene. Returns false if the project isn't from the template.
/// </summary>
public static bool EnsureScene(bool fromMenu = false)
{
var robotPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(RobotPrefabPath);
if (robotPrefab == null || AssetDatabase.LoadAssetAtPath<SceneAsset>(ScenePath) == null)
{
if (fromMenu)
{
Debug.LogWarning($"SIPSorcery: this project isn't from the Get Started template ({ScenePath} or " +
$"{RobotPrefabPath} is missing). Any scene with a camera tagged MainCamera can be streamed.");
}
return false;
}

// Edit the scene where it is if it's open, else open it alongside so the scene the
// user has open is left alone.
var scene = SceneManager.GetSceneByPath(ScenePath);
bool openedHere = !scene.isLoaded;
if (openedHere)
{
scene = EditorSceneManager.OpenScene(ScenePath, OpenSceneMode.Additive);
}

bool changed = false;
if (!ContainsInstanceOf(scene, robotPrefab))
{
var robot = (GameObject)PrefabUtility.InstantiatePrefab(robotPrefab, scene);
robot.transform.position = RobotPosition;
changed = true;
}

var musicPrefab = AssetDatabase.LoadAssetAtPath<GameObject>(MusicPrefabPath);
if (musicPrefab != null && !ContainsInstanceOf(scene, musicPrefab))
{
PrefabUtility.InstantiatePrefab(musicPrefab, scene);
changed = true;
}

if (changed)
{
EditorSceneManager.MarkSceneDirty(scene);
EditorSceneManager.SaveScene(scene);
Debug.Log($"SIPSorcery: added the PlayerRobot (camera and AudioListener) and background music to {ScenePath} and saved it.");
}
else if (fromMenu)
{
Debug.Log($"SIPSorcery: {ScenePath} already has the PlayerRobot and background music.");
}

if (openedHere)
{
EditorSceneManager.CloseScene(scene, removeScene: true);
}

Directory.CreateDirectory(Path.GetDirectoryName(MarkerPath));
File.WriteAllText(MarkerPath, $"Checked {ScenePath} for the PlayerRobot and background music.\n");
return true;
}

private static bool ContainsInstanceOf(Scene scene, GameObject prefab)
{
foreach (var root in scene.GetRootGameObjects())
{
if (PrefabUtility.GetCorrespondingObjectFromSource(root) == prefab)
{
return true;
}
}
return false;
}
}
Loading
Loading