Braltor

Unity - Cubicos - Editor de cubos

Sep 16th, 2016
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public class Cubicos : EditorWindow
  6. {
  7.     static Vector3 p;       // posición
  8.     static GameObject c;    // cubicos
  9.  
  10.     [MenuItem("Window/Cubicos")]
  11.     static void i() // inicia
  12.     {
  13.         EditorWindow.GetWindow<Cubicos>().Show();
  14.     }
  15.  
  16.     void OnEnable()
  17.     {
  18.         SceneView.onSceneGUIDelegate -= this.OnSceneGUI;
  19.         SceneView.onSceneGUIDelegate += this.OnSceneGUI;
  20.  
  21.         p = Vector3.zero;
  22.     }
  23.  
  24.     void OnDestroy()
  25.     {
  26.         UnityEditor.SceneView.onSceneGUIDelegate -= this.OnSceneGUI;
  27.  
  28.         if (c != null)
  29.             DestroyImmediate(c.gameObject);
  30.     }
  31.  
  32.     void OnGUI()
  33.     {
  34.         pc();
  35.  
  36.         GUILayout.BeginArea(new Rect(4, 4, 150, 150));
  37.         {
  38.             GUILayout.BeginVertical();
  39.             {
  40.                 GUILayout.BeginHorizontal();
  41.                 {
  42.                     if (GUILayout.Button("* U"))
  43.                         m(Vector3.forward);
  44.                     GUILayout.FlexibleSpace();
  45.                     if (GUILayout.Button("^ I"))
  46.                         m(Vector3.up);
  47.                     GUILayout.FlexibleSpace();
  48.                 }
  49.                 GUILayout.EndHorizontal();
  50.  
  51.                 GUILayout.BeginHorizontal();
  52.                 {
  53.                     GUILayout.FlexibleSpace();
  54.                     if (GUILayout.Button("< J"))
  55.                         m(Vector3.left);
  56.                     GUILayout.FlexibleSpace();
  57.                     if (GUILayout.Button("> L"))
  58.                         m(Vector3.right);
  59.                 }
  60.                 GUILayout.EndHorizontal();
  61.  
  62.                 GUILayout.BeginHorizontal();
  63.                 {
  64.                     if (GUILayout.Button("o M"))
  65.                         m(Vector3.back);
  66.                     GUILayout.FlexibleSpace();
  67.                     if (GUILayout.Button("v K"))
  68.                         m(Vector3.down);
  69.                     GUILayout.FlexibleSpace();
  70.                 }
  71.                 GUILayout.EndHorizontal();
  72.  
  73.                 GUILayout.Space(8);
  74.  
  75.                 GUILayout.BeginHorizontal();
  76.                 {
  77.                     if (GUILayout.Button("+ spc"))
  78.                         d();
  79.                     if (GUILayout.Button("- del"))
  80.                         b();
  81.                 }
  82.                 GUILayout.EndHorizontal();
  83.             }
  84.             GUILayout.EndVertical();
  85.         }
  86.         GUILayout.EndArea();
  87.  
  88.         c.transform.position = p;
  89.     }
  90.  
  91.     void OnSceneGUI(
  92.         SceneView sv)
  93.     {
  94.         pc();
  95.  
  96.         Event e = Event.current;
  97.  
  98.         if (e.type == EventType.KeyDown)
  99.         {
  100.             switch (e.keyCode)
  101.             {
  102.                 case KeyCode.I:
  103.                 m(Vector3.up);
  104.                 break;
  105.  
  106.                 case KeyCode.J:
  107.                 m(Vector3.left);
  108.                 break;
  109.  
  110.                 case KeyCode.K:
  111.                 m(Vector3.down);
  112.                 break;
  113.  
  114.                 case KeyCode.L:
  115.                 m(Vector3.right);
  116.                 break;
  117.  
  118.                 case KeyCode.U:
  119.                 m(Vector3.forward);
  120.                 break;
  121.  
  122.                 case KeyCode.M:
  123.                 m(Vector3.back);
  124.                 break;
  125.  
  126.                 case KeyCode.Space:
  127.                 d();
  128.                 break;
  129.  
  130.                 case KeyCode.Delete:
  131.                 case KeyCode.Backspace:
  132.                 b();
  133.                 break;
  134.             }
  135.  
  136.             c.transform.position = p;
  137.         }
  138.     }
  139.  
  140.     void pc()   // prepara cúbicos
  141.     {
  142.         if (c == null)
  143.         {
  144.             c = GameObject.Find("[Cubicos]");
  145.  
  146.             if (c == null)
  147.             {
  148.                 c = GameObject.CreatePrimitive(PrimitiveType.Cube);
  149.                 c.name = "[Cubicos]";
  150.                 c.GetComponent<Renderer>().enabled = false;
  151.                 c.GetComponent<Collider>().isTrigger = true;
  152.             }
  153.         }
  154.  
  155.         Selection.activeObject = c;
  156.     }
  157.  
  158.     void m( // mueve
  159.         Vector3 d)  // dirección
  160.     {
  161.         p += d;
  162.     }
  163.  
  164.     void d()    // dibuja
  165.     {
  166.         var mr = Transform.FindObjectsOfType<MeshRenderer>();   // Transforms
  167.  
  168.         foreach (var imr in mr) // índice de MeshRenderer
  169.         {
  170.             if (imr.gameObject != c && Vector3.Distance(imr.transform.position, p) < 0.001f)
  171.                 return;
  172.         }
  173.  
  174.         GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = p;
  175.     }
  176.  
  177.     void b()    // borra
  178.     {
  179.         var mr = Transform.FindObjectsOfType<MeshRenderer>();   // Transforms
  180.  
  181.         foreach (var imr in mr) // índice de MeshRenderer
  182.         {
  183.             if (imr.gameObject != c && Vector3.Distance(imr.transform.position, p) < 0.001f)
  184.                 DestroyImmediate(imr.gameObject);
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment