Advertisement
Pro_Unit

ScriptGenerationWizard

Mar 2nd, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.IO.Pipes;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text.RegularExpressions;
  9.  
  10. using UnityEditor;
  11.  
  12. using UnityEngine;
  13. namespace GameCore.CodeGeneration
  14. {
  15.     public class ScriptGenerationWizard : ScriptableWizard
  16.     {
  17.         [SerializeField] ScriptGenerator scriptGenerator;
  18.         [SerializeField] KeyValuePairStringType[] KeysValues;
  19.  
  20.         [MenuItem ("Game/Generation/Script")]
  21.         static void CreateWizard ()
  22.         {
  23.             var wizard = ScriptableWizard.DisplayWizard<ScriptGenerationWizard> ("Create Scripts", "Generate", "Get keys");
  24.         }
  25.  
  26.         void OnWizardCreate ()
  27.         {
  28.             if (scriptGenerator && !ValuesIsEmpty)
  29.             {
  30.                 Dictionary<string, string> keySelector = KeysValues.ToDictionary (kv => kv.Key, kv => kv.Value);
  31.  
  32.                 if (ScriptGenerator.generatedScripts == null) ScriptGenerator.generatedScripts = new List<ScriptGenerator> ();
  33.                 else ScriptGenerator.generatedScripts.Clear ();
  34.  
  35.                 var pahtText = scriptGenerator.Generate (keySelector);
  36.                 ScriptGenerator.generatedScripts.Clear ();
  37.  
  38.                 Debug.Log (pahtText.Log ());
  39.  
  40.                 foreach (var pair in pahtText)
  41.                 {
  42.                     Tools.ValidatePath (
  43.                         Path.GetDirectoryName ("Assets" + pair.Key.Replace (Application.dataPath, string.Empty)).Replace ('\\', '/')
  44.                     );
  45.                     File.WriteAllText (pair.Key, pair.Value);
  46.                     AssetDatabase.Refresh ();
  47.                 }
  48.             }
  49.         }
  50.  
  51.         void OnWizardUpdate ()
  52.         {
  53.             if (!scriptGenerator)
  54.             {
  55.                 errorString = "Choose the Script type generation";
  56.                 helpString = string.Empty;
  57.                 isValid = false;
  58.             }
  59.             else
  60.             if (KeysIsNullOrEmpty)
  61.             {
  62.                 errorString = "Press Get keys";
  63.                 helpString = string.Empty;
  64.                 isValid = true;
  65.             }
  66.             else
  67.             if (ValuesIsEmpty)
  68.             {
  69.                 errorString = "Fill the keys.";
  70.                 helpString = string.Empty;
  71.                 isValid = false;
  72.             }
  73.             else
  74.             {
  75.                 errorString = string.Empty;
  76.                 helpString = "Ready To Generate. Press to \"Generate\" ";
  77.                 isValid = true;
  78.             }
  79.         }
  80.  
  81.         private bool ValuesIsEmpty => !KeysIsNullOrEmpty && KeysValues.ToList ().Any (kv => kv.Value == string.Empty);
  82.  
  83.         private bool KeysIsNullOrEmpty => KeysValues == null || KeysValues.Length == 0;
  84.  
  85.         void OnWizardOtherButton ()
  86.         {
  87.             if (!scriptGenerator) return;
  88.  
  89.             KeysValues = scriptGenerator
  90.                 .GetKeysWithdependies ()
  91.                 .Select (k => new KeyValuePairStringType (k, string.Empty))
  92.                 .ToArray ();
  93.         }
  94.     }
  95.  
  96.     [Serializable]
  97.     public class KeyValuePairStringType
  98.     {
  99.         [SerializeField] string key;
  100.         [SerializeField] string value;
  101.  
  102.         public string Key => key;
  103.         public string Value => value;
  104.  
  105.         public KeyValuePairStringType (string key, string value)
  106.         {
  107.             this.key = key;
  108.             this.value = value;
  109.         }
  110.  
  111.         public KeyValuePairStringType ()
  112.         {
  113.             key = "$_$";
  114.             value = string.Empty;
  115.         }
  116.     }
  117.  
  118.     [CustomPropertyDrawer (typeof (KeyValuePairStringType))]
  119.     public class KeyValuePairStringTypeDrawer : PropertyDrawer
  120.     {
  121.         public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
  122.         {
  123.             var key = property.FindPropertyRelative ("key");
  124.             var value = property.FindPropertyRelative ("value");
  125.             var rect = EditorGUI.PrefixLabel (position, new GUIContent (key.stringValue));
  126.             EditorGUI.PropertyField (rect, value, GUIContent.none);
  127.         }
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement