UiSystemModule.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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]
  11. public class UiSystemModule : GameModuleBase
  12. {
  13. public override string name => "UI Module";
  14. [FoldoutGroup("@name"), 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. }
  22. public override void Load(Transform parent)
  23. {
  24. Transform uiParent = CreateUiManager(parent);
  25. }
  26. public override void Reset()
  27. {
  28. library = null;
  29. }
  30. public override void Destroy()
  31. {
  32. }
  33. private Transform CreateUiManager(Transform parent)
  34. {
  35. Transform UiTransform = parent.Find("UI");
  36. GameObject uiObj;
  37. if(UiTransform == null)
  38. {
  39. uiObj = new GameObject();
  40. uiObj.transform.position = new Vector3();
  41. uiObj.transform.rotation = Quaternion.identity;
  42. uiObj.transform.parent = parent;
  43. uiObj.name = "UI";
  44. }
  45. else uiObj = UiTransform.gameObject;
  46. uiObj.AddComponent<UiManager>();
  47. if(library != null) library.Load(uiObj.transform);
  48. return uiObj.transform;
  49. }
  50. public static UiSystemModule JSONToUiSystemModule(string data)
  51. {
  52. try
  53. {
  54. return JsonUtility.FromJson<UiSystemModule>(data);
  55. }
  56. catch (System.Exception e)
  57. {
  58. Debug.LogError($"Could not deserialize UiSystemModule: \n{e}");
  59. return new UiSystemModule(null);
  60. }
  61. }
  62. public override void OnBeforeSerialize(ObjectSerializer serializer)
  63. {
  64. if(library != null) serializer.AddScriptableObject("UiModule_UiSystemLibrary", library);
  65. }
  66. public override void OnBeforeDeserialize(ObjectSerializer serializer)
  67. {
  68. library = (UiSystemLibrary)serializer.GetScriptableObject("UiModule_UiSystemLibrary");
  69. }
  70. }
  71. }