Tell you what I’ll do I’ll post the scripts for the Menu of scriptable objects:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
/* This adds the MenuItems/Setup dropdown and when clicked creates the Resources/MenuItems path
* and the MenuItemManager Empty GameObject for us to hold the references in
*
* Essentially it just makes it easier to keep everything neat when dealing with numerous scenes
*/
public class SetupMenu : Editor {
[MenuItem("MenuItems/Setup")]
static void Init()
{
if(!Directory.Exists(Application.dataPath + "/Resources/MenuItems"))
Directory.CreateDirectory(Application.dataPath + "/Resources/MenuItems");
GameObject alchemyManager = new GameObject ("MenuItemManager");
alchemyManager.AddComponent ("MenuItemManager");
}
}
That creates the gameObject that stores the list and creates the resources folder to store the scriptable objects in.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MI: ScriptableObject {
public enum MIType
{
Contact,
Ranged,
Self
}
public string MIName;
public int MIManaCost;
public int MIDamage;
public string MIDescription;
public Sprite MIIcon;
public MIType MI_type;
}
That’s the object feilds
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MenuItemManager : MonoBehaviour {
public List<MI> menuItemList = new List<MI>();
}
The MenuItemManager script that’s put on the empty gameObject and stores the list.
Finally the script that does the bulk of the work:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CreateMenuItem : EditorWindow {
[MenuItem("MenuItems/Create Menu Item")]
static void Init()
{
CreateMenuItem menuItemWindow = (CreateMenuItem)EditorWindow.CreateInstance(typeof(CreateMenuItem));
menuItemWindow.Show();
}
MI tempMenuItem = null;
MenuItemManager menuItemManager = null;
void OnGUI()
{
if(menuItemManager == null){
//Instantiating our spell manager for adding our spells to the spell list.
menuItemManager = GameObject.Find("MenuItemManager").GetComponent<MenuItemManager>();
}
if(tempMenuItem)
{
tempMenuItem.MIName = EditorGUILayout.TextField("Name",tempMenuItem.MIName);
tempMenuItem.MI_type = (MI.MIType)EditorGUILayout.EnumPopup("Type", tempMenuItem.MI_type);
tempMenuItem.MIDescription = EditorGUILayout.TextField("Description",tempMenuItem.MIDescription);
tempMenuItem.MIIcon = (Sprite)EditorGUILayout.ObjectField("Icon",tempMenuItem.MIIcon,typeof(Sprite),false);
tempMenuItem.MIManaCost = EditorGUILayout.IntField("Mana Cost",tempMenuItem.MIManaCost);
tempMenuItem.MIDamage = EditorGUILayout.IntField ("Damage", tempMenuItem.MIDamage);
//tempAlchemyItem.attributeNeoAffected = (AttributeName)EditorGUILayout.EnumPopup("Neophyte Attribute Affected", tempAlchemyItem.attributeNeoAffected);
}
if(tempMenuItem == null){
if(GUILayout.Button("Create MenuItem"))
{
//This line of code instantiates of our temporary spell.
tempMenuItem = (MI)ScriptableObject.CreateInstance<MI>();
}
}
else
{
if(GUILayout.Button("Create Scriptable Object"))
{
//Creates a scriptable object that contains all of our spell properties.
AssetDatabase.CreateAsset(tempMenuItem,
"Assets/Resources/MenuItems/" + tempMenuItem.MIName + ".asset");
AssetDatabase.SaveAssets();
//Adding our spell to spellDatabase and we can reach all of spells in game.
menuItemManager.menuItemList.Add(tempMenuItem);
Selection.activeObject = tempMenuItem;
tempMenuItem = null;
}
if(GUILayout.Button("Reset"))
{
//Reseting spell properties.
Reset();
}
}
}
void Reset()
{
if(tempMenuItem)
{
tempMenuItem.MIName = "";
tempMenuItem.MIDescription = "";
tempMenuItem.MIIcon = null;
tempMenuItem.MIDamage = 0;
tempMenuItem.MIManaCost = 0;
}
}
}
You should be able to pick it apart to get what you want. But just put those scripts into an empty project to test how it works.
EDIT:
Just realized that’s still not showing you how to reference the items. I have a panel with various Text/Image/Toggle objects on it. That’s saved as a prefab and when the prefab is instantiated I call it newItem.
Before you loop through the items in the list you’ll need to reference the list …
myMenuItemManager = GameObject.Find ("MenuItemManager").GetComponent<MenuItemManager>();
Then after instantiating the item you can reference the child object of the panel (or button in your case) a couple of ways. I’ll not post all of it just a sample.
// test what the children of this panel are, change the number in the GetChild and name each item in the
// inspector so we can see what the child is.
string testThis = newItem.gameObject.transform.GetChild(0).name.ToString();
Debug.Log (newItem.name + " child 0 " + testThis);
// Or you can reference the child object by its name with newItem.gameObject.transform.Find("Name").name.ToString();
// The Icon is stored in child 0 #### May be different on your setup so test with the lines above ###
newItem.gameObject.transform.GetChild(0).GetComponent<Image>().sprite = myMenuItemManager.menuItemList[myCount - 1].MIIcon;
newItem.gameObject.transform.GetChild(1).GetComponent<Text>().text = myMenuItemManager.menuItemList[myCount - 1].MIName;
newItem.gameObject.transform.GetChild(2).GetComponent<Text>().text = myMenuItemManager.menuItemList[myCount - 1].MIDescription;
Hope that helps, if you want the package I’m working on that’s fine, it’s not finished as I’m really doing a test of tabstrip and scrollable menu to show what it can do. Basically if you want it I’ll post it on dropbox but ignore the buttons at the top as they do nothing at the moment.