123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- 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<PhotographUtil>
- {
- //传入父物体,返回边界大小
- 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<BoxCollider2D>();
- 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].IndexOf("e") >= 0) continue;//不计算特效大小
- BoxCollider2D render = t.GetComponent<BoxCollider2D>();
- if (render)
- {
- index++;
- center += render.bounds.center;
- }
- }
- if (index > 0) 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].IndexOf("e") >= 0) continue;
- BoxCollider2D render = t.GetComponent<BoxCollider2D>();
- 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].IndexOf("e") >= 0) 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<GameObject> itemGameObjs = PhotographDataManager.Instance.itemGameObjs;
- if (state != "refresh")
- {
- int index = itemGameObjs.IndexOf(hitGameObj);
- if (index == itemGameObjs.Count - 1 && state == "top") return;
- 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<SpriteRenderer>();//选中道具可能含有静态图
- for (int i = 0; i < sps.Length; i++)
- {
- int sortingOrder = sps[i].sortingOrder + changeLayer;
- sps[i].sortingOrder = sortingOrder;
- ParticleSystem[] particles = sps[i].gameObject.GetComponentsInChildren<ParticleSystem>();
- for (int j = 0; j < particles.Length; j++)//静态图可能是动画
- {
- var renderer = particles[j].GetComponent<Renderer>();
- 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].IndexOf("e") >= 0)//子物体是特效
- {
- DressUpUtil.SetRenderersOrder(tf.gameObject, changeLayer, true);
- }
- }
- CubismRenderController[] cubismRenders = parentObj.GetComponentsInChildren<CubismRenderController>();
- 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<SpriteRenderer>();
- if (sp && layer < sp.sortingOrder)
- {
- layer = sp.sortingOrder;
- ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren<ParticleSystem>();
- for (int j = 0; j < particles.Length; j++)
- {
- var renderer = particles[j].GetComponent<Renderer>();
- 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<SpriteRenderer>();
- if (sp && sp.sortingOrder < layer)
- {
- layer = sp.sortingOrder;
- ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren<ParticleSystem>();
- for (int j = 0; j < particles.Length; j++)
- {
- var renderer = particles[j].GetComponent<Renderer>();
- if (renderer != null && renderer.sortingOrder < layer)
- {
- layer = renderer.sortingOrder;
- }
- }
- }
- }
- return layer;
- }
- public void SetBgPos(GameObject hitGameObj, Vector2 uiSize)
- {
- Vector2 size = hitGameObj.GetComponent<SpriteRenderer>().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);
- }
- }
- /// <summary>
- /// 将照片保存到本地
- /// </summary>
- 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
- }
- }
- }
|