Example.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Example : MonoBehaviour {
  4. public TreeViewControl m_myTreeView = null;
  5. // Use this for initialization
  6. public void Start () {
  7. // m_myTreeView = gameObject.GetComponent<TreeViewControl>();
  8. if (null == m_myTreeView)
  9. {
  10. Debug.LogError("Use the tree view menu to add the control");
  11. return;
  12. }
  13. PopulateExampleData(m_myTreeView);
  14. }
  15. public static void Handler(object sender, System.EventArgs args)
  16. {
  17. Debug.Log(string.Format("{0} detected: {1}", args.GetType().Name, (sender as TreeViewItem).Header));
  18. }
  19. static void AddHandlerEvent(out System.EventHandler handler)
  20. {
  21. handler = new System.EventHandler(Handler);
  22. }
  23. static void AddEvents(TreeViewItem item)
  24. {
  25. AddHandlerEvent(out item.Click);
  26. AddHandlerEvent(out item.Checked);
  27. AddHandlerEvent(out item.Unchecked);
  28. AddHandlerEvent(out item.Selected);
  29. AddHandlerEvent(out item.Unselected);
  30. }
  31. public static void PopulateExampleData(TreeViewControl item)
  32. {
  33. item.Width = 600;
  34. item.Height = 500;
  35. item.Header = "Example.cs populated this treeview control";
  36. AddEvents(item.RootItem);
  37. TreeViewItem item1 = item.RootItem.AddItem("You can also add a tree view control");
  38. AddEvents(item1);
  39. AddEvents(item1.AddItem("to any existing game object by"));
  40. AddEvents(item1.AddItem("selecting the game object and"));
  41. AddEvents(item1.AddItem("using the menu item"));
  42. TreeViewItem games = item1.AddItem("TreeView->Add Tree View to selected.", false);
  43. AddEvents(games);
  44. AddEvents(games.AddItem("You can also drag and drop the"));
  45. AddEvents(games.AddItem("TreeViewControl script onto a"));
  46. AddEvents(games.AddItem("game object."));
  47. AddEvents(games.AddItem("New checked and", false, true));
  48. AddEvents(games.AddItem("unchecked checkboxes", false, false));
  49. }
  50. public void OnGUI()
  51. {
  52. if (null != m_myTreeView &&
  53. null != m_myTreeView.SelectedItem &&
  54. !string.IsNullOrEmpty(m_myTreeView.SelectedItem.Header))
  55. {
  56. GUILayout.Label(string.Format("Example selected: {0}", m_myTreeView.SelectedItem.Header));
  57. }
  58. }
  59. }