PhotographUtil.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. int curMinLayer = GetMinLayer(parentObj);
  154. count = layer - curMinLayer;
  155. }
  156. else if (state == "down")
  157. {
  158. int curMaxLayer = GetMaxLayer(parentObj);
  159. count = layer - curMaxLayer;
  160. }
  161. int changeLayer = count;
  162. int maxLayer = int.MinValue;
  163. SpriteRenderer[] sps = parentObj.GetComponentsInChildren<SpriteRenderer>();//选中道具可能含有静态图
  164. for (int i = 0; i < sps.Length; i++)
  165. {
  166. int sortingOrder = sps[i].sortingOrder + changeLayer;
  167. sps[i].sortingOrder = sortingOrder;
  168. ParticleSystem[] particles = sps[i].gameObject.GetComponentsInChildren<ParticleSystem>();
  169. for (int j = 0; j < particles.Length; j++)//静态图可能是动画
  170. {
  171. var renderer = particles[j].GetComponent<Renderer>();
  172. if (renderer != null)
  173. {
  174. renderer.sortingOrder = sortingOrder;
  175. }
  176. }
  177. if (maxLayer < sortingOrder)
  178. {
  179. maxLayer = sortingOrder;
  180. }
  181. }
  182. for (int i = 0; i < parentObj.transform.childCount; i++)//选中道具可能含有特效
  183. {
  184. Transform tf = parentObj.transform.GetChild(i);
  185. string[] strs = tf.name.Split('_');
  186. if (strs.Length > 1 && strs[1].IndexOf("e") >= 0)//子物体是特效
  187. {
  188. DressUpUtil.SetRenderersOrder(tf.gameObject, changeLayer, true);
  189. }
  190. }
  191. CubismRenderController[] cubismRenders = parentObj.GetComponentsInChildren<CubismRenderController>();
  192. for (int i = 0; i < cubismRenders.Length; i++)
  193. {
  194. CubismRenderController render = cubismRenders[i];
  195. if (render != null && render.gameObject.activeSelf == true)
  196. {
  197. render.SortingOrder = render.SortingOrder + changeLayer;
  198. }
  199. }
  200. }
  201. private int GetMaxLayer(GameObject parentObj)
  202. {
  203. int layer = int.MinValue;
  204. for (int i = 0; i < parentObj.transform.childCount; i++)
  205. {
  206. GameObject gameObject = parentObj.transform.GetChild(i).gameObject;
  207. Transform tf = gameObject.transform;
  208. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  209. if (sp && layer < sp.sortingOrder)
  210. {
  211. layer = sp.sortingOrder;
  212. ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren<ParticleSystem>();
  213. for (int j = 0; j < particles.Length; j++)
  214. {
  215. var renderer = particles[j].GetComponent<Renderer>();
  216. if (renderer != null && layer < renderer.sortingOrder)
  217. {
  218. layer = renderer.sortingOrder;
  219. }
  220. }
  221. }
  222. }
  223. return layer;
  224. }
  225. public int GetMinLayer(GameObject parentObj)
  226. {
  227. int layer = int.MaxValue;
  228. for (int i = 0; i < parentObj.transform.childCount; i++)
  229. {
  230. GameObject gameObject = parentObj.transform.GetChild(i).gameObject;
  231. Transform tf = gameObject.transform;
  232. if (tf.gameObject.activeSelf == false) continue;
  233. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  234. if (sp && sp.sortingOrder < layer)
  235. {
  236. layer = sp.sortingOrder;
  237. ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren<ParticleSystem>();
  238. for (int j = 0; j < particles.Length; j++)
  239. {
  240. var renderer = particles[j].GetComponent<Renderer>();
  241. if (renderer != null && renderer.sortingOrder < layer)
  242. {
  243. layer = renderer.sortingOrder;
  244. }
  245. }
  246. }
  247. }
  248. return layer;
  249. }
  250. public void SetBgPos(GameObject hitGameObj, Vector2 uiSize)
  251. {
  252. Vector2 size = hitGameObj.GetComponent<SpriteRenderer>().size;
  253. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  254. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  255. Vector2 pos = hitGameObj.transform.position;
  256. if (pos.x <= -deviationWidth)
  257. {
  258. hitGameObj.transform.position = new Vector2(-deviationWidth, hitGameObj.transform.position.y);
  259. }
  260. if (pos.x >= deviationWidth)
  261. {
  262. hitGameObj.transform.position = new Vector2(deviationWidth, hitGameObj.transform.position.y);
  263. }
  264. if (pos.y <= -deviationHeigh)
  265. {
  266. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, -deviationHeigh);
  267. }
  268. if (pos.y >= deviationHeigh)
  269. {
  270. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, deviationHeigh);
  271. }
  272. }
  273. /// <summary>
  274. /// 将照片保存到本地
  275. /// </summary>
  276. public void SavePicturoToLocal(byte[] bytes, string fileName)
  277. {
  278. string path = Application.persistentDataPath + "/Pictures/WanshiJing/";
  279. //判断目录是否存在,不存在则会创建目录
  280. if (!Directory.Exists(path))
  281. {
  282. try
  283. {
  284. Directory.CreateDirectory(path);
  285. }
  286. catch (Exception exception)
  287. {
  288. throw new Exception("创建文件夹失败, error:" + exception.Message);
  289. }
  290. }
  291. var filePath = path + fileName;
  292. // byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  293. File.WriteAllBytes(filePath, bytes);
  294. UpdateSystemPhoto(filePath);
  295. }
  296. //调用iOS或Android原生方法保存图片后更新相册.
  297. private void UpdateSystemPhoto(string filePath)
  298. {
  299. #if UNITY_ANDROID
  300. var SaveImageJavaClass = new AndroidJavaClass("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  301. SaveImageJavaClass.CallStatic("scanFile", filePath, "已保存至本地"); //这里我们可以设置保存成功弹窗内容
  302. #endif
  303. }
  304. }
  305. }