BattleStaticFunc.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace YLBattle
  4. {
  5. /// <summary>
  6. /// 战场静态公共函数
  7. /// </summary>
  8. public class BattleStaticFunc
  9. {
  10. /// <summary>
  11. /// 查找Transform
  12. /// </summary>
  13. /// <param name="tran">父级</param>
  14. /// <param name="name">名称</param>
  15. /// <returns></returns>
  16. public static Transform FindTransform(Transform tran, string name)
  17. {
  18. Transform[] trans = tran.GetComponentsInChildren<Transform>();
  19. for (int i = 0; i < trans.Length; i++)
  20. {
  21. if (trans[i].name == name)
  22. {
  23. return trans[i];
  24. }
  25. }
  26. return null;
  27. }
  28. /// <summary>
  29. /// 查找GameObject
  30. /// </summary>
  31. /// <param name="tran">父级</param>
  32. /// <param name="name">名称</param>
  33. /// <returns></returns>
  34. public static GameObject FindGameObject(Transform tran, string name)
  35. {
  36. Transform[] trans = tran.GetComponentsInChildren<Transform>();
  37. for (int i = 0; i < trans.Length; i++)
  38. {
  39. if (trans[i].name == name)
  40. {
  41. return trans[i].gameObject;
  42. }
  43. }
  44. return null;
  45. }
  46. /// <summary>
  47. ///
  48. /// </summary>
  49. /// <param name="tran"></param>
  50. /// <param name="layer"></param>
  51. public static void TranformResetLayer(Transform tran, int layer)
  52. {
  53. Transform[] all = tran.GetComponentsInChildren<Transform>();
  54. foreach (Transform rc in all)
  55. {
  56. rc.gameObject.layer = LayerMask.NameToLayer("BattleScreen"); //指定Layer
  57. }
  58. }
  59. /// <summary>
  60. ///
  61. /// </summary>
  62. static string hierarchyPath;
  63. /// <summary>
  64. ///
  65. /// </summary>
  66. /// <param name="t"></param>
  67. /// <param name="initPath"></param>
  68. /// <returns></returns>
  69. public static string GetHierarchyPath(Transform t, bool initPath = true)
  70. {
  71. if (initPath) hierarchyPath = "";
  72. if (t.parent != null)
  73. {
  74. hierarchyPath = t.name + hierarchyPath;
  75. Transform parent = t.parent;
  76. if (t.parent.parent != null)
  77. {
  78. hierarchyPath = "/" + hierarchyPath;
  79. }
  80. GetHierarchyPath(parent, false);
  81. }
  82. return hierarchyPath;
  83. }
  84. /// <summary>
  85. ///
  86. /// </summary>
  87. static Component result = null;
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <param name="t"></param>
  92. /// <param name="scriptName"></param>
  93. /// <param name="initPath"></param>
  94. /// <returns></returns>
  95. public static Component GetParentScript(Transform t, string scriptName, bool initPath = true)
  96. {
  97. if (initPath)
  98. {
  99. result = t.gameObject.GetComponent(scriptName);
  100. if (result != null)
  101. {
  102. return result;
  103. }
  104. }
  105. if (t.gameObject.GetComponent(scriptName) != null)
  106. {
  107. result = t.gameObject.GetComponent(scriptName);
  108. }
  109. else if (t.parent != null)
  110. {
  111. result = GetParentScript(t.parent, scriptName, false);
  112. }
  113. return result;
  114. }
  115. /// <summary>
  116. ///
  117. /// </summary>
  118. static GameObject resultTran = null;
  119. /// <summary>
  120. ///
  121. /// </summary>
  122. /// <param name="t"></param>
  123. /// <param name="objName"></param>
  124. /// <param name="initPath"></param>
  125. /// <returns></returns>
  126. public static GameObject GetUpGameObject(Transform t, string objName, bool initPath = true)
  127. {
  128. if (initPath)
  129. {
  130. if (t.gameObject.name == objName)
  131. {
  132. return t.gameObject;
  133. }
  134. resultTran = FindGameObject(t, objName);
  135. if (resultTran != null)
  136. {
  137. return resultTran;
  138. }
  139. }
  140. if (t.Find(objName) != null)
  141. {
  142. resultTran = t.Find(objName).gameObject;
  143. }
  144. else if (t.parent != null)
  145. {
  146. resultTran = GetUpGameObject(t.parent, objName, false);
  147. }
  148. return resultTran;
  149. }
  150. /// <summary>
  151. /// 获取括号内数值
  152. /// </summary>
  153. /// <param name="_value"></param>
  154. /// <returns></returns>
  155. public static string GetInSideKuoHaoValue(string _value)
  156. {
  157. string result = string.Empty;
  158. if (_value.IndexOf('(') != -1)
  159. {
  160. int _startIndex = _value.IndexOf('(') + 1;
  161. int _endIndex = _value.IndexOf(')');
  162. result = _value.Substring(_startIndex, _endIndex - _startIndex);
  163. return result;
  164. }
  165. return result;
  166. }
  167. /// <summary>
  168. /// 获取括号外数值
  169. /// </summary>
  170. /// <param name="_value"></param>
  171. /// <returns></returns>
  172. public static string GetOutSideKuoHaoValue(string _value)
  173. {
  174. string result = string.Empty;
  175. if (_value.IndexOf('(') != -1)
  176. {
  177. int _startIndex = _value.IndexOf('(');
  178. result = _value.Substring(0, _startIndex);
  179. return result;
  180. }
  181. result = _value;
  182. return result;
  183. }
  184. /// <summary>
  185. /// 计算特殊字符串
  186. /// </summary>
  187. /// <param name="index">索引(等级)</param>
  188. /// <param name="baseData">基础数据</param>
  189. /// <param name="adData">物理</param>
  190. /// <param name="apData">魔法</param>
  191. /// <param name="trgString">待分析数值</param>
  192. /// <returns></returns>
  193. public static int CalculateSpecialString(LogicFighter caster, int index, float baseData, string trgString)
  194. {
  195. int result1 = 0;
  196. int result2 = 0;
  197. float adData = caster.GetProperty(EBattleProperty.ATTACK);
  198. float apData = caster.GetProperty(EBattleProperty.MAGICSTRENGTH);
  199. string baseValue = string.Empty;
  200. string extraValue = string.Empty;
  201. string outSideValue = GetOutSideKuoHaoValue(trgString);
  202. string inSideValue = GetInSideKuoHaoValue(trgString);
  203. if (outSideValue.IndexOf('/') != -1)
  204. {
  205. string[] outSideAry = outSideValue.Split('/');
  206. if (index <= outSideAry.Length)
  207. {
  208. baseValue = outSideAry[index - 1];
  209. }
  210. else
  211. {
  212. baseValue = outSideAry[outSideAry.Length - 1];
  213. }
  214. }
  215. else
  216. {
  217. baseValue = outSideValue;
  218. }
  219. int rate1 = 1;
  220. if (baseValue.IndexOf('F') != -1)
  221. {
  222. rate1 = 100;
  223. baseValue = baseValue.Substring(0, baseValue.IndexOf('F'));
  224. result1 = (int)(baseData * int.Parse(baseValue) / rate1);
  225. }
  226. else
  227. {
  228. result1 = int.Parse(baseValue);
  229. }
  230. string _aValue = "0";
  231. int rate2 = 1;
  232. float typeValue = 0;
  233. if (inSideValue != string.Empty)
  234. {
  235. _aValue = inSideValue;
  236. if (inSideValue.IndexOf('F') != -1)
  237. {
  238. rate2 = 100;
  239. _aValue = _aValue.Substring(0, inSideValue.IndexOf('F'));
  240. }
  241. string type = inSideValue.Substring(inSideValue.IndexOf('F') + 1, 2);
  242. switch (type)
  243. {
  244. case "AP":
  245. typeValue = apData;
  246. break;
  247. case "AD":
  248. typeValue = adData;
  249. break;
  250. }
  251. }
  252. result2 = (int)(typeValue * int.Parse(_aValue) / rate2);
  253. return result1 + result2;
  254. }
  255. ///// <summary>
  256. ///// 获取栗子特效时间 (未启用)
  257. ///// </summary>
  258. ///// <param name="transform"></param>
  259. ///// <returns></returns>
  260. //public static float ParticleSystemLength(Transform tr)
  261. //{
  262. // ParticleSystem[] particleSystems = tr.GetComponentsInChildren<ParticleSystem>();
  263. // float maxDuration = 0;
  264. // foreach (ParticleSystem ps in particleSystems)
  265. // {
  266. // if (ps.enableEmission)
  267. // {
  268. // if (ps.loop)
  269. // {
  270. // return -1f;
  271. // }
  272. // float dunration = 0f;
  273. // if (ps.emissionRate <= 0)
  274. // {
  275. // dunration = ps.startDelay + ps.startLifetime;
  276. // }
  277. // else
  278. // {
  279. // dunration = ps.startDelay + Mathf.Max(ps.duration, ps.startLifetime);
  280. // }
  281. // if (dunration > maxDuration)
  282. // {
  283. // maxDuration = dunration;
  284. // }
  285. // }
  286. // }
  287. // return maxDuration;
  288. //}
  289. }
  290. }