List of Naninovel Services
Posted: 26 Nov 2020 12:59
Click details to show the script. Hope you find it useful.
Code: Select all
using Naninovel;
using Naninovel.UI;
using UnityEngine;
/// <summary>
/// List of Naninovel Engine. </summary>
public class NServices : MonoBehaviour
{
private static NServices _instance; // Singeleton
private IActorManager actorManager;
private IAudioManager audioManager;
private IBackgroundManager backgroundManager;
private ICameraManager cameraManager;
private ICharacterManager characterManager;
private IChoiceHandlerManager choiceHandlerManager;
private ICustomVariableManager customVariableManager;
private IInputManager inputManager;
private IScriptManager scriptManager;
private IScriptPlayer scriptPlayer;
private IStateManager stateManager;
private ISpawnManager spawnManager;
private IUIManager uIManager;
private IMoviePlayer moviePlayer;
private ITextManager textManager;
private ITextPrinterManager textPrinterManager;
private IUnlockableManager unlockableManager;
private void Awake()
{
_instance = this;
if (Engine.Initialized) GetService(); // Check Naninovel Engine is online and ready
else Engine.OnInitializationFinished += GetService; // If not, add listener to execute GetService() after Naninovel Engine is online and ready
}
private void GetService()
{
actorManager = Engine.GetService<IActorManager>();
audioManager = Engine.GetService<IAudioManager>();
backgroundManager = Engine.GetService<IBackgroundManager>();
cameraManager = Engine.GetService<ICameraManager>();
characterManager = Engine.GetService<ICharacterManager>();
choiceHandlerManager = Engine.GetService<IChoiceHandlerManager>();
customVariableManager = Engine.GetService<ICustomVariableManager>();
inputManager = Engine.GetService<IInputManager>();
moviePlayer = Engine.GetService<IMoviePlayer>();
scriptManager = Engine.GetService<IScriptManager>();
scriptPlayer = Engine.GetService<IScriptPlayer>();
stateManager = Engine.GetService<IStateManager>();
spawnManager = Engine.GetService<ISpawnManager>();
textManager = Engine.GetService<ITextManager>();
textPrinterManager = Engine.GetService<ITextPrinterManager>();
unlockableManager = Engine.GetService<IUnlockableManager>();
uIManager = Engine.GetService<IUIManager>();
GetNaninovelUI();
}
private void GetNaninovelUI()
{
GetBacklogUI = uIManager.GetUI<IBacklogUI>();
GetCGGalleryUI = uIManager.GetUI<ICGGalleryUI>();
GetConfirmationUI = uIManager.GetUI<IConfirmationUI>();
GetContinueInputUI = uIManager.GetUI<IContinueInputUI>();
GetExternalScriptsUI = uIManager.GetUI<IExternalScriptsUI>();
GetLoadingUI = uIManager.GetUI<ILoadingUI>();
GetManagedUI = uIManager.GetUI<IManagedUI>();
GetMovieUI = uIManager.GetUI<IMovieUI>();
GetPauseUI = uIManager.GetUI<IPauseUI>();
GetRollbackUI = uIManager.GetUI<IRollbackUI>();
GetSaveLoadUI = uIManager.GetUI<ISaveLoadUI>();
GetSceneTransitionUI = uIManager.GetUI<ISceneTransitionUI>();
GetSettingsUI = uIManager.GetUI<ISettingsUI>();
GetTipsUI = uIManager.GetUI<ITipsUI>();
GetTitleUI = uIManager.GetUI<ITitleUI>();
GetVariableInputUI = uIManager.GetUI<IVariableInputUI>();
}
public IBacklogUI GetBacklogUI { get; private set; }
public ICGGalleryUI GetCGGalleryUI { get; private set; }
public IConfirmationUI GetConfirmationUI { get; private set; }
public IContinueInputUI GetContinueInputUI { get; private set; }
public IExternalScriptsUI GetExternalScriptsUI { get; private set; }
public ILoadingUI GetLoadingUI { get; private set; }
public IManagedUI GetManagedUI { get; private set; }
public IMovieUI GetMovieUI { get; private set; }
public IPauseUI GetPauseUI { get; private set; }
public IRollbackUI GetRollbackUI { get; private set; }
public ISaveLoadUI GetSaveLoadUI { get; private set; }
public ISceneTransitionUI GetSceneTransitionUI { get; private set; }
public ISettingsUI GetSettingsUI { get; private set; }
public ITipsUI GetTipsUI { get; private set; }
public ITitleUI GetTitleUI { get; private set; }
public IVariableInputUI GetVariableInputUI { get; private set; }
public IActorManager GetActorManager() => actorManager;
public IAudioManager GetAudioManager() => audioManager;
public IBackgroundManager GetBackgroundManager() => backgroundManager;
public ICameraManager GetCameraManager() => cameraManager;
public ICharacterManager GetCharacterManager() => characterManager;
public IChoiceHandlerManager GetChoiceHandlerManager() => choiceHandlerManager;
public ICustomVariableManager GetCustomVariableManager() => customVariableManager;
public IInputManager GetInputManager() => inputManager;
public IMoviePlayer GetMoviePlayer() => moviePlayer;
public IScriptPlayer GetScriptPlayer() => scriptPlayer;
public IScriptManager GetScriptManager() => scriptManager;
public ISpawnManager GetSpawnManager() => spawnManager;
public IStateManager GetStateManager() => stateManager;
public ITextManager GetTextManager() => textManager;
public ITextPrinterManager GetTextPrinterManager() => textPrinterManager;
public IUnlockableManager GetUnlockableManager() => unlockableManager;
public IUIManager GetUIManager() => uIManager;
/// <summary>
/// Play Naninovel script.
/// </summary>
/// <remarks>
/// Example: NServices.Get().PlayScript("Sky","Ending");</remarks>
public void PlayScript(string ScriptName, string LabelName = null)
{
scriptPlayer.PreloadAndPlayAsync(ScriptName, label: LabelName);
}
/// <Summary>
/// Is Naninovel Engine is ready and online? </Summary>
public bool TheEngine()
{
return Engine.Initialized;
}
/// <Summary>
/// Is UI Name currently visible to player? </Summary>
/// <remarks>
/// Example: NServices.Get().IsUIActive("TitleUI");</remarks>
/// <param name="UIName"> Naninovel UI Name.</param>
public bool IsUIActive(string UIName)
{
var a = uIManager.GetUI(UIName).Visible;
return a;
}
public string GetValueString(string VariableName)
{
var a = GetCustomVariableManager().GetVariableValue(VariableName);
return a;
}
public float GetValueFloat(string VariableName)
{
GetCustomVariableManager().TryGetVariableValue<float>(VariableName, out var floatValue);
return floatValue;
}
public void SetValueFloat(string VariableName, float FloatValue)
{
GetCustomVariableManager().TrySetVariableValue(VariableName, FloatValue);
}
public int GetValueInt(string VariableName)
{
GetCustomVariableManager().TryGetVariableValue<int>(VariableName, out var IntValue);
return IntValue;
}
public void SetValueInt(string VariableName, int IntValue)
{
GetCustomVariableManager().TrySetVariableValue(VariableName, IntValue);
}
public bool GetValueBoolean(string VariableName)
{
GetCustomVariableManager().TryGetVariableValue<bool>(VariableName, out var BoolValue);
return BoolValue;
}
public void SetValueBoolean(string VariableName, bool BoolValue)
{
GetCustomVariableManager().TrySetVariableValue(VariableName, BoolValue);
}
/// <summary>
/// Singeleton. Calling from another script and you don't need to type Engine.GetService anymore. Attach this script as Naninovel UI Prefab. Example code: NServices.Get().GetScriptPlayer(); </summary>
/// <remarks>
/// If you are calling from Monobehaviour Script like Start, Awake, OnEnable, OnDisable you need to check wheter Naninovel Engine is online and ready or it will get null error. Check Awake method in this script.</remarks>
public static NServices Get()
{
return _instance;
}
///Example how to use it from another script. Copy this codes to new C# script. To remove comments, block all codes then CTRL + K, then CTRL + U.
//public void TestGetService() // We want to disable Auto Play novel.
//{
// var player = NServices.Get().GetScriptPlayer(); // Get Script Player service as variable player
// player.SetAutoPlayEnabled(false); // Set Auto Play false. If you are using Visual Studio 2019, it will has list dropdown of methods.
//}
//public void TestGetUI() // We want to show Title UI.
//{
// var myTitleUI = NServices.Get().GetTitleUI; // Get Title UI directly as variable myTitleUI
// myTitleUI.Show(); // Set Show. If you are using Visual Studio 2019, it will has list dropdown of methods.
//}
}