PhotographUtil.cs 13 KB

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