PhotographUtil.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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] == "eff") continue;//不计算特效大小
  49. BoxCollider2D render = t.GetComponent<BoxCollider2D>();
  50. if (render)
  51. {
  52. index++;
  53. center += render.bounds.center;
  54. }
  55. }
  56. 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] == "eff") 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] == "eff") 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 (state != "top")
  105. {
  106. if (index < 0)
  107. {
  108. PromptController.Instance.ShowFloatTextPrompt("未选中任物品");
  109. return;
  110. }
  111. if (index == 0 && state == "down")
  112. {
  113. PromptController.Instance.ShowFloatTextPrompt("已经在最下层了");
  114. return;
  115. }
  116. if (index == itemGameObjs.Count - 1 && state == "up")
  117. {
  118. PromptController.Instance.ShowFloatTextPrompt("已经在最上层了");
  119. return;
  120. }
  121. }
  122. GameObject gameObject = itemGameObjs[index];
  123. itemGameObjs.RemoveAt(index);
  124. if (state == "up")
  125. {
  126. itemGameObjs.Insert((index + 1), gameObject);
  127. }
  128. else if (state == "down")
  129. {
  130. itemGameObjs.Insert((index - 1), gameObject);
  131. }
  132. else if (state == "top")
  133. {
  134. itemGameObjs.Add(gameObject);
  135. }
  136. else
  137. {
  138. PromptController.Instance.ShowFloatTextPrompt(state + "操作失败");
  139. return;
  140. }
  141. }
  142. for (int i = 0; i < itemGameObjs.Count; i++)
  143. {
  144. ChangeLayer(itemGameObjs[i], i * 300, state);
  145. }
  146. }
  147. public void ChangeLayer(GameObject parentObj, int layer, string state)
  148. {
  149. int count = 0;
  150. if (state == "up" || state == "top")
  151. {
  152. count = layer - GetMinLayer(parentObj);
  153. }
  154. else if (state == "down")
  155. {
  156. count = layer - GetMaxLayer(parentObj);
  157. }
  158. int changeLayer = layer + count;
  159. int maxLayer = int.MinValue;
  160. SpriteRenderer[] sps = parentObj.GetComponentsInChildren<SpriteRenderer>();//选中道具可能含有静态图
  161. for (int i = 0; i < sps.Length; i++)
  162. {
  163. int sortingOrder = sps[i].sortingOrder + changeLayer;
  164. sps[i].sortingOrder = sortingOrder;
  165. ParticleSystem[] particles = sps[i].gameObject.GetComponentsInChildren<ParticleSystem>();
  166. for (int j = 0; j < particles.Length; j++)//静态图可能是动画
  167. {
  168. var renderer = particles[j].GetComponent<Renderer>();
  169. if (renderer != null)
  170. {
  171. renderer.sortingOrder = sortingOrder;
  172. }
  173. }
  174. if (maxLayer < sortingOrder)
  175. {
  176. maxLayer = sortingOrder;
  177. }
  178. }
  179. for (int i = 0; i < parentObj.transform.childCount; i++)//选中道具可能含有特效
  180. {
  181. Transform tf = parentObj.transform.GetChild(i);
  182. string[] strs = tf.name.Split('_');
  183. if (strs.Length > 1 && strs[1] == "eff")//子物体是特效
  184. {
  185. DressUpUtil.SetRenderersOrder(tf.gameObject, changeLayer, true);
  186. }
  187. }
  188. CubismRenderController[] cubismRenders = parentObj.GetComponentsInChildren<CubismRenderController>();
  189. for (int i = 0; i < cubismRenders.Length; i++)
  190. {
  191. CubismRenderController render = cubismRenders[i];
  192. if (render != null && render.gameObject.activeSelf == true)
  193. {
  194. render.SortingOrder = render.SortingOrder + changeLayer;
  195. }
  196. }
  197. }
  198. private int GetMaxLayer(GameObject parentObj)
  199. {
  200. int layer = int.MinValue;
  201. for (int i = 0; i < parentObj.transform.childCount; i++)
  202. {
  203. GameObject gameObject = parentObj.transform.GetChild(i).gameObject;
  204. Transform tf = gameObject.transform;
  205. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  206. if (sp && layer < sp.sortingOrder)
  207. {
  208. layer = sp.sortingOrder;
  209. ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren<ParticleSystem>();
  210. for (int j = 0; j < particles.Length; j++)
  211. {
  212. var renderer = particles[j].GetComponent<Renderer>();
  213. if (renderer != null && layer < renderer.sortingOrder)
  214. {
  215. layer = renderer.sortingOrder;
  216. }
  217. }
  218. }
  219. }
  220. return layer;
  221. }
  222. public int GetMinLayer(GameObject parentObj)
  223. {
  224. int layer = int.MaxValue;
  225. for (int i = 0; i < parentObj.transform.childCount; i++)
  226. {
  227. GameObject gameObject = parentObj.transform.GetChild(i).gameObject;
  228. Transform tf = gameObject.transform;
  229. SpriteRenderer sp = tf.GetComponent<SpriteRenderer>();
  230. if (sp && sp.sortingOrder < layer)
  231. {
  232. layer = sp.sortingOrder;
  233. ParticleSystem[] particles = sp.gameObject.GetComponentsInChildren<ParticleSystem>();
  234. for (int j = 0; j < particles.Length; j++)
  235. {
  236. var renderer = particles[j].GetComponent<Renderer>();
  237. if (renderer != null && renderer.sortingOrder < layer)
  238. {
  239. layer = renderer.sortingOrder;
  240. }
  241. }
  242. }
  243. }
  244. return layer;
  245. }
  246. public void SetBgPos(GameObject hitGameObj, Vector2 uiSize)
  247. {
  248. Vector2 size = hitGameObj.GetComponent<SpriteRenderer>().size;
  249. float deviationWidth = (size.x - uiSize.x / 100) / 2;
  250. float deviationHeigh = (size.y - uiSize.y / 100) / 2;
  251. Vector2 pos = hitGameObj.transform.position;
  252. if (pos.x <= -deviationWidth)
  253. {
  254. hitGameObj.transform.position = new Vector2(-deviationWidth, hitGameObj.transform.position.y);
  255. }
  256. if (pos.x >= deviationWidth)
  257. {
  258. hitGameObj.transform.position = new Vector2(deviationWidth, hitGameObj.transform.position.y);
  259. }
  260. if (pos.y <= -deviationHeigh)
  261. {
  262. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, -deviationHeigh);
  263. }
  264. if (pos.y >= deviationHeigh)
  265. {
  266. hitGameObj.transform.position = new Vector2(hitGameObj.transform.position.x, deviationHeigh);
  267. }
  268. }
  269. /// <summary>
  270. /// 将照片保存到本地
  271. /// </summary>
  272. public void SavePicturoToLocal(byte[] bytes, string fileName)
  273. {
  274. string path = Application.persistentDataPath + "/Pictures/WanshiJing/";
  275. //判断目录是否存在,不存在则会创建目录
  276. if (!Directory.Exists(path))
  277. {
  278. try
  279. {
  280. Directory.CreateDirectory(path);
  281. }
  282. catch (Exception exception)
  283. {
  284. throw new Exception("创建文件夹失败, error:" + exception.Message);
  285. }
  286. }
  287. var filePath = path + fileName;
  288. // byte[] bytes = tex.EncodeToJPG();//将纹理数据,转化成一个jpg图片
  289. File.WriteAllBytes(filePath, bytes);
  290. UpdateSystemPhoto(filePath);
  291. }
  292. //调用iOS或Android原生方法保存图片后更新相册.
  293. private void UpdateSystemPhoto(string filePath)
  294. {
  295. #if UNITY_ANDROID
  296. AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.gfg.gfglibrary.SaveImage"); //设置成我们aar库中的签名+类名
  297. androidJavaObject.Call("scanFile", filePath, "已保存至本地"); //这里我们可以设置保存成功弹窗内容
  298. #endif
  299. }
  300. }
  301. }