DressUpObjUI.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. public void ResetSceneObj(int scale = 100, bool needSetMask = false, bool showSceneType = true, GameObject roleObj = null, bool showBg = true)
  30. {
  31. //这里每次都要实例化新的,复用会有bug
  32. sceneObject = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath(this.prefabName));
  33. sceneObject.transform.localScale = new Vector3(scale, scale, scale);
  34. dressUpObj.setSceneObj(sceneObject, needSetMask, showSceneType, roleObj, showBg, OnShow);
  35. }
  36. public void UpdateWrapper(GGraph holder)
  37. {
  38. holder.SetNativeObject(wrapper);
  39. wrapper.wrapTarget = sceneObject;
  40. }
  41. public void Dispose()
  42. {
  43. onShowAction = null;
  44. if (sceneObject != null)
  45. {
  46. PrefabManager.Instance.Restore(sceneObject);
  47. sceneObject = null;
  48. }
  49. if (dressUpObj != null)
  50. {
  51. dressUpObj.Dispose();
  52. dressUpObj = null;
  53. }
  54. if (wrapper != null)
  55. {
  56. if (wrapper.wrapTarget != null)
  57. {
  58. wrapper.wrapTarget = null;
  59. }
  60. wrapper.Dispose();
  61. wrapper = null;
  62. }
  63. }
  64. private void OnShow()
  65. {
  66. onShowAction?.Invoke();
  67. if (sceneObject == null)
  68. {
  69. return;
  70. }
  71. if(wrapper != null)
  72. {
  73. wrapper.wrapTarget = sceneObject;
  74. //wrapper.CacheRenderers();
  75. }
  76. }
  77. }
  78. }