IArticyFlowPlayerCallbacksOnBranchesUpdated Method |
Namespace: Articy.Unity
void OnBranchesUpdated( IList<Branch> aBranches )
All branches found from the current object are passed into this method. How you implement this method is highly depending on your use case, you could use this to build a ui showing all available dialogue choices in a game dialogue for example. If the flow player ended in a dead end, an empty collection is passed.
using System.Collections.Generic; using Articy.Unity; using Articy.Unity.Interfaces; using UnityEditor; using UnityEngine; using UnityEngine.UI; public class BranchingExampleScript : MonoBehaviour, IArticyFlowPlayerCallbacks { public void OnFlowPlayerPaused(IFlowObject aObject) { var dspName = aObject as IObjectWithDisplayName; if (dspName != null) Debug.Log(dspName.DisplayName); var menuTxt = aObject as IObjectWithMenuText; if (menuTxt != null) Debug.Log(menuTxt.MenuText); var text = aObject as IObjectWithText; if (text != null) Debug.Log(text.Text); } // this will be called when we have new branches and we want to display each branch as a button on the screen public void OnBranchesUpdated(IList<Branch> aBranches) { // delete all old buttons from previous branches var oldButtons = FindObjectsOfType<Button>(); foreach (var btn in oldButtons) Destroy(btn.gameObject); var pos = new Vector3(0, 0, 0); // now we create for every branch a button foreach (var branch in aBranches) { // if a branch is not valid, we don't want to create a button for it if (!branch.IsValid) continue; // build a label depending on the target string buttonLabel = "Unknown"; // to give the button a nice label, we use the object interfaces and check which one is the most appropriate var dspName = branch.Target as IObjectWithDisplayName; if (dspName != null) buttonLabel = dspName.DisplayName; var text = branch.Target as IObjectWithText; if (text != null) buttonLabel = text.Text; var menuTxt = branch.Target as IObjectWithMenuText; if (menuTxt != null) buttonLabel = menuTxt.MenuText; Branch localBranch = branch; // necessary when we iterate over it (modified closure) // create the button CreateButton(pos, buttonLabel, () => ButtonClick(localBranch.BranchId)); // modify our position for the next button, so we create a simple vertical list of buttons. pos = new Vector3(0, pos.y - 64, 0); } // this is very rough code and would in its current form delete everytime all buttons found in the scene // The code would be a lot easier and cleaner with prefabs, but then would be difficult to show in a script only example. } // called when a button is clicked, this makes sure t void ButtonClick(int aId) { // if one of our branch button is clicked, we just tell our flow player to continue with the traversal GetComponent<ArticyFlowPlayer>().Play(aId); } // create a new button with label, position and click handler public Button CreateButton(Vector3 position, string aLabel, UnityEngine.Events.UnityAction method) { var buttonObj = new GameObject("Button"); var buttonTransform = buttonObj.AddComponent<RectTransform>(); var btn = buttonObj.AddComponent<Button>(); buttonObj.transform.position = position; buttonTransform.sizeDelta = new Vector2(200, 64); buttonTransform.SetParent(FindObjectOfType<Canvas>().transform, false); var image = buttonObj.AddComponent<Image>(); image.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>("UI/Skin/UISprite.psd"); image.type = Image.Type.Sliced; image.color = Color.white; var textObj = new GameObject("ButtonLabel"); var textTransform = textObj.AddComponent<RectTransform>(); var text = textObj.AddComponent<Text>(); text.color = Color.black; text.font = Resources.GetBuiltinResource<Font>("Arial.ttf"); text.alignment = TextAnchor.MiddleCenter; textTransform.anchorMin = Vector2.zero; textTransform.anchorMax = Vector2.one; textTransform.sizeDelta = Vector2.zero; GameObjectUtility.SetParentAndAlign(textObj, buttonObj); btn.onClick.AddListener(method); text.text = aLabel; return btn; } }