ProceduralImageEditorUtility.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using UnityEngine.UI;
  5. namespace UnityEditor.UI {
  6. /// <summary>
  7. /// This class adds a Menu Item "GameObject/UI/Procedural Image"
  8. /// Bahviour of this command is the same as with regular Images
  9. /// </summary>
  10. public class ProceduralImageEditorUtility {
  11. [MenuItem("GameObject/UI/Procedural Image")]
  12. public static void AddProceduralImage(){
  13. GameObject o = new GameObject ();
  14. o.AddComponent<ProceduralImage> ();
  15. o.name = "Procedural Image";
  16. if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponentInParent<Canvas> () != null) {
  17. o.transform.SetParent (Selection.activeGameObject.transform, false);
  18. Selection.activeGameObject = o;
  19. }
  20. /*else if (Selection.activeGameObject != null) {
  21. //selected GameObject is not child of canvas:
  22. }*/
  23. else {
  24. if(GameObject.FindObjectOfType<Canvas>()==null) {
  25. EditorApplication.ExecuteMenuItem("GameObject/UI/Canvas");
  26. }
  27. Canvas c = GameObject.FindObjectOfType<Canvas>();
  28. o.transform.SetParent (c.transform, false);
  29. Selection.activeGameObject = o;
  30. }
  31. }
  32. /// <summary>
  33. /// Replaces an Image Component with a Procedural Image Component.
  34. /// </summary>
  35. [MenuItem("CONTEXT/Image/Replace with Procedural Image")]
  36. public static void ReplaceWithProceduralImage(MenuCommand command){
  37. Image image = (Image)command.context;
  38. GameObject obj = image.gameObject;
  39. GameObject.DestroyImmediate (image);
  40. obj.AddComponent<ProceduralImage> ();
  41. }
  42. }
  43. }