PhotographUtil.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using FairyGUI;
  5. using Live2D.Cubism.Rendering;
  6. using UI.DressUp;
  7. using UnityEngine;
  8. namespace GFGGame
  9. {
  10. public class PhotographUtil : SingletonBase<PhotographUtil>
  11. {
  12. //传入父物体,返回边界大小
  13. public Vector2 GetGameObjectBoundsSize(GameObject parentObj)
  14. {
  15. float right = int.MinValue;
  16. float left = int.MaxValue;
  17. float top = int.MinValue;
  18. float bottom = int.MaxValue;
  19. BoxCollider2D[] boxColliders = parentObj.transform.GetComponentsInChildren<BoxCollider2D>();
  20. for (int i = 0; i < boxColliders.Length; i++)
  21. {
  22. Vector2 pos = boxColliders[i].transform.localPosition;
  23. Vector2 size = boxColliders[i].size * boxColliders[i].transform.localScale;
  24. right = Math.Max(size.x / 2 + pos.x, right);
  25. left = Math.Min(pos.x - size.x / 2, left);
  26. top = Math.Max(size.y / 2 + pos.y, top);
  27. bottom = Math.Min(pos.y - size.y / 2, bottom);
  28. }
  29. Vector2 boundsSize = new Vector2(right - left, top - bottom);
  30. boundsSize = boundsSize * 100 * Math.Abs(parentObj.transform.localScale.x);
  31. return boundsSize;
  32. }
  33. //设置物体中心点
  34. public void SetGameObjectCenter(GameObject parentObj)
  35. {
  36. Transform parent = parentObj.transform; // 2.选中技算
  37. Vector3 postion = parent.position;
  38. Quaternion rotation = parent.rotation;
  39. Vector3 scale = parent.localScale;
  40. parent.position = Vector3.zero;
  41. parent.rotation = Quaternion.Euler(Vector3.zero);
  42. parent.localScale = Vector3.one;
  43. Vector3 center = Vector3.zero;
  44. int index = 0;
  45. foreach (Transform t in parent)
  46. {
  47. string[] strs = t.name.Split('_');
  48. if (strs.Length > 1 && strs[1].IndexOf("e") >= 0) continue;//不计算特效大小
  49. BoxCollider2D render = t.GetComponent<BoxCollider2D>();
  50. if (render)
  51. {
  52. index++;
  53. center += render.bounds.center;
  54. }
  55. }
  56. if (index > 0) center /= index;
  57. Bounds bounds = new Bounds(center, Vector3.zero);
  58. foreach (Transform t in parent)
  59. {
  60. string[] strs = t.name.Split('_');
  61. if (strs.Length > 1 && strs[1].IndexOf("e") >= 0) continue;
  62. BoxCollider2D render = t.GetComponent<BoxCollider2D>();
  63. if (render) bounds.Encapsulate(render.bounds);
  64. }
  65. parent.position = postion;
  66. parent.rotation = rotation;
  67. parent.localScale = scale;
  68. foreach (Transform t in parent)
  69. {
  70. string[] strs = t.parent.name.Split('_');
  71. if (strs.Length > 1 && strs[1].IndexOf("e") >= 0) continue;
  72. t.position = t.position - bounds.center;
  73. }
  74. parent.position = bounds.center + parent.position;
  75. }
  76. public GameObject GetFirstHitObj(RaycastHit2D[] hit2Ds)
  77. {
  78. int layer = int.MinValue;
  79. GameObject gameObject = null;
  80. for (int i = 0; i < hit2Ds.Length; i++)
  81. {
  82. int gameobjMaxlayer = GetMaxLayer(hit2Ds[i].collider.transform.parent.gameObject);
  83. if (gameobjMaxlayer > layer)
  84. {
  85. layer = gameobjMaxlayer;
  86. gameObject = hit2Ds[i].collider.gameObject;
  87. }
  88. }
  89. return gameObject;
  90. }
  91. //是否点击在UI上
  92. public bool IsTouchUI(GComponent viewCom)
  93. {
  94. GObject obj = GRoot.inst.touchTarget;
  95. UI_PhotographUI _viewCom = UI_PhotographUI.Proxy(viewCom);
  96. 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;
  97. }
  98. public void SetLayer(GameObject hitGameObj, string state)
  99. {
  100. List<GameObject> itemGameObjs = PhotographDataManager.Instance.itemGameObjs;
  101. if (state != "refresh")
  102. {
  103. int index = itemGameObjs.IndexOf(hitGameObj);
  104. if (index == itemGameObjs.Count - 1 && state == "top") return;
  105. if (state != "top")
  106. {
  107. if (index < 0)
  108. {
  109. PromptController.Instance.ShowFloatTextPrompt("未选中任物品");
  110. return;
  111. }
  112. if (index == 0 && state == "down")
  113. {
  114. PromptController.Instance.ShowFloatTextPrompt("已经在最下层了");
  115. return;
  116. }
  117. if (index == itemGameObjs.Count - 1 && state == "up")
  118. {
  119. PromptController.Instance.ShowFloatTextPrompt("已经在最上层了");
  120. return;
  121. }
  122. }
  123. GameObject gameObject = itemGameObjs[index];
  124. itemGameObjs.RemoveAt(index);
  125. if (state == "up")
  126. {
  127. itemGameObjs.Insert((index + 1), gameObject);
  128. }
  129. else if (state == "down")
  130. {
  131. itemGameObjs.Insert((index - 1), gameObject);
  132. }
  133. else if (state == "top")
  134. {
  135. itemGameObjs.Add(gameObject);
  136. }
  137. else
  138. {
  139. PromptController.Instance.ShowFloatTextPrompt(state + "操作失败");
  140. return;
  141. }
  142. }
  143. for (int i = 0; i < itemGameObjs.Count; i++)
  144. {
  145. ChangeLayer(itemGameObjs[i], i * 300, state);
  146. }
  147. }
  148. public void ChangeLayer(GameObject parentObj, int layer, string state)
  149. {
  150. int count = 0;
  151. if (state == "up" || state == "top")
  152. {
  153. count = layer - GetMinLayer(parentObj);
  154. }
  155. else if (state == "down")
  156. {
  157. count = layer - GetMaxLayer(parentObj);
  158. }
  159. int changeLayer = layer + count;
  160. int maxLayer = int.MinValue;
  161. SpriteRenderer[] sps = parentObj.GetComponentsInChildren<SpriteRenderer>();//选中道具可能含有静态图
  162. for (int i = 0; i < sps.Length; i++)
  163. {
  164. int sortingOrder = sps[i].sortingOrder + changeLayer;
  165. sps[i].sortingOrder = sortingOrder;
  166. ParticleSystem[] particles = sps[i].gameObject.GetComponentsInChildren<ParticleSystem>();
  167. for (int j = 0; j < particles.Length; j++)//静态图可能是动画
  168. {
  169. var renderer = particles[j].GetComponent<Renderer>();
  170. if (renderer != null)
  171. {
  172. renderer.sortingOrder = sortingOrder;
  173. }
  174. }
  175. if (maxLayer < sortingOrder)
  176. {
  177. maxLayer = sortingOrder;
  178. }
  179. }
  180. for (int i = 0; i < parentObj.transform.childCount; i++)//选中道具可能含有特效
  181. {
  182. Transform tf = parentObj.transform.GetChild(i);
  183. string[] strs = tf.name.Split('_');
  184. if (strs.Length > 1 && strs[1].IndexOf("e") >= 0)//子物体是特效
  185. {
  186. DressUpUtil.SetRenderersOrder(tf.gameObject, changeLayer, true);
  187. }
  188. }
  189. CubismRenderController[] cubismRenders = parentObj.GetComponentsInChildren<CubismRenderController>();
  190. for (int i = 0; i < cubismRenders.Length; i++)
  191. {
  192. CubismRenderController render = cubismRenders[i];
  193. if (render != null && render.gameObject.activeSelf == true)
  194. {
  195. render.SortingOrder = render.SortingOrder + changeLayer;
  196. }
  197. }
  198. }
  199. private int GetMaxLayer(GameObject parentObj)
  200. {
  201. int layer = int.MinValue;
  202. for (int i = 0; i < parentObj.transform.childCount; i++)
  203. {
  204. GameObject gameObject = parentObj.transform.GetChild(i).gameObject;
  205. Transform tf = gameObject.transform;
  206. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  207. if (sp && layer < sp.sortingOrder)
  208. {
  209. layer = sp.sortingOrder;
  210. ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren<ParticleSystem>();
  211. for (int j = 0; j < particles.Length; j++)
  212. {
  213. var renderer = particles[j].GetComponent<Renderer>();
  214. if (renderer != null && layer < renderer.sortingOrder)
  215. {
  216. layer = renderer.sortingOrder;
  217. }
  218. }
  219. }
  220. }
  221. return layer;
  222. }
  223. public int GetMinLayer(GameObject parentObj)
  224. {
  225. int layer = int.MaxValue;
  226. for (int i = 0; i < parentObj.transform.childCount; i++)
  227. {
  228. GameObject gameObject = parentObj.transform.GetChild(i).gameObject;
  229. Transform tf = gameObject.transform;
  230. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  231. if (sp && sp.sortingOrder < layer)
  232. {
  233. layer = sp.sortingOrder;
  234. ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren<ParticleSystem>();
  235. for (int j = 0; j < particles.Length; j++)
  236. {
  237. var renderer = particles[j].GetComponent<Renderer>();
  238. if (renderer != null && renderer.sortingOrder < layer)
  239. {
  240. layer = renderer.sortingOrder;
  241. }
  242. }
  243. }
  244. }
  245. return layer;
  246. }
  247. public void SetBgPos(GameObject hitGameObj, Vector2 uiSize)
  248. {
  249. Vector2 size = hitGameObj.GetComponent<SpriteRenderer>().size;
  250. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  251. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  252. Vector2 pos = hitGameObj.transform.position;
  253. if (pos.x <= -deviationWidth)
  254. {
  255. hitGameObj.transform.position = new Vector2(-deviationWidth, hitGameObj.transform.position.y);
  256. }
  257. if (pos.x >= deviationWidth)
  258. {
  259. hitGameObj.transform.position = new Vector2(deviationWidth, hitGameObj.transform.position.y);
  260. }
  261. if (pos.y <= -deviationHeigh)
  262. {
  263. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, -deviationHeigh);
  264. }
  265. if (pos.y >= deviationHeigh)
  266. {
  267. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, deviationHeigh);
  268. }
  269. }
  270. /// <summary>
  271. /// 将照片保存到本地
  272. /// </summary>
  273. public void SavePicturoToLocal(byte[] bytes, string fileName)
  274. {
  275. string path = Application.persistentDataPath + "/Pictures/WanshiJing/";
  276. //判断目录是否存在,不存在则会创建目录
  277. if (!Directory.Exists(path))
  278. {
  279. try
  280. {
  281. Directory.CreateDirectory(path);
  282. }
  283. catch (Exception exception)
  284. {
  285. throw new Exception("创建文件夹失败, error:" + exception.Message);
  286. }
  287. }
  288. var filePath = path + fileName;
  289. // byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  290. File.WriteAllBytes(filePath, bytes);
  291. UpdateSystemPhoto(filePath);
  292. }
  293. //调用iOS或Android原生方法保存图片后更新相册.
  294. private void UpdateSystemPhoto(string filePath)
  295. {
  296. #if UNITY_ANDROID
  297. var SaveImageJavaClass = new AndroidJavaClass("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  298. SaveImageJavaClass.CallStatic("scanFile", filePath, "已保存至本地"); //这里我们可以设置保存成功弹窗内容
  299. #endif
  300. }
  301. }
  302. }