using UnityEngine; using System; using FairyGUI; 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.UpdateBodyOnly("ui_loginrole", sceneObj, true); } public static void UpdateMainScene(GameObject sceneObj) { //背景0 Transform tf = sceneObj.transform.Find("Bg"); SpriteRenderer spr = tf.GetComponent(); var resPath = ResPathUtil.GetBgImgPath("zjm_1"); Sprite sp = GFGAsset.Load(resPath); DressUpUtil.AddAssetReleaser(tf.gameObject, resPath); spr.sprite = sp; //背景1 Transform tf1 = sceneObj.transform.Find("Bg1"); SpriteRenderer spr1 = tf1.GetComponent(); var resPath1 = ResPathUtil.GetBgImgPath("zjm_2"); Sprite sp1 = GFGAsset.Load(resPath1); DressUpUtil.AddAssetReleaser(tf1.gameObject, resPath); spr1.sprite = sp1; //角色 CustomSuitData suitSavedData = CustomSuitDataManager.GetCurrentSuitList(); MyDressUpHelper.dressUpObj.setSceneObj(sceneObj, false,true,null,false); MyDressUpHelper.dressUpObj.PutOnDressUpData(suitSavedData.dressUpData); } 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; } //拍照角色 public static void UpdatePhotographBody(GameObject sceneObj, GameObject parentObj) { PhotographDataManager.Instance.dressUpObj.setSceneObj(sceneObj,false, false, parentObj, false); PhotographDataManager.Instance.dressUpObj.PutOnDressUpData(MyDressUpHelper.dressUpObj.DressUpDataClone()); GameObject gameObject = parentObj.transform.Find("Head").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 == 1) // { // GameObject.DestroyImmediate(parentGameObj.transform.GetChild(1).gameObject); // } // else if (resLayer == 2) // { // GameObject.DestroyImmediate(parentGameObj.transform.GetChild(0).gameObject); // } // else if (resLayer == 3) // { // } // } 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; } //移除指定GameObject的BoxCollider2D public static void DeleteBoxCollider2DFromGameObject(GameObject gameObject) { BoxCollider2D polygonCollider2D = gameObject.GetComponent(); if (polygonCollider2D != null) { GameObject.Destroy(polygonCollider2D); } } //传入父物体,返回体碰撞盒大小 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.DestroyImmediate(_gameObject); } _gameObject = DressUpUtil.CreateAnimationObj(res); _gameObject.transform.localScale = new Vector3(scale, scale, scale); if (_wrapper == null) { _wrapper = new GoWrapper(_gameObject); holder.SetNativeObject(_wrapper); } else { GameObject.Destroy(_wrapper.wrapTarget); _wrapper.wrapTarget = _gameObject;//替换资源 } wrapper = _wrapper; gameObject = _gameObject; } public static void DestroyObjectFromView(GameObject _gameObject, GoWrapper goWrapper) { if (_gameObject != null) { GameObject.DestroyImmediate(_gameObject); } if (goWrapper != null) { goWrapper.Dispose(); } } } }