Able
Askowl Base Library Enabler
PlayModeController.cs
1 // Copyright 2018 (C) paul@marrington.net http://www.askowl.net/unity-packages
2 
3 using System;
4 using System.Collections;
5 using UnityEditor;
6 using UnityEngine;
7 using UnityEngine.SceneManagement;
8 using UnityEngine.UI;
9 
10 namespace Askowl {
11  /// <a href="http://bit.ly/2NW3mGj">Control a running scene (skeleton implementation)</a>
12  public partial class PlayModeController {
13  /// <a href="http://bit.ly/2RhLCD5"></a>
14  protected Scene Scene;
15 
16  /// <a href="http://bit.ly/2NZbbe8">Load a scene by name (must be in build)</a>
17  protected virtual IEnumerator LoadScene(string sceneName) {
18  if (!Application.CanStreamedLevelBeLoaded(sceneName)) {
19  Debug.Log( //throw new Exception(
20  $@"Add the following to your file:
21  private static string sceneName = ""{sceneName}"";
22  #if UNITY_EDITOR
23  [InitializeOnLoadMethod] private static void AddSceneToBuildSettings() => AddSceneToBuildSettings(sceneName);
24  #endif
25  ");
26  }
27  var handle = SceneManager.LoadSceneAsync(sceneName: sceneName, mode: LoadSceneMode.Single);
28  if (handle == null) yield break;
29  while (!handle.isDone) yield return null;
30  Scene = SceneManager.GetActiveScene();
31  yield return null;
32  }
33 
34  /// <a href=""></a> //#TBD#//
35  public static void AddSceneToBuildSettings(string path) {
36  #if UNITY_EDITOR
37  path = Objects.FindFile($"{path}.unity");
38  if (path == null) return;
39  var scenes = EditorBuildSettings.scenes;
40  for (int i = scenes.Length - 1; i >= 0; i--) { // most likely at the end
41  if (scenes[i].path == path) return;
42  }
43 
44  var newSettings = new EditorBuildSettingsScene[scenes.Length + 1];
45  Array.Copy(scenes, newSettings, scenes.Length);
46  var sceneToAdd = new EditorBuildSettingsScene(path, true);
47  newSettings[newSettings.Length - 1] = sceneToAdd;
48  EditorBuildSettings.scenes = newSettings;
49  #endif
50  }
51 
52  /// <a href="http://bit.ly/2NUH87i">Push a GUI button</a>
53  public static IEnumerator PushButton(Button button) {
54  if (button != null) {
55  button.Select();
56  button.onClick.Invoke();
57  yield return null;
58  }
59  }
60 
61  /// <a href="http://bit.ly/2NZZYKj">See if component is visible on the UI display/screen</a>
62  public bool IsDisplayingInUi(RectTransform transform) {
63  if ((transform == null) || !transform.gameObject.activeInHierarchy) return false;
64 
65  Rect screenRect = new Rect(0, 0, Screen.width, Screen.height);
66  var objectCorners = new Vector3[4];
67  transform.GetWorldCorners(fourCornersArray: objectCorners);
68  if (Compare.AlmostEqual(objectCorners[0].y, objectCorners[1].y)) return false;
69  if (Compare.AlmostEqual(objectCorners[1].x, objectCorners[2].x)) return false;
70  return screenRect.Contains(point: objectCorners[1]);
71  }
72  }
73 }
Definition: Clock.cs:3
bool IsDisplayingInUi(RectTransform transform)
See if component is visible on the UI display/screen
static IEnumerator PushButton(Button button)
Push a GUI button
virtual IEnumerator LoadScene(string sceneName)
Load a scene by name (must be in build)
static void AddSceneToBuildSettings(string path)
//#TBD#//
Control a running scene (skeleton implementation)