JeCodeLeSoir

ManagerListObject

Jul 13th, 2023
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6.  
  7. public static class ManagerListObject
  8. {
  9.  
  10.     /* Dossier a créer
  11.      * --> Asset/Resources/ScriptObjects/Objects
  12.      */
  13.     /* Attention faut mettre MyObjectData dans un autre fichier Cs si non unity pas CONTENT ! */
  14.  
  15.     [CreateAssetMenu(fileName = "MyObject", menuName = "ScriptableObjects/MyObject", order = 1)]
  16.     public class MyObjectData : ScriptableObject
  17.     {
  18.         public string text_name;
  19.         public TypeObject type;
  20.  
  21.     }
  22.  
  23.     public enum TypeObject
  24.     {
  25.         None = -1,
  26.         Player = 0,
  27.         Enemy = 1,
  28.         Bullet = 2,
  29.         Item = 3,
  30.         Effect = 4,
  31.         UI = 5,
  32.         Max = 6,
  33.     }
  34.  
  35.     #if UNITY_EDITOR
  36.         [CustomEditor(typeof(MyObjectData))]
  37.         public class MyObjectDataEditor : Editor
  38.         {
  39.             public override void OnInspectorGUI()
  40.             {
  41.                 base.OnInspectorGUI();
  42.  
  43.                 MyObjectData myObjectData = (MyObjectData)target;
  44.  
  45.                 if (GUILayout.Button("Save"))
  46.                 {
  47.                     string assetPath = AssetDatabase.GetAssetPath(myObjectData.GetInstanceID());
  48.                     AssetDatabase.RenameAsset(assetPath, $"{myObjectData.text_name}_{(int)myObjectData.type}");
  49.                     AssetDatabase.SaveAssets();
  50.                     AssetDatabase.Refresh();
  51.                 }
  52.  
  53.                 if (GUILayout.Button("Build"))
  54.                 {
  55.                     ManagerListObject.Build();
  56.  
  57.                     Debug.Log(ManagerListObject.FindOne<ScriptableObject>(new FilterData { name = "TEST" }));
  58.                 }
  59.             }
  60.  
  61.         }
  62.     #endif
  63.  
  64.     public struct FilterData
  65.     {
  66.         public string name;
  67.         public TypeObject type;
  68.  
  69.         public FilterData(string name = "", TypeObject type = TypeObject.None)
  70.         {
  71.             this.name = name;
  72.             this.type = type;
  73.         }
  74.     }
  75.  
  76.     [System.Serializable]
  77.     public struct MetaData
  78.     {
  79.         public string header;
  80.  
  81.         public string name;
  82.         public TypeObject Type;
  83.     }
  84.  
  85.     [System.Serializable]
  86.     public struct MetaDatas
  87.     {
  88.         public MetaData[] metaDatas;
  89.     }
  90.  
  91. #if UNITY_EDITOR
  92.     public static void Build()
  93.     {
  94.         try
  95.         {
  96.             string[] files = Directory.GetFiles($"{Application.dataPath}/Resources/ScriptObjects/Objects");
  97.            
  98.             List<MetaData> _metaDatas = new ();
  99.  
  100.             for (int i = 0; i < files.Length; i++)
  101.             {
  102.                 FileInfo fileInfo = new FileInfo(files[i]);
  103.                 if (fileInfo.Extension == ".asset")
  104.                 {
  105.                     string name = fileInfo.Name.Replace(fileInfo.Extension, "");
  106.  
  107.                     string[] _metaData = name.Split("_");
  108.  
  109.                     MetaData data = new MetaData
  110.                     {
  111.                         header = name,
  112.                         name = _metaData[0],
  113.                         Type = (TypeObject)int.Parse(_metaData[1]),
  114.                     };
  115.  
  116.                     _metaDatas.Add(data);
  117.                 }
  118.             }
  119.  
  120.             string metaDatas_json = JsonUtility.ToJson(new MetaDatas { metaDatas = _metaDatas.ToArray() });
  121.  
  122.             File.WriteAllText($"{Application.dataPath}/Resources/ScriptObjects/Objects.json", metaDatas_json);
  123.         }
  124.         catch (Exception ex)
  125.         {
  126.             Debug.LogError(ex.Message);
  127.         }
  128.     }
  129. #endif
  130.    
  131.     public static T FindOne<T>(FilterData filterData) where T : ScriptableObject
  132.     {
  133.         TextAsset textAsset = Resources.Load<TextAsset>("ScriptObjects/Objects");
  134.         MetaDatas metaDatas = JsonUtility.FromJson<MetaDatas>(textAsset.text);
  135.  
  136.         for (int i = 0; i < metaDatas.metaDatas.Length; i++)
  137.         {
  138.             bool filterOk = false;
  139.  
  140.             if (filterData.type != TypeObject.None)
  141.                 if (metaDatas.metaDatas[i].Type == filterData.type)
  142.                     filterOk = true;
  143.  
  144.             if (filterData.name != string.Empty)
  145.                 if (metaDatas.metaDatas[i].name == filterData.name)
  146.                     filterOk = true;
  147.  
  148.             if (filterOk)
  149.                 return Resources.Load<T>($"ScriptObjects/Objects/{metaDatas.metaDatas[i].header}");
  150.         }
  151.  
  152.         return Resources.Load<T>("ScriptObjects/Objects/");
  153.     }
  154.  
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment