using UnityEngine; using Live2D.Cubism.Rendering; using System.IO; using FairyGUI; using System.Collections.Generic; 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(); assetDisposer.resPath = resPath; } public static void ChangeAssetReleaser(GameObject gameObj, string resPath) { var assetDisposer = gameObj.GetComponent(); if (assetDisposer == null) { assetDisposer = gameObj.AddComponent(); } 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); GameObject gameObject= AddSpriteObj(res, ext, spritObjName, parentObj, sortingOrder, needSetMask); if (itemCfg.subType == (int)ConstDressUpItemType.BEI_JING) { gameObject.layer = 6;//设置bg层 } 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(); 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(); if (spr == null) { spr = gameObj.AddComponent(); } float tx, ty; LoadSpritePos(res, out tx, out ty); gameObj.transform.localPosition = new Vector3(tx, ty, gameObj.transform.localPosition.z); Sprite sp = GFGAsset.Load(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(); //if(spr != null) //{ // spr.sprite = null; //} // var assetDisposer = gameObj_t.GetComponent(); // if (assetDisposer != null) // { // if (!string.IsNullOrEmpty(assetDisposer.resPath)) // { // GFGAsset.Release(assetDisposer.resPath); // assetDisposer.resPath = null; // } // } // SpriteRenderer spr = gameObj_t.GetComponent(); // 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(resPath); var gameObj = GameObject.Instantiate(prefab); AddAssetReleaser(gameObj, resPath); gameObj.name = objName; gameObj.transform.SetParent(parentObj.transform, false); var render = gameObj.GetComponent(); if (render == null && gameObj.transform.childCount > 0) { var childObj = gameObj.transform.GetChild(0); if (childObj != null) { render = childObj.GetComponent(); } } 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(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(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(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(); if (ps != null) { var renderer = ps.GetComponent(); if (renderer != null) { if (isAdd) { renderer.sortingOrder = renderer.sortingOrder + sortingOrder; } else { renderer.sortingOrder = sortingOrder; } } } } } public static NTexture GetPrintscreenNTexture(Camera camera) { Rect rect = new Rect(0, 0, UnityEngine.Screen.width, UnityEngine.Screen.height); RenderTexture render = new RenderTexture((int)rect.width, (int)rect.height, -1);//创建一个RenderTexture对象 camera.gameObject.SetActive(true);//启用截图相机 camera.targetTexture = render;//设置截图相机的targetTexture为render camera.Render();//手动开启截图相机的渲染 RenderTexture.active = render;//激活RenderTexture Texture2D tex = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);//新建一个Texture2D对象 tex.ReadPixels(rect, 0, 0);//读取像素 tex.Apply();//保存像素信息 camera.targetTexture = null;//重置截图相机的targetTexture RenderTexture.active = null;//关闭RenderTexture的激活状态 Object.Destroy(render);//删除RenderTexture对象 camera.gameObject.SetActive(true); return new NTexture(tex); } } }