public static GenericMenu GetMenu(Action <Type> callback, Type currentType) { GenericMenu menu = new GenericMenu(); //return value Dictionary <string, int> seperatorCheck = new Dictionary <string, int>(); //holds last entry to a sub menu //Loop through all Variable Types foreach (KeyValuePair <Type, VariableMenuAttribute> pair in CachedAttributes.OrderBy(p => p.Value, new VariableAttributeComparer())) { VariableMenuAttribute current = pair.Value; string path = pair.Value.menuName; string subMenu = pair.Value.GetSubMenuOnly(); //if first instance of submenu add if (!seperatorCheck.ContainsKey(subMenu)) { seperatorCheck.Add(current.GetSubMenuOnly(), current.order); } //Check if there is a gap of 10 between this and the last attribute, if so add a seperator if (current.order - seperatorCheck[subMenu] >= 10) { menu.AddSeparator(subMenu); } seperatorCheck[subMenu] = current.order; //Add item to menu, highlight if it is the current type menu.AddItem(new UnityEngine.GUIContent(path), currentType?.Equals(pair.Key) ?? false, (p => callback((Type)p)), pair.Key); } //Add seperator and create new button at end menu.AddSeparator(""); menu.AddItem(new UnityEngine.GUIContent("Create New"), false, VariableCreator.Open); return(menu); }
public void CreateVariableType(Type type, string folderPath, VariableMenuAttribute attribute, string nameSpace = null) { string variableName = type.Name + "Variable"; string fullPath = Path.Combine(folderPath, variableName + ".cs"); DirectoryInfo di = Directory.CreateDirectory(folderPath); // Create a file to write to. using (StreamWriter sw = File.CreateText(fullPath)) { sw.WriteLine("using Variables;"); if (type?.Namespace != null && type.Namespace.Length > 0) { sw.WriteLine($"using {type.Namespace};"); } sw.WriteLine(""); if (!string.IsNullOrEmpty(nameSpace)) { sw.WriteLine($"namespace {nameSpace}"); sw.WriteLine("{"); } sw.WriteLine($"[VariableMenu(menuName = \"{attribute.menuName}\", order = {attribute.order})]"); sw.WriteLine($"public class {variableName} : Variable<{type.Name}> {{ }}"); if (!string.IsNullOrEmpty(nameSpace)) { sw.WriteLine("}"); } } AssetDatabase.Refresh(); Debug.Log($"Created : {fullPath}"); }
private static void UpdateAttributes(object sender) { CachedAttributes = VariableMenuAttribute.GetAll(); }