123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- using UnityEngine;
- using Live2D.Cubism.Rendering;
- using System.IO;
- using FairyGUI;
- namespace GFGGame
- {
- public class DressUpUtil
- {
- private const string BODY_DEFAULT_RES_NAME = "renmo";
- private const string ROLE_OBJ_NAME = "Role";
- private const string BODY_SPRITE_NAME = "Body";
- private const string BODY_ANIMATION_NAME = "Body_a";
- private const string BODY_EFFECT_OBJ_NAME = "Body_eff";
- private 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}_eff";
- private const string FORMAT_LAYER_RES_NAME_WITH_T = "{0}_t";
- 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)
- {
- parentObj = sceneObj;
- }
- else
- {
- //角色
- Transform role = sceneObj.transform.Find(ROLE_OBJ_NAME);
- parentObj = role.gameObject;
- }
- }
- 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);
- }
- }
- //特效
- if (itemCfg.effLayer > 0)
- {
- var objName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType);
- ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
- int sortingOrder = typeCfg.defaultLayer;
- if (itemCfg.effLayer == 2)
- {
- sortingOrder = typeCfg.specialLayer;
- }
- AddEffectObj(itemCfg.res, objName, parentObj, sortingOrder);
- }
- }
- }
- public static void RemoveItem(int itemID, GameObject sceneObj)
- {
- ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemID);
- if (itemCfg != null)
- {
- GameObject parentObj = null;
- if (itemCfg.subType == (int)ConstDressUpItemType.BEI_JING)
- {
- 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);
- TryClearSpriteObj(parentObj, spritObjName);
- aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 1);
- TryRemoveAnimationObj(parentObj, aniObjName);
- }
- //特殊层
- if (!string.IsNullOrEmpty(itemCfg.resLayer2))
- {
- spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 2);
- TryClearSpriteObj(parentObj, spritObjName);
- aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 2);
- TryRemoveAnimationObj(parentObj, aniObjName);
- }
- //第三层
- if (!string.IsNullOrEmpty(itemCfg.resLayer3))
- {
- spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, 3);
- TryClearSpriteObj(parentObj, spritObjName);
- aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, 3);
- TryRemoveAnimationObj(parentObj, aniObjName);
- }
- //特效
- if (itemCfg.effLayer > 0)
- {
- string effObjName = string.Format(FORMAT_EFFECT_OBJ_NAME, itemCfg.subType);
- var effTf = parentObj.transform.Find(effObjName);
- if (effTf != null)
- {
- GameObject.DestroyImmediate(effTf.gameObject);
- }
- }
- }
- }
- public static void UpdateBody(string res, GameObject sceneObj, bool isAni = false, string effRes = null, bool needSetMask = false, GameObject parentObj = null)
- {
- //角色
- var roleTf = sceneObj.transform.Find(ROLE_OBJ_NAME);
- parentObj = parentObj == null ? roleTf.gameObject : parentObj;
- if (res == null)
- {
- res = BODY_DEFAULT_RES_NAME;
- }
- //清理旧的
- TryClearSpriteObj(parentObj, BODY_SPRITE_NAME);
- TryRemoveAnimationObj(parentObj, BODY_ANIMATION_NAME);
- if (isAni)
- {
- AddAnimationObj(res, BODY_ANIMATION_NAME, parentObj, 0);
- }
- else
- {
- AddSpriteObj(res, "png", BODY_SPRITE_NAME, parentObj, 0, needSetMask);
- }
- //特效
- var tf = parentObj.transform.Find(BODY_EFFECT_OBJ_NAME);
- if (tf != null)
- {
- GameObject.DestroyImmediate(tf.gameObject);
- }
- if (!string.IsNullOrEmpty(effRes))
- {
- AddEffectObj(effRes, BODY_EFFECT_OBJ_NAME, parentObj, 0);
- }
- }
- public static void AddAssetReleaser(GameObject gameObj, string resPath)
- {
- var assetDisposer = gameObj.AddComponent<AssetReleaser>();
- assetDisposer.resPath = resPath;
- }
- public static void ChangeAssetReleaser(GameObject gameObj, string resPath)
- {
- var assetDisposer = gameObj.GetComponent<AssetReleaser>();
- if (assetDisposer == null)
- {
- assetDisposer = gameObj.AddComponent<AssetReleaser>();
- }
- assetDisposer.resPath = resPath;
- }
- private static void updateLayerRes(ItemCfg itemCfg, GameObject parentObj, int layerId, bool needSetMask, bool showAni = true)
- {
- ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(itemCfg.subType);
- string res = itemCfg.res;
- int sortingOrder = typeCfg.defaultLayer;
- switch (layerId)
- {
- case 1:
- res = itemCfg.resLayer1 == "n" ? res : string.Format("{0}_{1}", res, itemCfg.resLayer1);
- break;
- case 2:
- sortingOrder = typeCfg.specialLayer;
- res = itemCfg.resLayer2 == "n" ? res : string.Format("{0}_{1}", res, itemCfg.resLayer2);
- break;
- case 3:
- sortingOrder = typeCfg.thirdlLayer;
- res = itemCfg.resLayer3 == "n" ? res : string.Format("{0}_{1}", res, itemCfg.resLayer3);
- break;
- }
- //清理旧的
- var spritObjName = string.Format(FORMAT_SPRITE_NAME, itemCfg.subType, layerId);
- TryClearSpriteObj(parentObj, spritObjName);
- var aniObjName = string.Format(FORMAT_ANIMATION_NAME, itemCfg.subType, layerId);
- TryRemoveAnimationObj(parentObj, aniObjName);
- string ext = ItemUtil.GetItemResExt(itemCfg.itemType, itemCfg.subType);
- AddSpriteObj(res, ext, spritObjName, parentObj, sortingOrder, needSetMask);
- if (itemCfg.isAni > 0 && showAni)
- {
- AddAnimationObj(res, aniObjName, parentObj, sortingOrder);
- Timers.inst.Add(0.03f, 1, (obj) =>
- {
- if (parentObj != null && parentObj.transform != null)
- {
- 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(res, ext);
- if (assetDisposer.resPath == resPath)
- {
- TryClearSpriteObj(parentObj, spritObjName);
- }
- }
- }
- }
- }
- });
- }
- }
- private static GameObject AddSpriteObj(string res, string ext, string objName, GameObject parentObj, int sortingOrder, bool needSetMask)
- {
- string resPath = ResPathUtil.GetDressUpPath(res, ext);
- SpriteRenderer spr = null;
- var gameObj = parentObj.transform.Find(objName)?.gameObject;
- if (gameObj == null)
- {
- gameObj = new GameObject(objName);
- gameObj.transform.SetParent(parentObj.transform, false);
- AddAssetReleaser(gameObj, resPath);
- }
- spr = gameObj.GetComponent<SpriteRenderer>();
- if (spr == null)
- {
- spr = gameObj.AddComponent<SpriteRenderer>();
- }
- float tx, ty;
- LoadSpritePos(res, out tx, out ty);
- gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z);
- Sprite sp = GFGAsset.Load<Sprite>(resPath);
- spr.sprite = sp;
- spr.sortingOrder = sortingOrder;
- if (needSetMask)
- {
- spr.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
- }
- else
- {
- spr.maskInteraction = SpriteMaskInteraction.None;
- }
- return gameObj;
- }
- public static void TryClearSpriteObj(GameObject parentObj, string spritObjName)
- {
- if (parentObj == null)
- {
- return;
- }
- Transform transform_t = parentObj.transform.Find(spritObjName);
- if (transform_t != null)
- {
- GameObject gameObj_t = transform_t.gameObject;
- if (gameObj_t != null)
- {
- //SpriteRenderer spr = null;
- //spr = gameObj_t.GetComponent<SpriteRenderer>();
- //if(spr != null)
- //{
- // spr.sprite = null;
- //}
- // var assetDisposer = gameObj_t.GetComponent<AssetReleaser>();
- // if (assetDisposer != null)
- // {
- // if (!string.IsNullOrEmpty(assetDisposer.resPath))
- // {
- // GFGAsset.Release(assetDisposer.resPath);
- // assetDisposer.resPath = null;
- // }
- // }
- // SpriteRenderer spr = gameObj_t.GetComponent<SpriteRenderer>();
- // if (spr != null)
- // {
- // GameObject.Destroy(spr);
- // }
- GameObject.DestroyImmediate(gameObj_t);
- }
- }
- }
- private static GameObject AddAnimationObj(string res, string objName, GameObject parentObj, int sortingOrder)
- {
- string resPath = ResPathUtil.GetDressUpAnimationPath(res);
- var prefab = GFGAsset.Load<GameObject>(resPath);
- var gameObj = GameObject.Instantiate(prefab);
- AddAssetReleaser(gameObj, resPath);
- 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.SortingOrder = sortingOrder;
- }
- SetParticleSortingOrder(gameObj, sortingOrder);
- return gameObj;
- }
- private static void TryRemoveAnimationObj(GameObject parentObj, string aniObjName)
- {
- if (parentObj == null)
- {
- return;
- }
- Transform transform = parentObj.transform.Find(aniObjName);
- if (transform != null)
- {
- GameObject gameObj = transform.gameObject;
- if (gameObj != null)
- {
- GameObject.DestroyImmediate(gameObj);
- }
- }
- }
- public static GameObject CreateAnimationObj(string resPath)
- {
- // string resPath = ResPathUtil.GetCardAnimationPath(res);
- var prefab = GFGAsset.Load<GameObject>(resPath);
- if (prefab == null)
- {
- return null;
- }
- var gameObj = GameObject.Instantiate(prefab);
- AddAssetReleaser(gameObj, resPath);
- return gameObj;
- }
- private static GameObject AddEffectObj(string res, string objName, GameObject parentObj, int sortingOrder)
- {
- var resPath = ResPathUtil.GetDressUpEffectPath(res);
- GameObject effPre = GFGAsset.Load<GameObject>(resPath);
- var gameObj = GameObject.Instantiate(effPre);
- AddAssetReleaser(gameObj, resPath);
- gameObj.transform.SetParent(parentObj.transform);
- gameObj.name = objName;
- SetParticleSortingOrder(gameObj, sortingOrder);
- return gameObj;
- }
- private static void LoadSpritePos(string res, out float tx, out float ty)
- {
- string resPath = ResPathUtil.GetDressUpPath(res, "bytes");
- if (VEngine.Versions.Contains(resPath))
- {
- var asset = GFGAsset.Load<TextAsset>(resPath);
- if (asset != null)
- {
- var st = new MemoryStream(asset.bytes);
- var br = new BinaryReader(st);
- tx = br.ReadInt32() / 100f;
- ty = -br.ReadInt32() / 100f;
- GFGAsset.Release(resPath);
- return;
- }
- }
- tx = 0;
- ty = 0;
- }
- public static void SetParticleSortingOrder(GameObject gameObj, int sortingOrder, bool isAdd = false)
- {
- var count = gameObj.transform.childCount;
- for (int i = 0; i < count; i++)
- {
- var tf = gameObj.transform.GetChild(i);
- var ps = tf.GetComponent<ParticleSystem>();
- if (ps != null)
- {
- var renderer = ps.GetComponent<Renderer>();
- if (renderer != null)
- {
- if (isAdd)
- {
- renderer.sortingOrder = renderer.sortingOrder + sortingOrder;
- }
- else
- {
- renderer.sortingOrder = sortingOrder;
- }
- }
- }
- }
- }
- }
- }
|