public static void InitEventSystemIfNeeded() { if (GameObject.FindObjectOfType <EventSystem>() == null) { ResourcesV2.LoadPrefab("Canvas/DefaultEventSystem"); } }
public virtual GameObject CreateListEntryUi(ActionMenu menu, bool showOnlyFavs, TaskCompletionSource <Entry> taskComplSource) { var entry = this; var listEntryUiGo = ResourcesV2.LoadPrefab(entry.listModeEntryPrefabName); var map = listEntryUiGo.GetLinkMap(); map.Get <Text>("Icon").text = entry.icon; map.Get <Text>("Title").text = entry.name; var description = map.Get <Text>("Description"); description.gameObject.SetActiveV2(menu.viewMode == ViewMode.full && !entry.descr.IsNullOrEmpty()); description.text = entry.descr; var button = map.Get <Button>("ActionSelected"); button.interactable = entry.isEnabled; map.Get <CanvasGroup>("ActionSelected").enabled = !entry.isEnabled; button.SetOnClickAction(go => { if (menu.entries.Contains(entry)) { menu.clickedEntry = entry; EventBus.instance.Publish(EventConsts.catUi + UiEvents.ACTION_MENU, menu.GetFullEntryId(entry)); taskComplSource.TrySetResult(entry); } entry.onClicked.InvokeIfNotNull(go); }); var onFavorite = map.Get <Toggle>("FavoriteToggle"); onFavorite.gameObject.SetActiveV2(!showOnlyFavs && entry.onFavoriteToggled != null); onFavorite.isOn = entry.isFavorite; onFavorite.SetOnValueChangedAction(entry.onFavoriteToggled); return(listEntryUiGo); }
private Dictionary <Entry, GameObject> FillTargetUi(ActionMenu menu, GameObject parentUi) { foreach (var child in parentUi.GetChildren()) { child.Destroy(); } // clear previous children if (!menu.title.IsNullOrEmpty() && menu.viewMode != ViewMode.iconsOnly) { var titleUi = parentUi.AddChild(ResourcesV2.LoadPrefab(menu.titlePrefabName)); var titleMap = titleUi.GetLinkMap(); titleMap.Get <Text>("TitleText").text = menu.title; } var sortedEntries = new List <Entry>(menu.entries); if (showOnlyFavorites) { sortedEntries.Add(NewShowMoreEntry(menu)); } sortedEntries.Sort(menu.SortMenuEntries); return(sortedEntries.ToDictionary(entry => entry, entry => { if (menu.viewMode == ViewMode.iconsOnly) { return parentUi.AddChild(entry.CreateIconEntryUi(menu, taskComplSource)); } else { return parentUi.AddChild(entry.CreateListEntryUi(menu, showOnlyFavorites, taskComplSource)); } })); }
private static GameObject AddPrefabToActiveView(string uiPrefabName, bool keepReferenceToEditorPrefab = true) { var go = GetSelectedCanvasChild().AddChild(ResourcesV2.LoadPrefab(uiPrefabName, keepReferenceToEditorPrefab)); SelectInHirarchyUi(go); return(go); }
public void TestScriptableObjectSingleton() { Injector injector = GetInjectorForTesting(); // The path to an ScriptableObject instance .asset file in a Resources folder: string pathToSoInstance1 = "MyExampleScriptableObject_Instance1.asset"; string someStringValue = "some string 123"; { // ScriptableObject instances can be accessed via ResourcesV2.LoadScriptableObjectInstance: var i = ResourcesV2.LoadScriptableObjectInstance <MyExampleScriptableObject>(pathToSoInstance1); Assert.IsNotNull(i); i.myString1 = someStringValue; } { // Load a ScriptableObject instance and set it as the singleton: var instance1 = ResourcesV2.LoadScriptableObjectInstance <MyExampleScriptableObject>(pathToSoInstance1); injector.SetSingleton(instance1); // Loading ScriptableObject instances multiple times via ResourcesV2.LoadScriptableObjectInstance will // result in the same instance each time, so the myString1 will be modified now: Assert.AreEqual(someStringValue, instance1.myString1); // Now that the singleton is set this instance is always returned for the ScriptableObject class: var instance1ViaIoC = injector.Get <MyExampleScriptableObject>(this); Assert.AreSame(instance1, instance1ViaIoC); Assert.AreEqual(someStringValue, instance1.myString1); } }
static void AddViewInViewStack() { RootCanvas.GetOrAddRootCanvas(); // Ensure there is a root canvas var view = AddViewToRootCanvas(ResourcesV2.LoadPrefab("Canvas/DefaultViewStackView")); view.name = "View " + (view.transform.GetSiblingIndex() + 1); }
public IEnumerator ExampleUsage() { MyUserUi userUiPresenter = new MyUserUi(); userUiPresenter.targetView = ResourcesV2.LoadPrefab("MyUserUi1"); { // Load a first user into the UI by passing it through the presenter: var user1 = new MyUserModel() { userName = "******", userAge = 4 }; yield return(userUiPresenter.LoadModelIntoView(user1).AsCoroutine()); Assert.AreEqual("Carl", userUiPresenter.NameInputField().text); Assert.AreEqual("4", userUiPresenter.AgeInputField().text); } yield return(new WaitForSeconds(0.5f)); // Load another user into the UI: { // Example of loading a second user in a separate asyn method "LoadUser2": yield return(LoadUser2(userUiPresenter).AsCoroutine()); Assert.AreEqual("55", userUiPresenter.AgeInputField().text); // The age of user 2 } }
private static SnackbarsUi InitSnackbarsUi() { var targetCanvas = CanvasFinder.GetOrAddRootCanvas().gameObject; var snackbarContainer = targetCanvas.AddChild(ResourcesV2.LoadPrefab("Messages/SnackbarContainer1")); return(snackbarContainer.GetOrAddComponent <SnackbarsUi>()); }
public void TestOnValueChangedListeners() { GameObject prefab = ResourcesV2.LoadPrefab("ExamplePrefab1.prefab"); Dictionary <string, Link> links = prefab.GetLinkMap(); { var toggle = links.Get <Toggle>("Toggle 1"); toggle.isOn = false; var counter = 0; toggle.SetOnValueChangedAction((isNowChecked) => { counter++; return(false); }); Assert.AreNotEqual(true, toggle.isOn); toggle.isOn = true; Assert.AreEqual(true, toggle.isOn); // The toggle was switched via code so the valueChanged listener should not be notified: Assert.AreEqual(0, counter); } { var input = links.Get <InputField>("Input Field 1"); input.text = ""; var counter = 0; input.SetOnValueChangedAction((newValue) => { counter++; return(false); }); Assert.AreNotEqual("1", input.text); input.text = "1"; Assert.AreEqual("1", input.text); // The input field was switched via code so the valueChanged listener should not be notified: Assert.AreEqual(0, counter); } }
public void TestLoadingPrefabs() { // Load the ExamplePrefab1.prefab located in Assets\Tests\TestLinking\Resources : Assert.IsNotNull(ResourcesV2.LoadPrefab("ExamplePrefab1")); // Loading a prefab that does not exist results in an error: AssertV2.Throws <Exception>(() => { ResourcesV2.LoadPrefab("ExamplePrefab2"); }); }
private void SyncUiColumnCountWithModelColumnCount(ImmutableDictionary <CellPos, Cell> Cells) { // Get the biggest column count in the cells model: int maxColumn = Cells.Keys.Map(k => k.columnNr).Max() + 1; // For each UI row check that they all have the same nr of UI cells: var rowNr = 0; foreach (var row in uiRows.GetChildren()) { var columnCount = row.GetChildCount() - 1; while (columnCount < maxColumn) { string columnId = CellPos.ToColumnName(columnCount); if (rowNr == 0) { var columnNameUi = row.AddChild(ResourcesV2.LoadPrefab("ColumnName")); columnNameUi.GetComponentInChildren <Text>().text = columnId; } else { var cellGo = row.AddChild(ResourcesV2.LoadPrefab("CellUiEntry")); var cell = cellGo.GetComponent <CellPresenter>(); cell.cellPos = new CellPos(columnId, rowNr); cell.store = store; } columnCount++; } rowNr++; } }
public void ExampleUsage1() { // Load a prefab that contains Link MonoBehaviours: GameObject prefab = ResourcesV2.LoadPrefab("ExamplePrefab1.prefab"); // Collect all Link MonoBehaviours in the prefab: Dictionary <string, Link> links = prefab.GetLinkMap(); // In the Prefab Link-Monos are placed in all GameObjects that need // to be accessed by the code. Links have a id to reference them: // Via the Link.id the objects can quickly be accessed: Assert.IsNotNull(links.Get <GameObject>("Button 1")); // The GameObject "Button 1" contains a Button-Mono that can be accessed: Button button1 = links.Get <Button>("Button 1"); button1.SetOnClickAction(delegate { Log.d("Button 1 clicked"); }); // The prefab also contains other Links in other places to quickly setup the UI: links.Get <Text>("Text 1").text = "Some text"; links.Get <Toggle>("Toggle 1").SetOnValueChangedAction((isNowChecked) => { Log.d("Toggle 1 is now " + (isNowChecked ? "checked" : "unchecked")); return(true); }); }
public void TestLinkMaps() { bool prefabLoadedEventReceived = false; EventBus.instance.Subscribe(new object(), IoEvents.PREFAB_LOADED, () => { prefabLoadedEventReceived = true; }); GameObject prefab = ResourcesV2.LoadPrefab("ExamplePrefab1.prefab"); Assert.IsTrue(prefabLoadedEventReceived); bool linkMapCreationEventReceived = false; EventBus.instance.Subscribe(new object(), LinkingEvents.LINK_MAP_CREATED, () => { linkMapCreationEventReceived = true; }); var links = prefab.GetLinkMap(); Assert.IsTrue(linkMapCreationEventReceived); Assert.IsNotNull(links.Get <Button>("Button 1")); Assert.IsNotNull(links.Get <GameObject>("Button 1")); AssertV2.Throws <Exception>(() => { links.Get <Button>("Button 2"); }); links.Get <Text>("Text 1").text = "Some text"; Assert.AreEqual("Some text", links.Get <Text>("Text 1").text); links.Get <Button>("Button 1").SetOnClickAction(delegate { Log.d("Button 1 clicked"); }); links.Get <Toggle>("Toggle 1").SetOnValueChangedAction((isNowChecked) => { Log.d("Toggle 1 is now " + (isNowChecked ? "checked" : "unchecked")); return(true); }); }
private static ToastsUi InitToastsUi() { var targetCanvas = CanvasFinder.GetOrAddRootCanvas().gameObject; var toastContainer = targetCanvas.AddChild(ResourcesV2.LoadPrefab("Messages/ToastContainer1")); return(toastContainer.GetOrAddComponent <ToastsUi>()); }
private static LogConsoleUi InitLogConsoleUi() { var targetCanvas = RootCanvas.GetOrAddRootCanvas().gameObject; var toastContainer = targetCanvas.AddChild(ResourcesV2.LoadPrefab("Messages/LogConsoleUi1")); return(toastContainer.GetComponentInChildren <LogConsoleUi>()); }
private static GameObject GetCanvasWithHighestSortingOrder() { var c = ResourcesV2.FindAllInScene <Canvas>(); c = c.Filter(x => x.gameObject.activeInHierarchy); return(c.OrderByDescending(x => x.sortingOrder).First().gameObject); }
private static ProgressUi NewGlobalProgressUi(ProgressManager pm, string prefab = "Progress/GlobalProgressOverlay1") { ProgressUi progressUi; var go = RootCanvas.GetOrAddRootCanvasV2().gameObject.AddChild(ResourcesV2.LoadPrefab(prefab)); progressUi = go.GetComponentInChildren <ProgressUi>(); progressUi.progressManager = pm; return(progressUi); }
private static void UpdateThemeColorMonos(NamedColor c) { var allAffected = ResourcesV2.FindAllInScene <ThemeColor>().Filter(x => x._colorName == c.colorName); foreach (var themeColor in allAffected) { themeColor.ApplyColor(c.colorValue); } }
public void TestLoadingBinaryDataFromResources() { var stream = ResourcesV2.LoadV2 <Stream>("SomeBinaryData") as MemoryStream; Assert.NotNull(stream); Assert.NotZero(stream.Length); var textFromBinaryFile = Encoding.UTF8.GetString(stream.ToArray()); Assert.AreEqual("Some data", textFromBinaryFile); }
public GameObject Show(string toastCaption, string toastMessage, int displayDurationInMs) { var newToast = ResourcesV2.LoadPrefab("Messages/Toast"); var toastUiElems = newToast.GetLinkMap(); InitText(toastUiElems, "Caption", toastCaption); InitText(toastUiElems, "Message", toastMessage); newToast.GetComponent <MonoBehaviour>().ExecuteDelayed(() => newToast.Destroy(), displayDurationInMs); toastsContainer.AddChild(newToast); return(newToast); }
public void ExampleUsage2() { // Load a ScriptableObject instance and set it as the singleton: var path = "MyExampleScriptableObject_Instance1.asset"; MyExampleScriptableObject x1 = ResourcesV2.LoadScriptableObjectInstance <MyExampleScriptableObject>(path); IoC.inject.SetSingleton(x1); // Now that the singleton is set this instance is always returned for the ScriptableObject class: MyExampleScriptableObject x2 = IoC.inject.Get <MyExampleScriptableObject>(this); Assert.AreSame(x1, x2); }
public static bool TryLoad <T>(string pathInResourcesFolder, out T result) { var caches = ResourcesV2.FindAllInScene <ResourceCache>(); foreach (var cache in caches) { if (cache.Find(pathInResourcesFolder, out T res)) { result = res; return(true); } } result = default(T); return(false); }
public static Canvas GetOrAddRootCanvas() { var roots = GetAllRootCanvases(); if (roots.IsNullOrEmpty()) { InitEventSystemIfNeeded(); return(ResourcesV2.LoadPrefab("Canvas/DefaultRootCanvas").GetComponent <Canvas>()); } var firstCanvasOnSceneRootLevel = roots.FirstOrDefault(x => x.gameObject.GetParent() == null); if (firstCanvasOnSceneRootLevel != null) { return(firstCanvasOnSceneRootLevel); } return(roots.First()); }
public void TestOnValueChangedListeners() { GameObject prefab = ResourcesV2.LoadPrefab("ExamplePrefab1.prefab"); Dictionary <string, Link> links = prefab.GetLinkMap(); { var toggle = links.Get <Toggle>("Toggle 1"); toggle.isOn = false; var counter = 0; toggle.SetOnValueChangedAction((isNowChecked) => { counter++; if (isNowChecked) { return(true); } return(false); }); Assert.AreNotEqual(true, toggle.isOn); toggle.isOn = true; Assert.AreEqual(true, toggle.isOn); Assert.AreEqual(1, counter); toggle.isOn = false; Assert.AreEqual(true, toggle.isOn); Assert.AreEqual(2, counter); } { var input = links.Get <InputField>("Input Field 1"); input.text = ""; var counter = 0; input.SetOnValueChangedAction((newValue) => { counter++; if (newValue == "1") { return(true); } Assert.AreEqual("2", newValue); return(false); }); Assert.AreNotEqual("1", input.text); input.text = "1"; Assert.AreEqual("1", input.text); Assert.AreEqual(1, counter); input.text = "2"; Assert.AreEqual("1", input.text); Assert.AreEqual(2, counter); } }
public virtual GameObject CreateIconEntryUi(ActionMenu menu, TaskCompletionSource <Entry> taskComplSource) { var entry = this; var iconGo = ResourcesV2.LoadPrefab(entry.iconModeEntryPrefabName); iconGo.GetComponentInChildren <Text>().text = entry.icon; var button = iconGo.GetComponentInChildren <Button>(); button.interactable = entry.isEnabled; iconGo.GetComponentInChildren <CanvasGroup>().enabled = !entry.isEnabled; button.SetOnClickAction(btnGo => { menu.clickedEntry = entry; taskComplSource.TrySetResult(entry); entry.onClicked.InvokeIfNotNull(btnGo); }); return(iconGo); }
private void SyncUiRowCountWithModelRowCount(ImmutableDictionary <CellPos, Cell> Cells) { // The biggest row count in the model: int maxRow = Cells.Keys.Map(k => k.rowNr).Max() + 1; // Add UI rows based on model row count (until UI has same row count): while (uiRows.GetChildCount() <= maxRow) { var rowUi = uiRows.AddChild(ResourcesV2.LoadPrefab("Row")); // Set the row nr in the row UIs first cell: rowUi.GetChild(0).GetComponentInChildren <Text>().text = "" + (uiRows.GetChildCount() - 1); } // Repeat until the count of UI rows is same as model // Removed UI rows that are no longer in the datamodel: while (uiRows.GetChildCount() > maxRow) { var lastRow = uiRows.GetChildrenIEnumerable().Last().Destroy(); } // Repeat until the count of UI rows is same as model }
public GameObject Show(string snackbarMsg, string buttonMsg, Action <GameObject> snackbarAction, int displayDurationInMs) { var newSnackbar = ResourcesV2.LoadPrefab("Messages/Snackbar"); var map = newSnackbar.GetLinkMap(); map.Get <Text>("Message").text = snackbarMsg; if (snackbarAction != null && !buttonMsg.IsNullOrEmpty()) { map.Get <Text>("SnackbarButton").text = buttonMsg; map.Get <Button>("SnackbarButton").SetOnClickAction(snackbarAction); } else { map.Get <GameObject>("SnackbarButton").Destroy(); } newSnackbar.GetComponent <MonoBehaviour>().ExecuteDelayed(() => newSnackbar.Destroy(), displayDurationInMs); snackbarsContainer.AddChild(newSnackbar); return(newSnackbar); }
public IEnumerator ExampleUsage3() { // Create an immutable datastore that will contain the data model in this example: var log = Middlewares.NewLoggingMiddleware <MyDataModel3>(); IDataStore <MyDataModel3> store = new DataStore <MyDataModel3>(MainReducer, new MyDataModel3(), log); IoC.inject.SetSingleton(store); // Create a presenter that connectes the model with the view (the Unity UI): var currentUserPresenter = new MyUserUi3(); // Set the target view by loading it from a prefab and setting the root GO: currentUserPresenter.targetView = ResourcesV2.LoadPrefab("MyUserUi1"); // Connect the model changes with the presenter: currentUserPresenter.ListenToStoreUpdates(store, state => state.currentUser); // Dispatch a first setUser action to update the UI: store.Dispatch(new ActionSetNewUser() { newUser = new MyUser3("Carl", 99) }); // Delay needed since the UI update simulates a delay too: yield return(new WaitForSeconds(0.1f)); // Check that the UI was automatically updated: Assert.AreEqual("Carl", currentUserPresenter.NameUi().text); Assert.AreEqual("99", currentUserPresenter.AgeUi().text); // Simulate that the user changed the model via the UI: store.Dispatch(new ActionUpdateUser() { target = store.GetState().currentUser, newValues = new MyUser3("Paul", 0) }); // Delay needed since the UI update simulates a delay too: yield return(new WaitForSeconds(0.1f)); // Check that the UI was automatically updated: Assert.AreEqual("Paul", currentUserPresenter.NameUi().text); Assert.AreEqual("0", currentUserPresenter.AgeUi().text); }
public virtual GameObject CreateIconEntryUi(ActionMenu menu, TaskCompletionSource <Entry> taskComplSource) { var entry = this; var iconGo = ResourcesV2.LoadPrefab(entry.iconModeEntryPrefabName); iconGo.GetComponentInChildren <Text>().text = entry.icon; var button = iconGo.GetComponentInChildren <Button>(); button.interactable = entry.isEnabled; iconGo.GetComponentInChildren <CanvasGroup>().enabled = !entry.isEnabled; button.SetOnClickAction(btnGo => { if (menu.entries.Contains(entry)) { menu.clickedEntry = entry; EventBus.instance.Publish(EventConsts.catUi + UiEvents.ACTION_MENU, menu.GetFullEntryId(entry)); taskComplSource.TrySetResult(entry); } entry.onClicked.InvokeIfNotNull(btnGo); }); return(iconGo); }
protected override void Start() { base.Start(); var assembliesToTest = anyTypeInTargetAssembly.Map(typeString => { try { return(Type.GetType(typeString).Assembly); } catch (Exception) { Log.e("Please check the XunitTestRunnerUi.anyTypeInTargetAssembly " + "list in your scene UI, it if's configured correctly. Could " + "not find type for string '" + typeString + "'", gameObject); return(null); } }); pm = new ProgressManager(); var progressUis = ResourcesV2.FindAllInScene <ProgressUi>(); AssertV2.IsFalse(progressUis.IsNullOrEmpty(), "progressUi"); foreach (var progrUi in progressUis) { progrUi.progressManager = pm; } // On the parent canvas level collect all links: var links = GetComponentInParent <Canvas>().gameObject.GetLinkMap(); var autoRunToggle = links.Get <Toggle>("AutoRunToggle"); autoRunToggle.isOn = autoRunAllTests; autoRunToggle.SetOnValueChangedAction(isChecked => { autoRunAllTests = isChecked; return(true); }); links.Get <Button>("StartButton").SetOnClickAction((_) => { CollectTests(assembliesToTest, links); UpdateSearchFilter(links); }); links.Get <InputField>("SearchInput").SetOnValueChangedActionThrottled((_) => { UpdateSearchFilter(links); }, 200); }