Browse Source

LuaEnvironment starts a debug server when running in the Unity Editor. Use VS Code to debug.

master
Christopher 8 years ago
parent
commit
54f1ce9be9
  1. 52
      Assets/Fungus/Thirdparty/FungusLua/Scripts/Components/LuaEnvironment.cs
  2. 9
      Assets/Fungus/Thirdparty/VSCode.meta

52
Assets/Fungus/Thirdparty/FungusLua/Scripts/Components/LuaEnvironment.cs vendored

@ -3,37 +3,24 @@
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System; using System;
using System.Diagnostics;
using System.Linq; using System.Linq;
using MoonSharp.Interpreter; using MoonSharp.Interpreter;
using MoonSharp.Interpreter.Loaders;
using MoonSharp.VsCodeDebugger; using MoonSharp.VsCodeDebugger;
namespace Fungus namespace Fungus
{ {
/// <summary> /// <summary>
/// Wrapper for a MoonSharp Lua Script instance. /// Wrapper for a MoonSharp Lua Script instance.
/// A debug server is started automatically when running in the Unity Editor. Use VS Code to debug Lua scripts.
/// </summary> /// </summary>
public class LuaEnvironment : MonoBehaviour public class LuaEnvironment : MonoBehaviour
{ {
/// <summary>
/// Launches the remote Lua debugger in your browser and breaks execution at the first executed Lua command.
/// </summary>
[Tooltip("Launches the remote Lua debugger in your browser and breaks execution at the first executed Lua command. Standalone platform only.")]
[SerializeField] protected bool remoteDebugger = false;
/// <summary> /// <summary>
/// The MoonSharp interpreter instance. /// The MoonSharp interpreter instance.
/// </summary> /// </summary>
protected Script interpreter; protected Script interpreter;
/// <summary>
/// Instance of VS Code debug server when debugging option is enabled.
/// </summary>
protected MoonSharpVsCodeDebugServer debugServer;
/// <summary> /// <summary>
/// Flag used to avoid startup dependency issues. /// Flag used to avoid startup dependency issues.
/// </summary> /// </summary>
@ -45,13 +32,13 @@ namespace Fungus
} }
/// <summary> /// <summary>
/// This function is called when the MonoBehaviour will be destroyed. /// Detach the MoonSharp script from the debugger.
/// </summary> /// </summary>
protected virtual void OnDestroy() protected virtual void OnDestroy()
{ {
if (debugServer != null) if (DebugServer != null)
{ {
debugServer.Detach(interpreter); DebugServer.Detach(interpreter);
} }
} }
@ -125,16 +112,16 @@ namespace Fungus
yield return StartCoroutine(coroutine); yield return StartCoroutine(coroutine);
} }
protected virtual void ActivateVSCodeDebugger(Script script) protected virtual void StartVSCodeDebugger()
{
if (DebugServer == null)
{ {
// Create the debugger server // Create the debugger server
debugServer = new MoonSharpVsCodeDebugServer(); DebugServer = new MoonSharpVsCodeDebugServer();
// Start the debugger server // Start the debugger server
debugServer.Start(); DebugServer.Start();
}
// Attach the script to the debugger
debugServer.AttachToScript(script, gameObject.name);
} }
/// <summary> /// <summary>
@ -162,6 +149,11 @@ namespace Fungus
#region Public members #region Public members
/// <summary>
/// Instance of VS Code debug server when debugging option is enabled.
/// </summary>
public static MoonSharpVsCodeDebugServer DebugServer { get; private set; }
/// <summary> /// <summary>
/// Returns the first Lua Environment found in the scene, or creates one if none exists. /// Returns the first Lua Environment found in the scene, or creates one if none exists.
/// This is a slow operation, call it once at startup and cache the returned value. /// This is a slow operation, call it once at startup and cache the returned value.
@ -261,11 +253,15 @@ namespace Fungus
initializer.Initialize(); initializer.Initialize();
} }
#if UNITY_STANDALONE //
if (remoteDebugger) // Change this to #if UNITY_STANDALONE if you want to debug a standalone build.
{ //
ActivateVSCodeDebugger(interpreter);
} #if UNITY_EDITOR
StartVSCodeDebugger();
// Attach the MoonSharp script to the debugger
DebugServer.AttachToScript(interpreter, gameObject.name);
#endif #endif
initialised = true; initialised = true;

9
Assets/Fungus/Thirdparty/VSCode.meta vendored

@ -1,9 +0,0 @@
fileFormatVersion: 2
guid: f550ad0c0d1094942a72476811c503d6
folderAsset: yes
timeCreated: 1476781536
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save