using UnityEngine; using System.Collections; using System; using UnityEditor; using FairyGUI; using System.Collections.Generic; using System.Linq; namespace GFGGame { public class SceneController { public static void UpdateLoginScene(GameObject sceneObj) { //背景 Transform tf = sceneObj.transform.Find("Bg"); SpriteRenderer spr = tf.GetComponent(); var resPath = ResPathUtil.GetDressUpPath("jhsy_bg", "jpg"); Sprite sp = GFGAsset.Load(resPath); DressUpUtil.AddAssetReleaser(tf.gameObject, resPath); spr.sprite = sp; //角色 DressUpUtil.UpdateBody("ui_loginrole", sceneObj, true, null); } public static void UpdateMainScene(GameObject sceneObj) { //背景 Transform tf = sceneObj.transform.Find("Bg"); SpriteRenderer spr = tf.GetComponent(); var resPath = ResPathUtil.GetDressUpPath("zjm_bg1", "jpg"); Sprite sp = GFGAsset.Load(resPath); DressUpUtil.AddAssetReleaser(tf.gameObject, resPath); spr.sprite = sp; //角色 CustomSuitData suitSavedData = CustomSuitDataManager.GetCurrentSuitList(); List equipDatas = suitSavedData.equipDatas; UpdateRole(equipDatas, sceneObj, false, null, true, null, false); if (suitSavedData.pic && suitSavedData.suitId > 0) { SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitSavedData.suitId); DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, !string.IsNullOrEmpty(suitCfg.aniRes), suitCfg.effRes); } else { DressUpUtil.UpdateBody(null, sceneObj); } } public static void UpdateRole(int[] equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null, bool reset = true) { if(reset) { Reset(sceneObj); } int count = equipDatas.Length; for (int i = 0; i < count; i++) { int id = (int)equipDatas[i]; if (exceptTypes != null) { int subType = ItemUtilCS.GetItemSubType(id); if (Array.IndexOf(exceptTypes, subType) >= 0) { continue; } } DressUpUtil.AddItem(id, sceneObj, needSetMask, showAni, parentObj); } DressUpUtil.UpdateBody(null, sceneObj, false, null, needSetMask, parentObj); } public static void UpdateRole(List equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null, bool reset = true) { UpdateRole(equipDatas.ToArray(), sceneObj, needSetMask, exceptTypes, showAni, parentObj, reset); } public static void UpdateDialogBg(string value, GameObject sceneObj) { Transform tf = sceneObj.transform.Find("Bg"); SpriteRenderer spr = tf.GetComponent(); if (value == "0") { spr.sprite = null; } else { var resPath = ResPathUtil.GetDressUpPath(value, "jpg"); Sprite sp = GFGAsset.Load(resPath); DressUpUtil.AddAssetReleaser(tf.gameObject, resPath); spr.sprite = sp; } } public static void UpdateDialogPic(string value, GameObject sceneObj) { Transform tf = sceneObj.transform.Find("Pic"); SpriteRenderer spr = tf.GetComponent(); if (value == "0") { spr.sprite = null; } else { void UpdateDialogPicAlpha(object param) { if (spr != null) { Color c = spr.color; if (spr.enabled && c.a < 1f) { c.a += 0.05f; spr.color = c; } else { FairyGUI.Timers.inst.Remove(UpdateDialogPicAlpha); } } else { FairyGUI.Timers.inst.Remove(UpdateDialogPicAlpha); } } var resPath = ResPathUtil.GetNpcPicSPath(value); Sprite sp = GFGAsset.Load(resPath); DressUpUtil.AddAssetReleaser(tf.gameObject, resPath); spr.sprite = sp; Color c = spr.color; c.a = 0f; spr.color = c; FairyGUI.Timers.inst.AddUpdate(UpdateDialogPicAlpha); } } public static void UpdateFightTarget(string value, GameObject sceneObj) { Transform tf = sceneObj.transform.Find("Npc"); SpriteRenderer spr = tf.GetComponent(); var resPath = ResPathUtil.GetNpcPicFPath(value); Sprite sp = GFGAsset.Load(resPath); DressUpUtil.AddAssetReleaser(tf.gameObject, resPath); spr.sprite = sp; } private static void Reset(GameObject sceneObj) { //背景层次 Transform bg = sceneObj.transform.Find("Bg"); if (bg != null) { SpriteRenderer spr = bg.GetComponent(); if (spr != null) { ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING); spr.sortingOrder = typeCfg.defaultLayer; } } //角色清理 Transform role = sceneObj.transform.Find("Role"); if (role != null) { int childCount = role.childCount; for (int i = childCount - 1; i >= 0; --i) { Transform child = role.GetChild(i); if (child.gameObject.name != "Body") { GameObject.DestroyImmediate(child.gameObject); } } } } //拍照角色 public static void UpdatePhotographBody(List equipDatas, GameObject sceneObj, GameObject parentObj) { SceneController.UpdateRole(equipDatas, sceneObj, false, null, false, parentObj); if (EquipDataCache.cacher.IsSuitPic && EquipDataCache.cacher.suitId > 0) { SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(EquipDataCache.cacher.suitId); DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, false, null, false, parentObj); } GameObject gameObject = parentObj.transform.Find("Body").gameObject; SceneController.SetBoxCollider2DToGameObject(gameObject); } //拍照场景添加单个道具 public static void AddItemToScene(GameObject sceneObj, GameObject parentGameObj, int itemId, int resLayer) { ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId); parentGameObj.transform.SetParent(sceneObj.transform.Find("Scene"), false); DressUpUtil.AddItem(itemId, sceneObj, false, false, parentGameObj, resLayer); if (parentGameObj.transform.childCount > 1) { if (resLayer == itemCfg.resLayer1) { GameObject.DestroyImmediate(parentGameObj.transform.GetChild(1).gameObject); } else if (resLayer == itemCfg.resLayer2) { GameObject.DestroyImmediate(parentGameObj.transform.GetChild(0).gameObject); } } SceneController.SetBoxCollider2DToGameObject(parentGameObj.transform.GetChild(0).gameObject); } //向Transform添加SpriteRenderer并设置资源 public static void SetSpriteRendererToTransform(Transform tf, string resPath) { tf.position = Vector3.zero; SpriteRenderer spr = tf.GetComponent(); if (spr == null) { tf.gameObject.AddComponent(); spr = tf.GetComponent(); } DressUpUtil.ChangeAssetReleaser(tf.gameObject, resPath); Sprite sp = GFGAsset.Load(resPath); spr.sprite = sp; spr.size = spr.sprite.bounds.size;//将节点设置为原图大小 ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING); } //向GameObject添加BoxCollider2D public static void SetBoxCollider2DToGameObject(GameObject gameObject) { BoxCollider2D polygonCollider2D = gameObject.GetComponent(); if (polygonCollider2D != null) { GameObject.Destroy(polygonCollider2D); } polygonCollider2D = gameObject.AddComponent(); polygonCollider2D.isTrigger = true; } //传入父物体,返回体碰撞盒大小 public static Vector2 GetGameObjectSize(GameObject parentObj) { Vector2 size = Vector2.one; for (int i = 0; i < parentObj.transform.childCount; i++) { GameObject childGameObj = parentObj.transform.GetChild(i).gameObject; BoxCollider2D boxCollider2D = childGameObj.GetComponent(); if (boxCollider2D != null) { size = GetGameObjectBoundsSize(parentObj); if (parentObj.name == "Role") boxCollider2D.size = size; boxCollider2D.offset = -childGameObj.transform.localPosition; //* childGameObj.transform.localScale size = size * parentObj.transform.localScale * 100; return size; } } return Vector2.zero; } private static Vector2 GetGameObjectBoundsSize(GameObject parentObj) { float right = int.MinValue; float left = int.MaxValue; float top = int.MinValue; float bottom = int.MaxValue; for (int i = 0; i < parentObj.transform.childCount; i++) { Transform transform = parentObj.transform.GetChild(i); SpriteRenderer sr = transform.GetComponent(); if (sr != null) { Vector2 pos = transform.localPosition; Vector2 size = sr.bounds.size / parentObj.transform.localScale.x; Debug.Log("size:" + size + " pos:" + pos); right = Math.Max(size.x / 2 + pos.x, right); left = Math.Min(pos.x - size.x / 2, left); top = Math.Max(size.y / 2 + pos.y, top); bottom = Math.Min(pos.y - size.y / 2, bottom); } } Vector2 bounds = new Vector2(right - left, top - bottom); Debug.Log("size:" + bounds); return bounds; } //设置物体中心点 public static void SetGameObjectCenter(GameObject parentObj) { Transform parent = parentObj.transform; // 2.选中技算 Vector3 postion = parent.position; Quaternion rotation = parent.rotation; Vector3 scale = parent.localScale; parent.position = Vector3.zero; parent.rotation = Quaternion.Euler(Vector3.zero); parent.localScale = Vector3.one; Vector3 center = Vector3.zero; Renderer[] renders = parent.GetComponentsInChildren(); int index = 0; foreach (Transform t in parent) { string[] strs = t.name.Split('_'); if (strs.Length > 1 && strs[1] == "eff") continue;//不计算特效大小 Renderer render = t.GetComponent(); if (render) { index++; center += render.bounds.center; } } center /= index; Bounds bounds = new Bounds(center, Vector3.zero); foreach (Transform t in parent) { string[] strs = t.name.Split('_'); if (strs.Length > 1 && strs[1] == "eff") continue; Renderer render = t.GetComponent(); if (render) bounds.Encapsulate(render.bounds); } parent.position = postion; parent.rotation = rotation; parent.localScale = scale; foreach (Transform t in parent) { string[] strs = t.parent.name.Split('_'); if (strs.Length > 1 && strs[1] == "eff") continue; t.position = t.position - bounds.center; } parent.position = bounds.center + parent.position; } public static GameObject GetFirstHitObj(RaycastHit2D[] hit2Ds) { int layer = int.MinValue; GameObject gameObject = null; for (int i = 0; i < hit2Ds.Length; i++) { SpriteRenderer spr = hit2Ds[i].collider.gameObject.GetComponent(); if (spr && spr.sortingOrder > layer) { gameObject = hit2Ds[i].collider.gameObject; layer = spr.sortingOrder; } } return gameObject; } public static void AddObjectToView(GameObject _gameObject, GoWrapper _wrapper, GGraph holder, string res, out GameObject gameObject, out GoWrapper wrapper, float scale = 100) { if (_gameObject != null) { GameObject.Destroy(_gameObject); _gameObject = null; } _gameObject = DressUpUtil.CreateAnimationObj(res); _gameObject.transform.localScale = new Vector3(scale, scale, scale); if (_wrapper == null) { _wrapper = new GoWrapper(_gameObject); holder.SetNativeObject(_wrapper); } else { _wrapper.wrapTarget = _gameObject;//替换资源 } wrapper = _wrapper; gameObject = _gameObject; } public static void DestroyObjectFromView(GameObject _gameObject) { if (_gameObject != null) { GameObject.Destroy(_gameObject); _gameObject = null; } } } }