/// <summary> /// This attempts to restore the last set manager on the target. /// </summary> /// <param name="target">The target to restore.</param> /// <returns>Whether or not the target was restored.</returns> public static bool RestoreVisManagerTarget(IVisManagerTarget target) { //make sure the vis manager is set, and if not, check if there //is a name set and try and find that object as the manager if (/*target.Manager == null && */ target.LastManagerName != null && target.LastManagerName.Length > 0) { //try to get that game object GameObject managerObject = GameObject.Find(target.LastManagerName); if (managerObject != null) { //try to get the manager of it target.Manager = managerObject.GetComponent <VisManager>(); if (target.Manager != null) { return(true); } } } return(false); }
/// <summary> /// This attempts to restore the last set manager on the target. /// </summary> /// <param name="target">The target to restore.</param> /// <returns>Whether or not the target was restored.</returns> public static bool RestoreVisManagerTarget(IVisManagerTarget target) { //make sure the vis manager is set, and if not, check if there //is a name set and try and find that object as the manager if (/*target.Manager == null && */target.LastManagerName != null && target.LastManagerName.Length > 0) { //try to get that game object GameObject managerObject = GameObject.Find(target.LastManagerName); if (managerObject != null) { //try to get the manager of it target.Manager = managerObject.GetComponent<VisManager>(); if (target.Manager != null) return true; } } return false; }
/// <summary> /// This displays the drop down for selecting a manager from the inspector. /// </summary> /// <param name="managerTarget">The manager target to set the manager for.</param> /// <returns>Whether or not a manager is currently set.</returns> public bool DisplayIVisManagerTargetGUI(IVisManagerTarget managerTarget) { if (managerTarget != null) { if (managerTarget is MonoBehaviour) { GameObject gameObject = (managerTarget as MonoBehaviour).gameObject; if (managerTarget.Manager == null || managerTarget.Manager.gameObject != gameObject) { VisManager manager = gameObject.GetComponent<VisManager>(); if (manager != null) { managerTarget.Manager = manager; return true; } } else if (managerTarget.Manager != null && managerTarget.Manager.gameObject == gameObject) { return true; } } bool result = false; EditorGUILayout.BeginHorizontal(); //make sure and try to restore it first VisManager.RestoreVisManagerTarget(managerTarget); Object[] visManagers = GameObject.FindObjectsOfType(typeof(VisManager)); if (visManagers.Length > 0) { //create list of vis manager names and a dictionary to map IDs, and sort it List<string> sortedNames = new List<string>(visManagers.Length); Dictionary<string, int> nameToIndexMap = new Dictionary<string, int>(visManagers.Length); for (int i = 0; i < visManagers.Length; i++) { string name = (visManagers[i] as VisManager).name; if (nameToIndexMap.ContainsKey(name) && ((visManagers[nameToIndexMap[name]] as VisManager).Controllers.Count < (visManagers[i] as VisManager).Controllers.Count || (visManagers[nameToIndexMap[name]] as VisManager).DataGroups.Count < (visManagers[i] as VisManager).DataGroups.Count)) {//override index nameToIndexMap[name] = i; } else {//add new sortedNames.Add((visManagers[i] as VisManager).name); nameToIndexMap.Add((visManagers[i] as VisManager).name, i); } } sortedNames.Sort(); //create array of names and set current index int currentIndex = 0; string[] displayedNames = new string[visManagers.Length + 1]; displayedNames[0] = "None"; for (int i = 0; i < sortedNames.Count; i++) { displayedNames[i + 1] = sortedNames[i]; if (managerTarget.Manager == visManagers[nameToIndexMap[sortedNames[i]]]) currentIndex = i + 1; } //display popup int newIndex = EditorGUILayout.Popup(" Manager", currentIndex, displayedNames); //set new vis manager if the index has changed if (newIndex != currentIndex) { if (newIndex == 0) managerTarget.Manager = null; else { string newName = sortedNames[newIndex - 1]; int remappedIndex = nameToIndexMap[newName]; managerTarget.Manager = visManagers[remappedIndex] as VisManager; } EditorUtility.SetDirty(target); } result = managerTarget.Manager != null; } else { if (managerTarget.LastManagerName != null && managerTarget.LastManagerName.Length > 0) EditorGUILayout.LabelField(" Manager", managerTarget.LastManagerName + " (not found, try selecting the Object with this Manager)"); else EditorGUILayout.LabelField(" Manager", "NO MANAGERS FOUND!"); result = false; } managerTarget.Manager = (VisManager)EditorGUILayout.ObjectField(managerTarget.Manager, typeof(VisManager), true, GUILayout.Width(Screen.width * 0.2f)); EditorGUILayout.EndHorizontal(); return result; } return false; }
/// <summary> /// This displays the drop down for selecting a manager from the inspector. /// </summary> /// <param name="managerTarget">The manager target to set the manager for.</param> /// <returns>Whether or not a manager is currently set.</returns> public bool DisplayIVisManagerTargetGUI(IVisManagerTarget managerTarget) { if (managerTarget != null) { if (managerTarget is MonoBehaviour) { GameObject gameObject = (managerTarget as MonoBehaviour).gameObject; if (managerTarget.Manager == null || managerTarget.Manager.gameObject != gameObject) { VisManager manager = gameObject.GetComponent <VisManager>(); if (manager != null) { managerTarget.Manager = manager; return(true); } } else if (managerTarget.Manager != null && managerTarget.Manager.gameObject == gameObject) { return(true); } } bool result = false; EditorGUILayout.BeginHorizontal(); //make sure and try to restore it first VisManager.RestoreVisManagerTarget(managerTarget); Object[] visManagers = GameObject.FindObjectsOfType(typeof(VisManager)); if (visManagers.Length > 0) { //create list of vis manager names and a dictionary to map IDs, and sort it List <string> sortedNames = new List <string>(visManagers.Length); Dictionary <string, int> nameToIndexMap = new Dictionary <string, int>(visManagers.Length); for (int i = 0; i < visManagers.Length; i++) { string name = (visManagers[i] as VisManager).name; if (nameToIndexMap.ContainsKey(name) && ((visManagers[nameToIndexMap[name]] as VisManager).Controllers.Count < (visManagers[i] as VisManager).Controllers.Count || (visManagers[nameToIndexMap[name]] as VisManager).DataGroups.Count < (visManagers[i] as VisManager).DataGroups.Count)) {//override index nameToIndexMap[name] = i; } else {//add new sortedNames.Add((visManagers[i] as VisManager).name); nameToIndexMap.Add((visManagers[i] as VisManager).name, i); } } sortedNames.Sort(); //create array of names and set current index int currentIndex = 0; string[] displayedNames = new string[visManagers.Length + 1]; displayedNames[0] = "None"; for (int i = 0; i < sortedNames.Count; i++) { displayedNames[i + 1] = sortedNames[i]; if (managerTarget.Manager == visManagers[nameToIndexMap[sortedNames[i]]]) { currentIndex = i + 1; } } //display popup int newIndex = EditorGUILayout.Popup(" Manager", currentIndex, displayedNames); //set new vis manager if the index has changed if (newIndex != currentIndex) { if (newIndex == 0) { managerTarget.Manager = null; } else { string newName = sortedNames[newIndex - 1]; int remappedIndex = nameToIndexMap[newName]; managerTarget.Manager = visManagers[remappedIndex] as VisManager; } EditorUtility.SetDirty(target); } result = managerTarget.Manager != null; } else { if (managerTarget.LastManagerName != null && managerTarget.LastManagerName.Length > 0) { EditorGUILayout.LabelField(" Manager", managerTarget.LastManagerName + " (not found, try selecting the Object with this Manager)"); } else { EditorGUILayout.LabelField(" Manager", "NO MANAGERS FOUND!"); } result = false; } managerTarget.Manager = (VisManager)EditorGUILayout.ObjectField(managerTarget.Manager, typeof(VisManager), true, GUILayout.Width(Screen.width * 0.2f)); EditorGUILayout.EndHorizontal(); return(result); } return(false); }