Advertisement
jackd5011

Unity Hierarchy Section Creator

Jun 4th, 2019 (edited)
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. /**
  5.  *  Hierarchy Section Creator
  6.  *  Created by Jack Davenport (@memedealer#6607) 2019
  7.  *  Updated: 16/9/2020
  8.  *  
  9.  *  HOW TO USE:
  10.  *  1) Create a folder in your Assets called "Editor"
  11.  *  2) Download this script, save it as "HierarchySection.cs"
  12.  *  3) Place it in the Editor folder you created
  13.  *  4) Go to "Window > General > Create Hierarchy Section" to use
  14.  */
  15. namespace JackDavenport.Editor {
  16.  
  17.     public class HierarchySection : EditorWindow
  18.     {
  19.         string spacerName = "New Section";
  20.         int width = 30;
  21.  
  22.         void OnGUI()
  23.         {
  24.             spacerName = EditorGUILayout.TextField("Section Name", spacerName);
  25.             width = Mathf.Max(1, EditorGUILayout.IntField("Width", width));
  26.  
  27.             EditorGUILayout.Space();
  28.  
  29.             string shortName = spacerName.Trim();
  30.             string error = null;
  31.  
  32.             if(shortName.Length <= 0) {
  33.                 error = "Please enter a name!";
  34.             } else if(shortName.Length > width) {
  35.                 error = "Name is longer than allowed width!";
  36.             }
  37.  
  38.             if (error != null) {
  39.                 EditorGUILayout.HelpBox(error, MessageType.Error);
  40.             } else {
  41.                 shortName = GenerateText(shortName);
  42.                 EditorGUILayout.LabelField("Preview", EditorStyles.boldLabel);
  43.                 EditorGUILayout.LabelField(shortName);
  44.             }
  45.  
  46.             EditorGUILayout.Space();
  47.  
  48.             EditorGUI.BeginDisabledGroup(error != null);
  49.             if (GUILayout.Button("Create")) {
  50.                 CreateItem(shortName);
  51.             }
  52.             EditorGUI.EndDisabledGroup();
  53.         }
  54.  
  55.         void CreateItem(string objectName)
  56.         {
  57.             GameObject obj = new GameObject(objectName);
  58.             obj.tag = "EditorOnly";
  59.             obj.isStatic = true;
  60.         }
  61.  
  62.         string GenerateText(string name)
  63.         {
  64.             int padding = (width - name.Length) / 2;
  65.             string text = "|";
  66.  
  67.             for (int i = 0; i < padding; i++) {
  68.                 text += "-";
  69.             }
  70.             text += name;
  71.             for (int i = 0; i < padding; i++) {
  72.                 text += "-";
  73.             }
  74.             return text + "|";
  75.         }
  76.  
  77.         [MenuItem("Window/General/Create Hierarchy Section")]
  78.         static void OpenWindow()
  79.         {
  80.             GetWindow<HierarchySection>("Create Hierarchy Section").Show();
  81.         }
  82.  
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement