DressUpObjUI.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using FairyGUI;
  2. using System;
  3. using UnityEngine;
  4. namespace GFGGame
  5. {
  6. //实现换装对象在UI界面上显示
  7. public class DressUpObjUI
  8. {
  9. public GameObject sceneObject;
  10. public GoWrapper wrapper;
  11. public DressUpObj dressUpObj;
  12. string prefabName;
  13. private Action onShowAction;
  14. public DressUpObjUI(string prefabName = "SceneDressUp", Action onShowAction = null)
  15. {
  16. this.prefabName = prefabName;
  17. this.onShowAction = onShowAction;
  18. dressUpObj = new DressUpObj();
  19. wrapper = new GoWrapper();
  20. }
  21. /// <summary>
  22. /// 异步重置场景对象
  23. /// </summary>
  24. /// <param name="scale"></param>
  25. /// <param name="needSetMask">如果UI界面的组件有遮罩,那么这里也需要设置为true才能生效</param>
  26. /// <param name="showSceneType">是否显示场景道具类型</param>
  27. /// <param name="roleObj"></param>
  28. /// <param name="showBg">是否显示背景</param>
  29. /// <param name="callback">场景对象加载完成后的回调</param>
  30. public void ResetSceneObjAsync(int scale = 100, bool needSetMask = false, bool showSceneType = true,
  31. GameObject roleObj = null, bool showBg = true, Action<GameObject> callback = null)
  32. {
  33. // 先销毁旧的场景对象
  34. if (sceneObject != null)
  35. {
  36. GameObject.Destroy(sceneObject);
  37. sceneObject = null;
  38. }
  39. // 异步加载新场景对象
  40. PrefabManager.Instance.InstantiateAsync(
  41. ResPathUtil.GetPrefabPath(this.prefabName),
  42. (go) =>
  43. {
  44. if (go != null)
  45. {
  46. sceneObject = go;
  47. sceneObject.transform.localScale = new Vector3(scale, scale, scale);
  48. dressUpObj.setSceneObj(sceneObject, needSetMask, showSceneType, roleObj, showBg, OnShow);
  49. callback?.Invoke(sceneObject);
  50. }
  51. else
  52. {
  53. Debug.LogError($"Failed to instantiate scene object: {this.prefabName}");
  54. callback?.Invoke(null);
  55. }
  56. });
  57. }
  58. /// <summary>
  59. ///
  60. /// </summary>
  61. /// <param name="scale"></param>
  62. /// <param name="needSetMask">如果UI界面的组件有遮罩,那么这里也需要设置为true才能生效</param>
  63. /// <param name="showSceneType">是否显示场景道具类型</param>
  64. /// <param name="roleObj"></param>
  65. /// <param name="showBg">是否显示背景</param>
  66. public void ResetSceneObj(int scale = 100, bool needSetMask = false, bool showSceneType = true, GameObject roleObj = null, bool showBg = true)
  67. {
  68. // 先销毁旧的场景对象
  69. if (sceneObject != null)
  70. {
  71. GameObject.Destroy(sceneObject);
  72. sceneObject = null;
  73. }
  74. //这里每次都要实例化新的,复用会有bug
  75. sceneObject = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath(this.prefabName));
  76. sceneObject.transform.localScale = new Vector3(scale, scale, scale);
  77. dressUpObj.setSceneObj(sceneObject, needSetMask, showSceneType, roleObj, showBg, OnShow);
  78. }
  79. public void UpdateWrapper(GGraph holder)
  80. {
  81. wrapper.wrapTarget = sceneObject;
  82. holder.SetNativeObject(wrapper);
  83. }
  84. public void Dispose()
  85. {
  86. onShowAction = null;
  87. if (sceneObject != null)
  88. {
  89. PrefabManager.Instance.Restore(sceneObject);
  90. sceneObject = null;
  91. }
  92. if (dressUpObj != null)
  93. {
  94. dressUpObj.Dispose();
  95. dressUpObj = null;
  96. }
  97. if (wrapper != null)
  98. {
  99. if (wrapper.wrapTarget != null)
  100. {
  101. wrapper.wrapTarget = null;
  102. }
  103. wrapper.Dispose();
  104. wrapper = null;
  105. }
  106. }
  107. private void OnShow()
  108. {
  109. onShowAction?.Invoke();
  110. if (sceneObject == null)
  111. {
  112. return;
  113. }
  114. if (wrapper != null)
  115. {
  116. wrapper.wrapTarget = sceneObject;
  117. //wrapper.CacheRenderers();
  118. }
  119. }
  120. }
  121. }