123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554 |
- using UnityEngine;
- using Live2D.Cubism.Rendering;
- using System.IO;
- using FairyGUI;
- using UnityEngine.Rendering;
- using YooAsset;
- namespace GFGGame
- {
- public class DressUpUtil
- {
- private const string HEAD_DEFAULT_RES_NAME = "head";
- private const string BODY_DEFAULT_RES_NAME = "body";
- private const string ROLE_OBJ_NAME = "Role";
- private const string HEAD_SPRITE_NAME = "Head";
- private const string BODY_SPRITE_NAME = "Body";
- public const string BODY_ANIMATION_NAME = "Body_a";
- private const string BODY_EFFECT_OBJ_NAME = "Body_eff";
- public const string FORMAT_SPRITE_NAME = "T{0}_s{1}";
- private const string FORMAT_ANIMATION_NAME = "T{0}_a{1}";
- private const string FORMAT_EFFECT_OBJ_NAME = "T{0}_e{1}";
- public static void AddItem(int itemID, GameObject sceneObj, bool needSetMask = false, bool showAni = true, GameObject parentObj = null, int resLayer = 0)
- {
- ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
- if (itemCfg != null)
- {
- // GameObject parentObj = null;
- if (parentObj == null)
- {
- if (itemCfg.subType == ConstDressUpItemType.BEI_JING || DressUpMenuItemCfg1Array.Instance.CheckIsSceneTypeBySubType(itemCfg.subType))
- {
- parentObj = sceneObj;
- }
- else
- {
- //角色
- Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
- parentObj = role.gameObject;
- }
- }
- showAni = showAni || itemCfg.subType == ConstDressUpItemType.ZHUANG_RONG;
- if (resLayer > 0)
- {
- string layerName = "";
- switch (resLayer)
- {
- case 1:
- layerName = itemCfg.resLayer1;
- break;
- case 2:
- layerName = itemCfg.resLayer2;
- break;
- case 3:
- layerName = itemCfg.resLayer3;
- break;
- }
- if (!string.IsNullOrEmpty(layerName))
- {
- updateLayerRes(itemCfg, parentObj, resLayer, needSetMask, showAni);
- }
- // updateLayerRes(itemCfg, parentObj, resLayer, resLayer == 2, needSetMask, showAni);
- }
- else
- {
- //普通层
- if (!string.IsNullOrEmpty(itemCfg.resLayer1))
- {
- updateLayerRes(itemCfg, parentObj, 1, needSetMask, showAni);
- }
- //第二层
- if (!string.IsNullOrEmpty(itemCfg.resLayer2))
- {
- updateLayerRes(itemCfg, parentObj, 2, needSetMask, showAni);
- }
- //第三层
- if (!string.IsNullOrEmpty(itemCfg.resLayer3))
- {
- updateLayerRes(itemCfg, parentObj, 3, needSetMask, showAni);
- }
- }
- }
- }
- public static void RemoveItem(int itemID, GameObject sceneObj, GameObject parentObj = null)
- {
- ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
- if (itemCfg != null)
- {
- if (parentObj == null)
- {
- if (itemCfg.subType == ConstDressUpItemType.BEI_JING || DressUpMenuItemCfg1Array.Instance.CheckIsSceneTypeBySubType(itemCfg.subType))
- {
- parentObj = sceneObj;
- }
- else
- {
- //角色
- Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
- parentObj = role.gameObject;
- }
- }
- string spritObjName;
- string aniObjName;
- //默认层
- if (!string.IsNullOrEmpty(itemCfg.resLayer1))
- {
- spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 1);
- TryRemoveObj(parentObj, spritObjName);
- aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 1);
- TryRemoveObj(parentObj, aniObjName);
- aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, 1);
- TryRemoveObj(parentObj, aniObjName);
- }
- //特殊层
- if (!string.IsNullOrEmpty(itemCfg.resLayer2))
- {
- spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 2);
- TryRemoveObj(parentObj, spritObjName);
- aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 2);
- TryRemoveObj(parentObj, aniObjName);
- aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, 2);
- TryRemoveObj(parentObj, aniObjName);
- }
- //第三层
- if (!string.IsNullOrEmpty(itemCfg.resLayer3))
- {
- spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 3);
- TryRemoveObj(parentObj, spritObjName);
- aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 3);
- TryRemoveObj(parentObj, aniObjName);
- aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, 3);
- TryRemoveObj(parentObj, aniObjName);
- }
- }
- }
- public static void UpdateHead(bool show, GameObject sceneObj, bool needSetMask = false, GameObject parentObj = null)
- {
- var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
- parentObj = parentObj == null ? roleTf.gameObject : parentObj;
- string resPath = ResPathUtil.GetDressUpPath(HEAD_DEFAULT_RES_NAME);
- Transform transform_t = parentObj.transform.Find(HEAD_SPRITE_NAME);
- if (show)
- {
- if (transform_t != null)
- {
- transform_t.gameObject.SetActive(true);
- return;
- }
- AddSpriteObj(resPath, "png", HEAD_SPRITE_NAME, parentObj, 1, needSetMask);
- }
- else
- {
- if (transform_t == null)
- {
- return;
- }
- transform_t.gameObject.SetActive(false);
- }
- }
- public static void UpdateBody(string actionRes, GameObject sceneObj, bool needSetMask = false, GameObject parentObj = null)
- {
- //角色
- var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
- parentObj = parentObj == null ? roleTf.gameObject : parentObj;
- var extPng = "png";
- if (!string.IsNullOrEmpty(actionRes))
- {
- string resPath = ResPathUtil.GetDressUpAnimationPath(actionRes);
- if (GetGameObjExisted(parentObj, BODY_ANIMATION_NAME, resPath) != null)
- {
- return;
- }
- }
- else
- {
- string resPath = ResPathUtil.GetDressUpPath(BODY_DEFAULT_RES_NAME, extPng);
- if (GetGameObjExisted(parentObj, BODY_SPRITE_NAME, resPath) != null)
- {
- return;
- }
- }
- var removeBodyAni = TryRemoveObj(parentObj, BODY_ANIMATION_NAME);
- TryRemoveObj(parentObj, BODY_EFFECT_OBJ_NAME);
- TryRemoveObj(parentObj, BODY_SPRITE_NAME);
- if (!string.IsNullOrEmpty(actionRes))
- {
- var addAniObj = AddAnimationObj(actionRes, BODY_ANIMATION_NAME, parentObj, 0);
- //特效
- TryAddEffectObj(actionRes, BODY_EFFECT_OBJ_NAME, parentObj, 0, addAniObj != null);
- }
- else
- {
- AddSpriteObj(ResPathUtil.GetDressUpPath(BODY_DEFAULT_RES_NAME), extPng, BODY_SPRITE_NAME, parentObj, 0, needSetMask);
- if (removeBodyAni)
- {
- parentObj.transform.SetPositionAndRotation(new Vector3(), new Quaternion());
- }
- }
- }
- public static void AddAssetReleaser(GameObject gameObj, string resPath, AssetOperationHandle handle)
- {
- var assetReleaser = gameObj.GetComponent<AssetReleaser>();
- if (assetReleaser == null)
- {
- assetReleaser = gameObj.AddComponent<AssetReleaser>();
- }
- assetReleaser.SetRes(resPath, handle);
- }
- private static void updateLayerRes(ItemCfg itemCfg, GameObject parentObj, int layerId, bool needSetMask, bool showAni = true)
- {
- ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
- string res = ResPathUtil.GetDressUpItemLayerRes(itemCfg, layerId);
- string resPath = ResPathUtil.GetDressUpPath(itemCfg, layerId);
- int sortingOrder = typeCfg.defaultLayer;
- if (layerId == 2)
- {
- sortingOrder = typeCfg.specialLayer;
- }
- else if (layerId == 3)
- {
- sortingOrder = typeCfg.thirdlLayer;
- }
- //清理旧的
- var spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, layerId);
- TryRemoveObj(parentObj, spritObjName);
- var objName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, layerId);
- TryRemoveObj(parentObj, objName);
- string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
- GameObject spriteObj = null;
- if (DressUpMenuItemCfg1Array.Instance.CheckIsDefaultType(itemCfg.subType))
- {
- //默认类型的部件需要先添加静态图,防止加载动画有延迟,出现光头
- spriteObj = AddSpriteObj(resPath, ext, spritObjName, parentObj, sortingOrder, needSetMask);
- }
- GameObject addAniObj = null;
- if (showAni)
- {
- addAniObj = AddAnimationObj(res, objName, parentObj, sortingOrder);
- if (addAniObj != null && spriteObj != null)
- {
- var dressUpPart = addAniObj.GetComponent<DressUpPart>();
- if (dressUpPart == null)
- {
- dressUpPart = addAniObj.AddComponent<DressUpPart>();
- }
- dressUpPart.OnTimer = (obj) =>
- {
- if (parentObj != null && parentObj.transform != null)
- {
- string resPath = ResPathUtil.GetDressUpPath(itemCfg, layerId);
- spriteObj = GetGameObjExisted(parentObj, spritObjName, resPath);
- if(spriteObj != null)
- {
- PrefabManager.Instance.Restore(spriteObj);
- }
- //Transform tf = parentObj.transform.Find(spritObjName);
- //if (tf != null && tf.gameObject != null && tf.gameObject.activeInHierarchy)
- //{
- // var assetDisposer = tf.gameObject.GetComponent<AssetReleaser>();
- // if (assetDisposer != null)
- // {
- // if (!string.IsNullOrEmpty(assetDisposer.resPath))
- // {
- // string resPath = ResPathUtil.GetDressUpPath(itemCfg, layerId);
- // if (assetDisposer.resPath == resPath)
- // {
- // TryRemoveObj(parentObj, spritObjName);
- // }
- // }
- // }
- //}
- }
- };
- Timers.inst.Add(0.03f, 1, dressUpPart.OnTimer);
- }
- }
- if (spriteObj == null && addAniObj == null)
- {
- //如果两个都没有,就添加静态图
- spriteObj = AddSpriteObj(resPath, ext, spritObjName, parentObj, sortingOrder, needSetMask);
- }
- objName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType, layerId);
- TryRemoveObj(parentObj, objName);
- TryAddEffectObj(res, objName, parentObj, sortingOrder, addAniObj != null);
- }
- private static GameObject AddSpriteObj(string resPath, string ext, string objName, GameObject parentObj, int sortingOrder, bool needSetMask)
- {
- if (!YooAssets.CheckDressUpResExist(resPath))
- {
- return null;
- }
- var gameObj = GetGameObjExisted(parentObj, objName, resPath);
- if (gameObj != null)
- {
- return gameObj;
- }
- SpriteRenderer spr = null;
- gameObj = parentObj.transform.Find(objName)?.gameObject;
- if (gameObj == null)
- {
- gameObj = new GameObject(objName);
- gameObj.transform.SetParent(parentObj.transform, false);
- }
- spr = gameObj.GetComponent<SpriteRenderer>();
- if (spr == null)
- {
- spr = gameObj.AddComponent<SpriteRenderer>();
- }
- float tx, ty;
- LoadSpritePos(resPath, out tx, out ty);
- gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
- //Sprite sp = GFGAsset.Load<Sprite>(resPath);
- var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
- Sprite sp = handle.AssetObject as Sprite;
- AddAssetReleaser(gameObj, resPath, handle);
- spr.sprite = sp;
- spr.sortingOrder = sortingOrder;
- if (needSetMask)
- {
- spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
- if (parentObj.transform.parent != null && parentObj.transform.parent.name == "targetRole")
- {
- spr.maskInteraction = SpriteMaskInteraction.VisibleOutsideMask;
- }
- else
- {
- spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
- }
- }
- else
- {
- spr.maskInteraction = SpriteMaskInteraction.None;
- }
- return gameObj;
- }
- private static GameObject AddAnimationObj(string res, string objName, GameObject parentObj, int sortingOrder)
- {
- string resPath = ResPathUtil.GetDressUpAnimationPath(res);
- var gameObj = GetGameObjExisted(parentObj, objName, resPath);
- if (gameObj != null)
- {
- return gameObj;
- }
- gameObj = CreateObj(resPath);
- if(gameObj == null)
- {
- return null;
- }
- if (objName == BODY_ANIMATION_NAME)
- {
- //如果是动作动画,就根据动画位置及角度信息设置给Role对象
- parentObj.transform.SetPositionAndRotation(gameObj.transform.localPosition, gameObj.transform.localRotation);
- gameObj.transform.SetPositionAndRotation(new Vector3(), new Quaternion());
- }
- gameObj.name = objName;
- gameObj.transform.SetParent(parentObj.transform, false);
- var render = gameObj.GetComponent<CubismRenderController>();
- if (render == null && gameObj.transform.childCount > 0)
- {
- var childObj = gameObj.transform.GetChild(0);
- if (childObj != null)
- {
- render = childObj.GetComponent<CubismRenderController>();
- }
- }
- if (render != null && render.gameObject.activeSelf == true)
- {
- render.SortingOrder = sortingOrder;
- }
- SetRenderersOrder(gameObj, sortingOrder);
- return gameObj;
- }
- private static GameObject TryAddEffectObj(string res, string objName, GameObject parentObj, int sortingOrder, bool inAniDir)
- {
- var resPath = ResPathUtil.GetDressUpEffectPath(res, inAniDir);
- var gameObj = GetGameObjExisted(parentObj, objName, resPath);
- if (gameObj != null)
- {
- return gameObj;
- }
- gameObj = CreateObj(resPath);
- if (gameObj == null)
- {
- return null;
- }
- gameObj.transform.SetParent(parentObj.transform, false);
- gameObj.name = objName;
- var sortingGroup = gameObj.transform.GetComponent<SortingGroup>();
- if (sortingGroup != null)
- {
- GameObject.Destroy(sortingGroup);
- }
- SetRenderersOrder(gameObj, sortingOrder + 1);//特效层默认高一个层级
- return gameObj;
- }
- private static bool TryRemoveObj(GameObject parentObj, string objName)
- {
- if (parentObj == null)
- {
- return false;
- }
- Transform transform = parentObj.transform.Find(objName);
- if (transform != null)
- {
- GameObject gameObj = transform.gameObject;
- if (gameObj != null)
- {
- PrefabManager.Instance.Restore(gameObj);
- return true;
- }
- }
- return false;
- }
- private static GameObject GetGameObjExisted(GameObject parentObj, string objName, string resPath)
- {
- if (parentObj == null)
- {
- return null;
- }
- Transform transform = parentObj.transform.Find(objName);
- if (transform != null)
- {
- GameObject gameObj = transform.gameObject;
- if (gameObj != null)
- {
- var assetReleaser = gameObj.GetComponent<AssetReleaser>();
- if (assetReleaser != null)
- {
- if (assetReleaser.ResPath == resPath)
- {
- return gameObj;
- }
- }
- }
- }
- return null;
- }
- public static GameObject CreateObj(string resPath)
- {
- if (!YooAssets.CheckDressUpResExist(resPath))
- {
- return null;
- }
- //var handle = YooAssets.LoadAssetSync<Sprite>(resPath);
- //Sprite sp = handle.AssetObject as Sprite;
- var gameObj = PrefabManager.Instance.SpawnSync(resPath);
- //AddAssetReleaser(gameObj, resPath);
- return gameObj;
- }
- public static void LoadSpritePos(string resPath, out float tx, out float ty)
- {
- int index = resPath.LastIndexOf('.');
- resPath = resPath.Substring(0, index) + ".bytes";
- //resPath = resPath.Replace(".png", ".bytes");
- //resPath = resPath.Replace(".jpg", ".bytes");
- if (YooAssets.CheckDressUpResExist(resPath))
- {
- //var asset = GFGAsset.Load<TextAsset>(resPath);
- var handle = YooAssets.LoadAssetSync<TextAsset>(resPath);
- TextAsset asset = handle.AssetObject as TextAsset;
- var st = new MemoryStream(asset.bytes);
- var br = new BinaryReader(st);
- tx = br.ReadInt32() / 100f;
- ty = -br.ReadInt32() / 100f;
- //GFGAsset.Release(resPath);
- handle.Release();
- }
- else
- {
- tx = 0;
- ty = 0;
- }
- }
- public static void SetRenderersOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
- {
- var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
- for (int i = 0; i < meshRenderers.Length; i++)
- {
- var renderer = meshRenderers[i].GetComponent<Renderer>();
- if (renderer != null)
- {
- if (isAdd)
- {
- renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
- }
- else
- {
- renderer.sortingOrder = sortingOrder;
- }
- }
- }
- ParticleSystem[] particles = gameObj.transform.GetComponentsInChildren<ParticleSystem>();
- for (int i = 0; i < particles.Length; i++)
- {
- var renderer = particles[i].GetComponent<Renderer>();
- if (renderer != null)
- {
- if (isAdd)
- {
- renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
- }
- else
- {
- renderer.sortingOrder = sortingOrder;
- }
- }
- }
- }
- public static void SetSpriteRenderSortingOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
- {
- SpriteRenderer[] spriteRenders = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
- for (int i = 0; i < spriteRenders.Length; i++)
- {
- if (isAdd)
- {
- spriteRenders[i].sortingOrder = spriteRenders[i].sortingOrder + sortingOrder;
- }
- else
- {
- spriteRenders[i].sortingOrder = sortingOrder;
- }
- }
- }
- }
- }
|