using System; using System.Collections.Generic; using System.IO; using FairyGUI; using Live2D.Cubism.Rendering; using UI.DressUp; using UnityEngine; namespace GFGGame { public class PhotographUtil : SingletonBase { //传入父物体,返回边界大小 public Vector2 GetGameObjectBoundsSize(GameObject parentObj) { float right = int.MinValue; float left = int.MaxValue; float top = int.MinValue; float bottom = int.MaxValue; BoxCollider2D[] boxColliders = parentObj.transform.GetComponentsInChildren(); for (int i = 0; i < boxColliders.Length; i++) { Vector2 pos = boxColliders[i].transform.localPosition; Vector2 size = boxColliders[i].size * boxColliders[i].transform.localScale; 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 boundsSize = new Vector2(right - left, top - bottom); boundsSize = boundsSize * 100 * Math.Abs(parentObj.transform.localScale.x); return boundsSize; } //设置物体中心点 public 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; int index = 0; foreach (Transform t in parent) { string[] strs = t.name.Split('_'); if (strs.Length > 1 && strs[1] == "eff") continue;//不计算特效大小 BoxCollider2D 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; BoxCollider2D 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 GameObject GetFirstHitObj(RaycastHit2D[] hit2Ds) { int layer = int.MinValue; GameObject gameObject = null; for (int i = 0; i < hit2Ds.Length; i++) { int gameobjMaxlayer = GetMaxLayer(hit2Ds[i].collider.transform.parent.gameObject); if (gameobjMaxlayer > layer) { layer = gameobjMaxlayer; gameObject = hit2Ds[i].collider.gameObject; } } return gameObject; } //是否点击在UI上 public bool IsTouchUI(GComponent viewCom) { GObject obj = GRoot.inst.touchTarget; UI_PhotographUI _viewCom = UI_PhotographUI.Proxy(viewCom); return _viewCom.m_comSelectBox.m_btnSize.GetChild("icon").asLoader == obj || _viewCom.m_comSelectBox.m_btnDelete.GetChild("icon").asLoader == obj || _viewCom.m_comSelectBox.m_btnFlip.GetChild("icon").asLoader == obj || _viewCom.m_btnBack == obj || _viewCom.m_btnChoose.GetChild("icon").asLoader == obj || _viewCom.m_btnPhotograph.GetChild("icon").asLoader == obj || _viewCom.m_btnUp.GetChild("icon").asLoader == obj || _viewCom.m_btnDown.GetChild("icon").asLoader == obj || _viewCom.m_btnGalleryJoin.GetChild("icon").asLoader == obj; } public void SetLayer(GameObject hitGameObj, string state) { List itemGameObjs = PhotographDataManager.Instance.itemGameObjs; if (state != "refresh") { int index = itemGameObjs.IndexOf(hitGameObj); if (state != "top") { if (index < 0) { PromptController.Instance.ShowFloatTextPrompt("未选中任物品"); return; } if (index == 0 && state == "down") { PromptController.Instance.ShowFloatTextPrompt("已经在最下层了"); return; } if (index == itemGameObjs.Count - 1 && state == "up") { PromptController.Instance.ShowFloatTextPrompt("已经在最上层了"); return; } } GameObject gameObject = itemGameObjs[index]; itemGameObjs.RemoveAt(index); if (state == "up") { itemGameObjs.Insert((index + 1), gameObject); } else if (state == "down") { itemGameObjs.Insert((index - 1), gameObject); } else if (state == "top") { itemGameObjs.Add(gameObject); } else { PromptController.Instance.ShowFloatTextPrompt(state + "操作失败"); return; } } for (int i = 0; i < itemGameObjs.Count; i++) { ChangeLayer(itemGameObjs[i], i * 300, state); } } public void ChangeLayer(GameObject parentObj, int layer, string state) { int count = 0; if (state == "up" || state == "top") { count = layer - GetMinLayer(parentObj); } else if (state == "down") { count = layer - GetMaxLayer(parentObj); } int changeLayer = layer + count; int maxLayer = int.MinValue; SpriteRenderer[] sps = parentObj.GetComponentsInChildren();//选中道具可能含有静态图 for (int i = 0; i < sps.Length; i++) { int sortingOrder = sps[i].sortingOrder + changeLayer; sps[i].sortingOrder = sortingOrder; ParticleSystem[] particles = sps[i].gameObject.GetComponentsInChildren(); for (int j = 0; j < particles.Length; j++)//静态图可能是动画 { var renderer = particles[j].GetComponent(); if (renderer != null) { renderer.sortingOrder = sortingOrder; } } if (maxLayer < sortingOrder) { maxLayer = sortingOrder; } } for (int i = 0; i < parentObj.transform.childCount; i++)//选中道具可能含有特效 { Transform tf = parentObj.transform.GetChild(i); string[] strs = tf.name.Split('_'); if (strs.Length > 1 && strs[1] == "eff")//子物体是特效 { DressUpUtil.SetParticleSortingOrder(tf.gameObject, changeLayer, true); } } CubismRenderController[] cubismRenders = parentObj.GetComponentsInChildren(); for (int i = 0; i < cubismRenders.Length; i++) { CubismRenderController render = cubismRenders[i]; if (render != null && render.gameObject.activeSelf == true) { render.SortingOrder = render.SortingOrder + changeLayer; } } } private int GetMaxLayer(GameObject parentObj) { int layer = int.MinValue; for (int i = 0; i < parentObj.transform.childCount; i++) { GameObject gameObject = parentObj.transform.GetChild(i).gameObject; Transform tf = gameObject.transform; SpriteRenderer sp = tf.GetComponent(); if (sp && layer < sp.sortingOrder) { layer = sp.sortingOrder; ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren(); for (int j = 0; j < particles.Length; j++) { var renderer = particles[j].GetComponent(); if (renderer != null && layer < renderer.sortingOrder) { layer = renderer.sortingOrder; } } } } return layer; } public int GetMinLayer(GameObject parentObj) { int layer = int.MaxValue; for (int i = 0; i < parentObj.transform.childCount; i++) { GameObject gameObject = parentObj.transform.GetChild(i).gameObject; Transform tf = gameObject.transform; SpriteRenderer sp = tf.GetComponent(); if (sp && sp.sortingOrder < layer) { layer = sp.sortingOrder; ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren(); for (int j = 0; j < particles.Length; j++) { var renderer = particles[j].GetComponent(); if (renderer != null && renderer.sortingOrder < layer) { layer = renderer.sortingOrder; } } } } return layer; } public void SetBgPos(GameObject hitGameObj, Vector2 uiSize) { Vector2 size = hitGameObj.GetComponent().size; float deviationWidth = (size.x - uiSize.x / 100) / 2; float deviationHeigh = (size.y - uiSize.y / 100) / 2; Vector2 pos = hitGameObj.transform.position; if (pos.x <= -deviationWidth) { hitGameObj.transform.position = new Vector2(-deviationWidth, hitGameObj.transform.position.y); } if (pos.x >= deviationWidth) { hitGameObj.transform.position = new Vector2(deviationWidth, hitGameObj.transform.position.y); } if (pos.y <= -deviationHeigh) { hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, -deviationHeigh); } if (pos.y >= deviationHeigh) { hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, deviationHeigh); } } /// /// 将照片保存到本地 /// public void SavePicturoToLocal(byte[] bytes, string fileName) { string path = Application.persistentDataPath + "/Pictures/WanshiJing/"; //判断目录是否存在,不存在则会创建目录 if (!Directory.Exists(path)) { try { Directory.CreateDirectory(path); } catch (Exception exception) { throw new Exception("创建文件夹失败, error:" + exception.Message); } } var filePath = path + fileName; // byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片 File.WriteAllBytes(filePath, bytes); UpdateSystemPhoto(filePath); } //调用iOS或Android原生方法保存图片后更新相册. private void UpdateSystemPhoto(string filePath) { #if UNITY_ANDROID AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名 androidJavaObject.Call("scanFile", filePath, "已保存至本地"); //这里我们可以设置保存成功弹窗内容 #endif } } }