| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using FairyGUI;
- using System;
- using UnityEngine;
- namespace GFGGame
- {
- //实现换装对象在UI界面上显示
- public class DressUpObjUI
- {
- public GameObject sceneObject;
- public GoWrapper wrapper;
- public DressUpObj dressUpObj;
- string prefabName;
- private Action onShowAction;
- public DressUpObjUI(string prefabName = "SceneDressUp", Action onShowAction = null)
- {
- this.prefabName = prefabName;
- this.onShowAction = onShowAction;
- dressUpObj = new DressUpObj();
- wrapper = new GoWrapper();
- }
- /// <summary>
- /// 异步重置场景对象
- /// </summary>
- /// <param name="scale"></param>
- /// <param name="needSetMask">如果UI界面的组件有遮罩,那么这里也需要设置为true才能生效</param>
- /// <param name="showSceneType">是否显示场景道具类型</param>
- /// <param name="roleObj"></param>
- /// <param name="showBg">是否显示背景</param>
- /// <param name="callback">场景对象加载完成后的回调</param>
- public void ResetSceneObjAsync(int scale = 100, bool needSetMask = false, bool showSceneType = true,
- GameObject roleObj = null, bool showBg = true, Action<GameObject> callback = null)
- {
- // 先销毁旧的场景对象
- if (sceneObject != null)
- {
- GameObject.Destroy(sceneObject);
- sceneObject = null;
- }
- // 异步加载新场景对象
- PrefabManager.Instance.InstantiateAsync(
- ResPathUtil.GetPrefabPath(this.prefabName),
- (go) =>
- {
- if (go != null)
- {
- sceneObject = go;
- sceneObject.transform.localScale = new Vector3(scale, scale, scale);
- dressUpObj.setSceneObj(sceneObject, needSetMask, showSceneType, roleObj, showBg, OnShow);
- callback?.Invoke(sceneObject);
- }
- else
- {
- Debug.LogError($"Failed to instantiate scene object: {this.prefabName}");
- callback?.Invoke(null);
- }
- });
- }
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="scale"></param>
- /// <param name="needSetMask">如果UI界面的组件有遮罩,那么这里也需要设置为true才能生效</param>
- /// <param name="showSceneType">是否显示场景道具类型</param>
- /// <param name="roleObj"></param>
- /// <param name="showBg">是否显示背景</param>
- public void ResetSceneObj(int scale = 100, bool needSetMask = false, bool showSceneType = true, GameObject roleObj = null, bool showBg = true)
- {
- //这里每次都要实例化新的,复用会有bug
- sceneObject = PrefabManager.Instance.InstantiateSync(ResPathUtil.GetPrefabPath(this.prefabName));
- sceneObject.transform.localScale = new Vector3(scale, scale, scale);
- dressUpObj.setSceneObj(sceneObject, needSetMask, showSceneType, roleObj, showBg, OnShow);
- }
- public void UpdateWrapper(GGraph holder)
- {
- holder.SetNativeObject(wrapper);
- wrapper.wrapTarget = sceneObject;
- }
- public void Dispose()
- {
- onShowAction = null;
- if (sceneObject != null)
- {
- PrefabManager.Instance.Restore(sceneObject);
- sceneObject = null;
- }
- if (dressUpObj != null)
- {
- dressUpObj.Dispose();
- dressUpObj = null;
- }
- if (wrapper != null)
- {
- if (wrapper.wrapTarget != null)
- {
- wrapper.wrapTarget = null;
- }
- wrapper.Dispose();
- wrapper = null;
- }
- }
- private void OnShow()
- {
- onShowAction?.Invoke();
- if (sceneObject == null)
- {
- return;
- }
- if (wrapper != null)
- {
- wrapper.wrapTarget = sceneObject;
- //wrapper.CacheRenderers();
- }
- }
- }
- }
|