Advertisement
whiteflare

Untitled

Feb 22nd, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.90 KB | None | 0 0
  1.  
  2. using UdonSharp;
  3. using UnityEngine;
  4. using VRC.SDKBase;
  5. using VRC.Udon;
  6.  
  7. [UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
  8.  
  9. public class LurasSwitch : UdonSharpBehaviour
  10. {
  11. [Header("------------------------------------------------------------------")]
  12. [Header("■■■ 機能切り替えスライダー/Function switching slider ■■■")]
  13. [Header("------------------------------------------------------------------")]
  14. [Header("[0]トグルスイッチ/ToggleSwitch")]
  15. [Header("[1] シーケンススイッチ/Sequence switch")]
  16. [Header("[2] 位置リセットスイッチ/Position Reset switch (Global)")]
  17.  
  18. [Space(20)]
  19.  
  20. [Range(0, 2)] public int switchID;
  21.  
  22. [Space(10)]
  23.  
  24. [SerializeField] private bool isGlobal = false;
  25.  
  26. [SerializeField] private bool masterOnly = false;
  27.  
  28. [SerializeField] private bool instanceOwnerOnly = false;
  29.  
  30. [Space(10)]
  31.  
  32. [Header("------------------------------------------------------------------")]
  33.  
  34. [Header("ターゲットオブジェクトのsizeに数を入れて切り替えたいオブジェクトをドラッグ&ドロップ")]
  35. [Header("Enter a number in size and drag and drop the object you want to switch")]
  36. [Header("TargetObjectには空きが出ないようにする必要があります")]
  37. [Header("TargetObject should not be left empty")]
  38.  
  39.  
  40. [Space(10)]
  41.  
  42. [SerializeField] private GameObject[] targetObject;
  43.  
  44. private Vector3[] defaultPosition;
  45. private Quaternion[] defaultRotation;
  46. private Rigidbody[] targetRigidbody;
  47. private int localActiveObjectIndex = 0;
  48. [UdonSynced] int globalActiveObjectIndex = 0;
  49. [UdonSynced] bool[] isEnabled;
  50.  
  51.  
  52. void Start()
  53. {
  54. isEnabled = new bool[targetObject.Length]; //配列を初期化
  55.  
  56. switch (switchID)
  57. {
  58. case 0:
  59. if (isGlobal)
  60. {
  61.  
  62.  
  63. for (int x = 0; x < targetObject.Length; x = x + 1)
  64. {
  65. if (targetObject[x] != null) //配列内のNullチェック
  66. {
  67. isEnabled[x] = targetObject[x].activeSelf; //現在の状態を配列の同期変数に代入
  68. }
  69. }
  70. }
  71. break;
  72.  
  73. case 1:
  74. if (isGlobal)
  75. {
  76. SwitchActiveObjectGlobal();
  77. }
  78. break;
  79.  
  80. case 2:
  81.  
  82. defaultPosition = new Vector3[targetObject.Length];
  83. defaultRotation = new Quaternion[targetObject.Length];
  84. targetRigidbody = new Rigidbody[targetObject.Length];
  85.  
  86. for (int i = 0; i < targetObject.Length; i++)
  87. {
  88. if (targetObject[i] != null)
  89. {
  90. defaultPosition[i] = targetObject[i].transform.position;
  91. defaultRotation[i] = targetObject[i].transform.rotation;
  92. targetRigidbody[i] = targetObject[i].GetComponent<Rigidbody>();
  93. }
  94. }
  95. break;
  96. }
  97. }
  98.  
  99. private bool _IsIgnoreUser()
  100. {
  101. if (masterOnly && instanceOwnerOnly)
  102. {
  103. return !Networking.IsInstanceOwner && !Networking.IsMaster;
  104. }
  105. if (masterOnly)
  106. {
  107. return !Networking.IsMaster;
  108. }
  109. if (instanceOwnerOnly)
  110. {
  111. return !Networking.IsInstanceOwner;
  112. }
  113. return false;
  114. }
  115.  
  116. public override void Interact()
  117. {
  118. if (_IsIgnoreUser())
  119. {
  120. return;
  121. }
  122.  
  123. switch (switchID)
  124. {
  125. case 0:
  126. // SwitchType -ToggleObject-
  127.  
  128. if (!isGlobal)
  129. {
  130. //Local
  131. ToggleObjectLocal(); //ObjectIndexのオンオフを反転させる
  132. }
  133. if (isGlobal)
  134. {
  135. //Global
  136.  
  137. if (!Networking.IsOwner(gameObject))
  138. {
  139. Networking.SetOwner(Networking.LocalPlayer, gameObject);
  140. }
  141. for (int x = 0; x < targetObject.Length; x = x + 1)
  142. {
  143. if (targetObject[x] != null) //配列内のNullチェック
  144. {
  145. isEnabled[x] = !isEnabled[x]; //同期変数のBoolを反転
  146. }
  147. }
  148.  
  149. SetObjectFromGlobal(); //オブジェクトのアクティブを切り替え
  150. RequestSerialization(); //同期をリクエスト
  151. }
  152. break;
  153.  
  154. case 1:
  155. // SwitchType -SwitchSequenceObjectLocal-
  156.  
  157. if (!isGlobal)
  158. {
  159. //Local
  160. NextObjectIndexLocal(); //次のObjectIndexに切り替える(Local)
  161. }
  162. else
  163. {
  164. //Global
  165. if (!Networking.IsOwner(gameObject))
  166. {
  167. Networking.SetOwner(Networking.LocalPlayer, gameObject);
  168. }
  169. NextObjectIndexGlobal(); //次のObjectIndexに切り替える(Global)
  170. }
  171. break;
  172.  
  173. case 2:
  174. //SwitchType -Position Reset Switch-
  175. for (int i = 0; i < targetObject.Length; i++)
  176. {
  177. Networking.SetOwner(Networking.LocalPlayer, targetObject[i]);
  178. }
  179.  
  180. for (int i = 0; i < targetObject.Length; i++)
  181. {
  182. if (targetObject[i] != null)
  183. {
  184. if (defaultPosition[i] != null)
  185. {
  186. targetObject[i].transform.position = defaultPosition[i];
  187. targetObject[i].transform.rotation = defaultRotation[i];
  188. targetRigidbody[i].Sleep();
  189. }
  190. }
  191. }
  192. break;
  193. }
  194. }
  195.  
  196. public override void OnDeserialization()
  197. {
  198. if (isGlobal)
  199. {
  200. switch (switchID)
  201. {
  202. case 0:
  203. if (!Networking.IsOwner(gameObject))
  204. {
  205. SetObjectFromGlobal(); //受信したisEnabledの状態を反映
  206. }
  207.  
  208. break;
  209.  
  210. case 1:
  211. if (!Networking.IsOwner(gameObject))
  212. {
  213. SwitchActiveObjectGlobal(); //受信したactiveObjectIndexを反映
  214. }
  215.  
  216. break;
  217. }
  218. }
  219. }
  220.  
  221. public void ToggleObjectLocal() //オブジェクトのアクティブを切り替え
  222. {
  223. for (int x = 0; x < targetObject.Length; x = x + 1)
  224. {
  225. if (targetObject[x] != null) //配列内のNullチェック
  226. {
  227. targetObject[x].SetActive(!targetObject[x].activeSelf); //現在の状態を確認してオブジェクトのアクティブを反転
  228. }
  229. }
  230. }
  231.  
  232. public void SetObjectFromGlobal()
  233. {
  234. for (int x = 0; x < targetObject.Length; x = x + 1)
  235. {
  236. if (targetObject[x] != null) //配列内のNullチェック
  237. {
  238. targetObject[x].SetActive(isEnabled[x]); //現在の状態を確認してオブジェクトのアクティブを反転
  239. }
  240. }
  241. }
  242.  
  243. private void SwitchActiveObjectLocal() //activeObjectIndexをセットして反映させる
  244. {
  245. for (int x = 0; x < targetObject.Length; x = x + 1)
  246. {
  247. if (targetObject[x] != null) //配列内のNullチェック
  248. {
  249. targetObject[x].SetActive(x == localActiveObjectIndex); //番号に対応したオブジェクトをオンにする
  250. }
  251. }
  252. }
  253.  
  254. private void SwitchActiveObjectGlobal() //activeObjectIndexをセットして反映させる
  255. {
  256. for (int x = 0; x < targetObject.Length; x = x + 1)
  257. {
  258. if (targetObject[x] != null) //配列内のNullチェック
  259. {
  260. targetObject[x].SetActive(x == globalActiveObjectIndex); //番号に対応したオブジェクトをオンにする
  261. }
  262. }
  263. }
  264.  
  265. private void AddActiveObjectIndexLocal() //activeObjectIndexに1を足す(Local)
  266. {
  267. localActiveObjectIndex = localActiveObjectIndex + 1;
  268.  
  269. if (localActiveObjectIndex >= targetObject.Length)
  270. {
  271. localActiveObjectIndex = 0;
  272. }
  273. }
  274.  
  275. private void AddActiveObjectIndexGlobal() //activeObjectIndexに1を足す(Global)
  276. {
  277. globalActiveObjectIndex = globalActiveObjectIndex + 1;
  278.  
  279. if (globalActiveObjectIndex >= targetObject.Length)
  280. {
  281. globalActiveObjectIndex = 0;
  282. }
  283. }
  284.  
  285. private void NextObjectIndexLocal() //次のObjectIndexに切り替える(Local)
  286. {
  287. AddActiveObjectIndexLocal(); //activeObjectIndexに1を足す
  288. SwitchActiveObjectLocal(); //activeObjectIndexをセットして反映させる(Local)
  289. }
  290.  
  291. private void NextObjectIndexGlobal() //次のObjectIndexに切り替える(Global)
  292. {
  293. AddActiveObjectIndexGlobal(); //activeObjectIndexに1を足す
  294. SwitchActiveObjectGlobal(); //activeObjectIndexをセットして反映させる(Global)
  295. RequestSerialization();
  296. }
  297. }
  298.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement