| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726 |
- using UnityEngine;
- using Live2D.Cubism.Rendering;
- using System.IO;
- using UnityEngine.Rendering;
- using YooAsset;
- using System.Collections.Generic;
- using System;
- using cfg.GfgCfg;
- namespace GFGGame
- {
- //提供各种换装相关的方法
- public class DressUpUtil
- {
- public const string HEAD_DEFAULT_RES_NAME = "head";
- public const string BODY_DEFAULT_RES_NAME = "body";
- public const string ROLE_OBJ_NAME = "Role";
- public const string HEAD_SPRITE_NAME = "Head";
- public const string BODY_SPRITE_NAME = "Body";
- public const string BODY_ANIMATION_NAME = "Body_a";
- public const string BODY_EFFECT_OBJ_NAME = "Body_eff";
- public const string FORMAT_SPRITE_NAME = "I_{0}_s{1}";
- public const string FORMAT_ANIMATION_NAME = "I_{0}_a{1}";
- public const string FORMAT_EFFECT_OBJ_NAME = "I_{0}_e{1}";
- public static List<DressUpLayerOperation> AddItemAsync(int itemID, GameObject sceneObj,
- bool needSetMask = false, bool showAni = true, GameObject parentObj = null, int resLayer = 0)
- {
- ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemID);
- if (itemCfg == null) return 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;
- List<DressUpLayerOperation> handlers = new List<DressUpLayerOperation>();
- DressUpLayerOperation handler;
- 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))
- {
- handler = UpdateLayerResAsync(itemCfg, parentObj, resLayer, needSetMask, showAni);
- if (handler != null) handlers.Add(handler);
- }
- }
- else
- {
- //普通层
- if (!string.IsNullOrEmpty(itemCfg.ResLayer1))
- {
- handler = UpdateLayerResAsync(itemCfg, parentObj, 1, needSetMask, showAni);
- if (handler != null) handlers.Add(handler);
- }
- //第二层
- if (!string.IsNullOrEmpty(itemCfg.ResLayer2))
- {
- handler = UpdateLayerResAsync(itemCfg, parentObj, 2, needSetMask, showAni);
- if (handler != null) handlers.Add(handler);
- }
- //第三层
- if (!string.IsNullOrEmpty(itemCfg.ResLayer3))
- {
- handler = UpdateLayerResAsync(itemCfg, parentObj, 3, needSetMask, showAni);
- if (handler != null) handlers.Add(handler);
- }
- }
- if (handlers.Count > 0)
- {
- return handlers;
- }
- return null;
- }
- public static string GetSpriteName(ItemCfg itemCfg, int layer)
- {
- if (itemCfg.SubType == ConstDressUpItemType.BEI_JING)
- {
- return "Bg";
- }
- return string.Format(FORMAT_SPRITE_NAME, itemCfg.Id, layer);
- }
- public static void RemoveItem(int itemID, GameObject parentObj)
- {
- ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemID);
- if (itemCfg == null || parentObj == null) return;
- string spritObjName;
- string aniObjName;
- //默认层
- if (!string.IsNullOrEmpty(itemCfg.ResLayer1))
- {
- spritObjName = GetSpriteName(itemCfg, 1);
- TryRemoveSprite(parentObj, spritObjName);
- aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.Id, 1);
- TryRemoveObj(parentObj, aniObjName);
- aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.Id, 1);
- TryRemoveObj(parentObj, aniObjName);
- }
- //特殊层
- if (!string.IsNullOrEmpty(itemCfg.ResLayer2))
- {
- spritObjName = GetSpriteName(itemCfg, 2);
- TryRemoveSprite(parentObj, spritObjName);
- aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.Id, 2);
- TryRemoveObj(parentObj, aniObjName);
- aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.Id, 2);
- TryRemoveObj(parentObj, aniObjName);
- }
- //第三层
- if (!string.IsNullOrEmpty(itemCfg.ResLayer3))
- {
- spritObjName = GetSpriteName(itemCfg, 3);
- TryRemoveSprite(parentObj, spritObjName);
- aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.Id, 3);
- TryRemoveObj(parentObj, aniObjName);
- aniObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.Id, 3);
- TryRemoveObj(parentObj, aniObjName);
- }
- }
- public static DressUpRemoveOperation RemoveItemAsync(int itemID, GameObject sceneObj,
- GameObject parentObj = null)
- {
- ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemID);
- if (itemCfg == null) return 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;
- }
- }
- var operation = new DressUpRemoveOperation(itemID, parentObj);
- operation.Begin();
- return operation;
- }
- public static DressUpLayerOperation UpdateHeadAsync(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 res = null;
- string resPath = null;
- if (show)
- {
- res = HEAD_DEFAULT_RES_NAME;
- resPath = ResPathUtil.GetDressUpPath(res);
- }
- DressUpLayerOperation handler = new DressUpLayerOperation(parentObj, needSetMask, false, resPath, null);
- handler.InitHead();
- handler.Begin();
- return handler;
- }
- public static DressUpLayerOperation UpdateBodyAsync(string actionRes, GameObject sceneObj,
- bool needSetMask = false, GameObject parentObj = null, int itemIdLianYiQun = 0)
- {
- //角色
- var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
- parentObj = parentObj == null ? roleTf.gameObject : parentObj;
- var extPng = "png";
- string resPath = null;
- string effectResPath = null;
- bool showAni = !string.IsNullOrEmpty(actionRes);
- string aniResPath = null;
- if (showAni)
- {
- aniResPath = ResPathUtil.GetDressUpAnimationPath(actionRes);
- if (!YooAssets.CheckResExist(aniResPath))
- {
- showAni = false;
- }
- }
- if (showAni)
- {
- resPath = aniResPath;
- //特效
- effectResPath = ResPathUtil.GetDressUpEffectPath(actionRes, true);
- if (!YooAssets.CheckResExist(effectResPath))
- {
- effectResPath = null;
- }
- }
- else
- {
- bool hideBody = false;
- if (itemIdLianYiQun > 0)
- {
- ItemCfg itemCfg = CommonDataManager.Tables.TblItemCfg.GetOrDefault(itemIdLianYiQun);
- hideBody = itemCfg.HideBody > 0;
- }
- if (hideBody)
- {
- resPath = null;
- }
- else
- {
- resPath = ResPathUtil.GetDressUpPath(BODY_DEFAULT_RES_NAME, extPng);
- }
- }
- DressUpLayerOperation handler =
- new DressUpLayerOperation(parentObj, needSetMask, showAni, resPath, effectResPath);
- handler.InitBody();
- handler.Begin();
- return handler;
- }
- private static DressUpLayerOperation UpdateLayerResAsync(ItemCfg itemCfg, GameObject parentObj, int layerId,
- bool needSetMask, bool showAni = true)
- {
- string resPath;
- string aniResPath = null;
- //根据资源存在与否再次检查是否播放动画
- if (showAni)
- {
- aniResPath = ResPathUtil.GetDressUpLayerAnimationResPath(itemCfg, layerId);
- if (!YooAssets.CheckResExist(aniResPath))
- {
- showAni = false;
- }
- }
- if (showAni)
- {
- resPath = aniResPath;
- }
- else
- {
- resPath = ResPathUtil.GetDressUpLayerSpriteResPath(itemCfg, layerId);
- }
- if (!YooAssets.CheckResExist(resPath))
- {
- return null;
- }
- //特效
- string effectResPath = ResPathUtil.GetDressUpLayerEffectResPath(itemCfg, layerId, showAni);
- if (!YooAssets.CheckResExist(effectResPath))
- {
- effectResPath = null;
- }
- DressUpLayerOperation handler =
- new DressUpLayerOperation(parentObj, needSetMask, showAni, resPath, effectResPath);
- handler.InitLayer(itemCfg, layerId);
- handler.Begin();
- return handler;
- }
- public static void AddSpriteObjAsync(
- string resPath,
- string objName,
- GameObject parentObj,
- int sortingOrder,
- bool needSetMask,
- Action<GameObject> onComplete)
- {
- // 检查资源是否存在
- if (!YooAssets.CheckResExist(resPath))
- {
- onComplete?.Invoke(null);
- return;
- }
- // 检查是否已存在相同对象
- var existedObj = GetGameObjExisted(parentObj, objName, resPath);
- if (existedObj != null)
- {
- onComplete?.Invoke(existedObj);
- return;
- }
- // 查找或创建游戏对象
- GameObject gameObj = parentObj.transform.Find(objName)?.gameObject;
- if (gameObj == null)
- {
- gameObj = new GameObject(objName);
- gameObj.transform.SetParent(parentObj.transform, false);
- }
- // 获取或添加SpriteRenderer
- SpriteRenderer spr = gameObj.GetComponent<SpriteRenderer>();
- if (spr == null)
- {
- spr = gameObj.AddComponent<SpriteRenderer>();
- }
- // 异步加载精灵位置
- LoadSpritePosAsync(resPath, (tx, ty) =>
- {
- // 设置位置
- gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
- // 添加精灵
- SpriteHelper.AddSpriteTo(spr, resPath);
- gameObj.SetActive(true);
- spr.sortingOrder = sortingOrder;
- // 设置遮罩交互
- if (needSetMask)
- {
- spr.maskInteraction = parentObj.transform.parent != null &&
- parentObj.transform.parent.name == "targetRole"
- ? SpriteMaskInteraction.VisibleOutsideMask
- : SpriteMaskInteraction.VisibleInsideMask;
- }
- else
- {
- spr.maskInteraction = SpriteMaskInteraction.None;
- }
- onComplete?.Invoke(gameObj);
- });
- }
- public static void AddAnimationObjAsync(
- string resPath,
- string objName,
- GameObject parentObj,
- int sortingOrder,
- Action<GameObject> callback)
- {
- // 检查已有对象
- var gameObj = GetGameObjExisted(parentObj, objName, resPath);
- if (gameObj != null)
- {
- callback?.Invoke(gameObj);
- return;
- }
- // 异步加载预制体
- PrefabManager.Instance.SpawnAsync(resPath, (loadedObj) =>
- {
- if (loadedObj == null)
- {
- callback?.Invoke(null);
- return;
- }
- if (objName == BODY_ANIMATION_NAME)
- {
- DressUpOffsetInfo dressUpOffsetInfo = loadedObj.GetComponent<DressUpOffsetInfo>();
- if (dressUpOffsetInfo == null)
- {
- dressUpOffsetInfo = loadedObj.AddComponent<DressUpOffsetInfo>();
- dressUpOffsetInfo.OffsetPosition = loadedObj.transform.localPosition;
- dressUpOffsetInfo.Rotation = loadedObj.transform.localRotation;
- }
- parentObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
- parentObj.transform.rotation = dressUpOffsetInfo.Rotation;
- }
- // 设置位置和父对象
- loadedObj.transform.localPosition = Vector3.zero;
- loadedObj.transform.rotation = Quaternion.identity;
- loadedObj.name = objName;
- loadedObj.transform.SetParent(parentObj.transform, false);
- // 设置渲染顺序
- var render = loadedObj.GetComponent<CubismRenderController>();
- if (render == null && loadedObj.transform.childCount > 0)
- {
- var childObj = loadedObj.transform.GetChild(0);
- if (childObj != null)
- {
- render = childObj.GetComponent<CubismRenderController>();
- }
- }
- if (render != null && render.gameObject.activeSelf)
- {
- render.SortingOrder = sortingOrder;
- }
- SetRenderersOrder(loadedObj, sortingOrder);
- callback?.Invoke(loadedObj);
- });
- }
- // public static GameObject AddAnimationObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
- // {
- // var gameObj = GetGameObjExisted(parentObj, objName, resPath);
- // if (gameObj != null)
- // {
- // return gameObj;
- // }
- //
- // gameObj = PrefabManager.Instance.SpawnSync(resPath);
- // if (gameObj == null)
- // {
- // return null;
- // }
- //
- // if (objName == BODY_ANIMATION_NAME)
- // {
- // //这里有个需求是一些特殊动作需要自定义位置和角度,
- // //实现方法就是美术在动作预制体上设置这个位置,
- // //然后程序记录下这个位置,把他设置到role节点上,并把动作节点的位置重置成默认值。
- // DressUpOffsetInfo dressUpOffsetInfo = gameObj.GetComponent<DressUpOffsetInfo>();
- // if (dressUpOffsetInfo == null)
- // {
- // dressUpOffsetInfo = gameObj.AddComponent<DressUpOffsetInfo>();
- // dressUpOffsetInfo.OffsetPosition = gameObj.transform.localPosition;
- // dressUpOffsetInfo.Rotation = gameObj.transform.localRotation;
- // }
- //
- // //如果是动作动画,就根据动画位置及角度信息设置给Role对象
- // parentObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
- // parentObj.transform.rotation = dressUpOffsetInfo.Rotation;
- // //.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
- // }
- //
- // gameObj.transform.localPosition = Vector3.zero;
- // gameObj.transform.rotation = Quaternion.identity;
- // 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;
- // }
- public static GameObject TryAddEffectObj(string resPath, string objName, GameObject parentObj, int sortingOrder)
- {
- var gameObj = GetGameObjExisted(parentObj, objName, resPath);
- if (gameObj != null)
- {
- return gameObj;
- }
- gameObj = PrefabManager.Instance.SpawnSync(resPath);
- if (gameObj == null)
- {
- return null;
- }
- DressUpOffsetInfo dressUpOffsetInfo = gameObj.GetComponent<DressUpOffsetInfo>();
- if (dressUpOffsetInfo == null)
- {
- //这里因为特效资源初始是有位移偏移的,记录下来每次设置为初始位置和角度,以防unity自身的坐标转换导致位置偏移
- dressUpOffsetInfo = gameObj.AddComponent<DressUpOffsetInfo>();
- dressUpOffsetInfo.OffsetPosition = gameObj.transform.localPosition;
- dressUpOffsetInfo.Rotation = gameObj.transform.localRotation;
- }
- gameObj.transform.localPosition = dressUpOffsetInfo.OffsetPosition;
- gameObj.transform.rotation = dressUpOffsetInfo.Rotation;
- 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;
- }
- public 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;
- }
- public static bool TryRemoveSprite(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)
- {
- var spr = gameObj.GetComponent<SpriteRenderer>();
- if (spr != null)
- {
- //LogUtil.LogEditor($"TryRemoveSprite {objName}");
- SpriteHelper.RemoveSpriteFrom(spr);
- gameObj.SetActive(false);
- }
- return true;
- }
- }
- return false;
- }
- public 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 void LoadSpritePos(string resPath, out float tx, out float ty)
- // {
- // int index = resPath.LastIndexOf('.');
- // resPath = resPath.Substring(0, index) + ".bytes";
- // if (YooAssets.CheckResExist(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;
- // handle.Release();
- // }
- // else
- // {
- // tx = 0;
- // ty = 0;
- // }
- // }
- public static void LoadSpritePosAsync(string resPath, Action<float, float> onComplete)
- {
- int index = resPath.LastIndexOf('.');
- resPath = resPath.Substring(0, index) + ".bytes";
- if (YooAssets.CheckResExist(resPath))
- {
- var handle = YooAssets.LoadAssetAsync<TextAsset>(resPath);
- handle.Completed += (operation) =>
- {
- if (operation.Status == EOperationStatus.Succeed)
- {
- TextAsset asset = operation.AssetObject as TextAsset;
- using (var st = new MemoryStream(asset.bytes))
- using (var br = new BinaryReader(st))
- {
- float tx = br.ReadInt32() / 100f;
- float ty = -br.ReadInt32() / 100f;
- onComplete?.Invoke(tx, ty);
- }
- }
- else
- {
- Debug.LogError($"Failed to load position data: {resPath}");
- onComplete?.Invoke(0, 0);
- }
- operation.Release();
- };
- }
- else
- {
- onComplete?.Invoke(0, 0);
- }
- }
- public static void SetRenderersOrder(GameObject gameObj, int sortingOrder)
- {
- var meshRenderers = gameObj.transform.GetComponentsInChildren<MeshRenderer>();
- for (int i = 0; i < meshRenderers.Length; i++)
- {
- var renderer = meshRenderers[i].GetComponent<Renderer>();
- if (renderer != null)
- {
- 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)
- {
- renderer.sortingOrder = sortingOrder;
- }
- }
- var spritesRenderers = gameObj.transform.GetComponentsInChildren<SpriteRenderer>();
- for (int i = 0; i < spritesRenderers.Length; i++)
- {
- var spriteRender = spritesRenderers[i];
- spriteRender.sortingOrder = sortingOrder;
- }
- }
- public static List<int> GetSuitItems(int suitId, bool isAction, int[] excludeType = null,
- bool showOptional = true, bool CheckOwn = true, bool isDress = false)
- {
- SuitCfg suitCfg = CommonDataManager.Tables.TblSuitCfg.GetOrDefault(suitId);
- if (suitCfg == null)
- {
- return null;
- }
- List<int> items = new List<int>(suitCfg.Parts);
- if (showOptional)
- {
- if (suitCfg.PartsOptional != null && suitCfg.PartsOptional.Count > 0)
- {
- items.AddRange(suitCfg.PartsOptional);
- }
- }
- int subType = 0;
- //找到要穿的散件
- List<int> targetItemList = new List<int>();
- foreach (int itemId in items)
- {
- if (!CheckOwn || DressUpMenuItemDataManager.CheckHasItem(itemId) || isDress)
- {
- subType = ItemDataManager.GetItemSubType(itemId);
- bool notInAction = SuitCfgArray.Instance.CheckItemNotInAction(itemId);
- if (!isAction || notInAction)
- {
- if (excludeType == null || Array.IndexOf(excludeType, subType) < 0)
- {
- targetItemList.Add(itemId);
- }
- }
- }
- }
- return targetItemList;
- }
- }
- }
|