Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. using Photon.Pun;
  2. using Photon.Realtime;
  3. using Timer = UnityEngine.Time;
  4. using UnityEngine;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. /// <summary>
  9. /// Plugin For PhotonPun 0.2
  10. /// </summary>
  11. public class Network
  12. {
  13. private static Network myClass;
  14. private float timer;
  15. /// <summary>
  16. /// Instance of Network class
  17. /// </summary>
  18. public static Network Get
  19. {
  20. get { return myClass ?? (myClass = new Network()); }
  21. }
  22. /// <summary>
  23. /// Destroy object in Network or offline mode
  24. /// </summary>
  25. /// <param name="Object"> GameObject requires PhotonView </param>
  26. /// <param name="Offline">Set this true, if code must run in offline</param>
  27. public static void Destroy(GameObject Object, bool Offline)
  28. {
  29. if (Object != null)
  30. {
  31. if (!IsOffline())
  32. {
  33. if (Object.GetPhotonView().IsMine)
  34. {
  35. PhotonNetwork.Destroy(Object);
  36. }
  37. else
  38. {
  39. Object.SetActive(false);
  40. }
  41. }
  42. else
  43. if (Offline)
  44. {
  45. {
  46. UnityEngine.Object.Destroy(Object);
  47. }
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// Destroy Timer Use StartCoroutine(Network.Get.Destroy());
  53. /// </summary>
  54. /// <param name="Object">GameObject requires PhotonView </param>
  55. /// <param name="Offline">Set this true, if code must run in offline</param>
  56. /// <param name="time"></param>
  57. /// <returns></returns>
  58. public IEnumerator Destroy(GameObject Object, bool Offline, float time)
  59. {
  60. yield return new WaitForSeconds(time);
  61. if (Object != null)
  62. {
  63. if (Object.activeInHierarchy)
  64. {
  65. Destroy(Object, Offline);
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Instantiate in network or offline mode
  71. /// </summary>
  72. /// <param name="IsSceneObject">Set this to true if is not a Player and not spawned by Player</param>
  73. /// <param name="Object">The prefab must be in a folder called Resources. more information in doc photon pun</param>
  74. /// <param name="Position"></param>
  75. /// <param name="Rotation"></param>
  76. /// <returns></returns>
  77. public static GameObject Instantiate(bool IsSceneObject, GameObject Object, Vector3 Position, Quaternion Rotation)
  78. {
  79. if (!IsOffline())
  80. {
  81. if (IsSceneObject)
  82. {
  83. return PhotonNetwork.InstantiateSceneObject(Object.name, Position, Rotation);
  84. }
  85. else
  86. {
  87. return PhotonNetwork.Instantiate(Object.name, Position, Rotation);
  88. }
  89. }
  90. else
  91. {
  92. return UnityEngine.Object.Instantiate(Object, Position, Rotation);
  93. }
  94. }
  95. /// <summary>
  96. /// Instantiate in network or offline mode
  97. /// </summary>
  98. /// <param name="IsSceneObject">Set this to true if is not a Player and not spawned by Player</param>
  99. /// <param name="Object">The prefab must be in a folder called Resources. more information in doc photon pun</param>
  100. /// <param name="Position"></param>
  101. /// <param name="Rotation"></param>
  102. /// <param name="group"></param>
  103. /// <returns></returns>
  104. public static GameObject Instantiate(bool IsSceneObject, GameObject Object, Vector3 Position, Quaternion Rotation, byte group)
  105. {
  106. if (!IsOffline() && PhotonNetwork.InRoom)
  107. {
  108. if (IsSceneObject)
  109. {
  110. return PhotonNetwork.InstantiateSceneObject(Object.name, Position, Rotation, group);
  111. }
  112. else
  113. {
  114. return PhotonNetwork.Instantiate(Object.name, Position, Rotation, group);
  115. }
  116. }
  117. else
  118. {
  119. return UnityEngine.Object.Instantiate(Object, Position, Rotation);
  120. }
  121. }
  122. /// <summary>
  123. /// Instantiate in network or offline mode
  124. /// </summary>
  125. /// <param name="IsSceneObject">Set this to true if is not a Player and not spawned by Player</param>
  126. /// <param name="Object">The prefab must be in a folder called Resources. More information in doc photon pun</param>
  127. /// <param name="Position"></param>
  128. /// <param name="Rotation"></param>
  129. /// <param name="group"></param>
  130. /// <param name="data"></param>
  131. /// <returns></returns>
  132. public static GameObject Instantiate(bool IsSceneObject, GameObject Object, Vector3 Position, Quaternion Rotation, byte group, object[] data)
  133. {
  134. if (!IsOffline())
  135. {
  136. if (IsSceneObject)
  137. {
  138. return PhotonNetwork.InstantiateSceneObject(Object.name, Position, Rotation, group, data);
  139. }
  140. else
  141. {
  142. return PhotonNetwork.Instantiate(Object.name, Position, Rotation, group, data);
  143. }
  144. }
  145. else
  146. {
  147. return UnityEngine.Object.Instantiate(Object, Position, Rotation);
  148. }
  149. }
  150. /// <summary>
  151. /// return true if Is Connected,Ready and IsMine
  152. /// </summary>
  153. /// <param name="photonView"></param>
  154. /// <param name="Offline">Set this true, if code must run in offline</param>
  155. /// <returns></returns>
  156. public bool IsConnectedAndIsMine(PhotonView photonView, bool Offline)
  157. {
  158. if (!IsOffline())
  159. {
  160. if (photonView != null)
  161. {
  162. if (photonView.IsMine)
  163. {
  164. return true;
  165. }
  166. else
  167. {
  168. return false;
  169. }
  170. }
  171. else
  172. {
  173. Debug.LogWarning("PhotonView == null Return false");
  174. return false;
  175. }
  176. }
  177. else if (Offline)
  178. {
  179. return true;
  180. }
  181. else
  182. {
  183. return false;
  184. }
  185. }
  186. public static bool IsOffline()
  187. {
  188. return !PhotonNetwork.IsConnectedAndReady;
  189. }
  190. /// <summary>
  191. /// if is offlone return false
  192. /// </summary>
  193. public static bool IsMaster()
  194. {
  195. if (!IsOffline())
  196. {
  197. return PhotonNetwork.IsMasterClient;
  198. }
  199. else
  200. {
  201. return false;
  202. }
  203. }
  204. public void ChangeRegion(Region region)
  205. {
  206. if (region != Region.ind)
  207. {
  208. PhotonNetwork.ConnectToRegion(region.ToString());
  209. }
  210. else
  211. {
  212. PhotonNetwork.ConnectToRegion("in");
  213. }
  214. }
  215. public static readonly string[] RegionNames = new string[12]
  216. {
  217. //Region | Token
  218. "Asia",// asia
  219. "Australia",// au
  220. "Canada East",// cae
  221. "Europe",// eu
  222. "India",// ind
  223. "Japan",// jp
  224. "Russia",// ru
  225. "Russia East",// rue
  226. "South America",// sa
  227. "South Korea",// kr
  228. "USA East",// us
  229. "USA West"// usw
  230. };
  231. public enum Region
  232. {
  233. asia,
  234. au,
  235. cae,
  236. eu,
  237. ind,
  238. jp,
  239. ru,
  240. rue,
  241. sa,
  242. kr,
  243. us,
  244. usw,
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement