PhotographUtil.cs 13 KB

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