UiSystemModule.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using KairoEngine.Core;
  6. using KairoEngine.Core.ModuleSystem;
  7. using Sirenix.OdinInspector;
  8. namespace KairoEngine.UI
  9. {
  10. [Serializable, HideReferenceObjectPicker, Toggle("enableModule")]
  11. public class UiSystemModule : GameModuleBase
  12. {
  13. public override string name => "UI Module";
  14. [InlineEditor(InlineEditorObjectFieldModes.Boxed), LabelText("UI Library"), NonSerialized, ShowInInspector]
  15. public UiSystemLibrary library;
  16. public UiSystemModule(GameConfig config) : base(config)
  17. {
  18. this.gameConfig = config;
  19. this.className = this.GetType().AssemblyQualifiedName;
  20. this.typeName = "UiSystemModule";
  21. this.enableModule = true;
  22. }
  23. public override void Load(Transform parent)
  24. {
  25. Transform uiParent = CreateUiManager(parent);
  26. }
  27. public override void Reset()
  28. {
  29. library = null;
  30. }
  31. public override void Destroy()
  32. {
  33. }
  34. private Transform CreateUiManager(Transform parent)
  35. {
  36. Transform UiTransform = parent.Find("UI");
  37. GameObject uiObj;
  38. if(UiTransform == null)
  39. {
  40. uiObj = new GameObject();
  41. uiObj.transform.position = new Vector3();
  42. uiObj.transform.rotation = Quaternion.identity;
  43. uiObj.transform.parent = parent;
  44. uiObj.name = "UI";
  45. }
  46. else uiObj = UiTransform.gameObject;
  47. uiObj.AddComponent<UiManager>();
  48. if(library != null) library.Load(uiObj.transform);
  49. return uiObj.transform;
  50. }
  51. public static UiSystemModule JSONToUiSystemModule(string data)
  52. {
  53. try
  54. {
  55. return JsonUtility.FromJson<UiSystemModule>(data);
  56. }
  57. catch (System.Exception e)
  58. {
  59. Debug.LogError($"Could not deserialize UiSystemModule: \n{e}");
  60. return new UiSystemModule(null);
  61. }
  62. }
  63. public override void OnBeforeSerialize(ObjectSerializer serializer)
  64. {
  65. if(library != null) serializer.AddScriptableObject("UiModule_UiSystemLibrary", library);
  66. }
  67. public override void OnBeforeDeserialize(ObjectSerializer serializer)
  68. {
  69. library = (UiSystemLibrary)serializer.GetScriptableObject("UiModule_UiSystemLibrary");
  70. }
  71. }
  72. }