using System.Collections.Generic; using PhysicsTools; using UnityEngine; using UnityEngine.UI; public class Scene3Events : MonoBehaviour { private Rope mainRope; public GameObject prefab; private List lstPrefab; private List freeSegments; public Text txtHelp; private void Start() { string text = "Camera Control\r\nDrag Mouse To Rotate\r\nArrow Keys and Page Up/Down to move\r\n\r\n'Space': Throw a ball in direction of camera\r\n'r': Reset the scene\r\n'i': Insert prefab randomly\r\n'd': Delete prefab randomly"; txtHelp.text = text; lstPrefab = new List(); freeSegments = new List(); } private void attachNewPrefabToMainRope() { if (mainRope == null) { mainRope = GameObject.Find("MainRope").GetComponent(); int count = mainRope.getSegments().Count; for (int i = 0; i < count; i++) { freeSegments.Add(i); } } int num = (int)Mathf.Ceil(Random.value * (float)freeSegments.Count); if (num >= 0 && num < freeSegments.Count) { int index = freeSegments[num]; freeSegments.RemoveAt(num); GameObject seg = mainRope.getSegments()[index].seg; Vector3 position = seg.transform.position; GameObject gameObject = Object.Instantiate(prefab, position, Quaternion.identity); GameObject gameObject2 = gameObject.transform.GetChild(0).gameObject; Rope component = gameObject2.GetComponent(); GameObject seg2 = component.getSegments()[0].seg; UnityEngine.Joint[] components = seg2.GetComponents(); FixedJoint fixedJoint = seg2.AddComponent(); fixedJoint.connectedBody = seg.GetComponent(); lstPrefab.Add(gameObject); } } private void Update() { if (Input.GetKeyDown(KeyCode.I)) { attachNewPrefabToMainRope(); } if (Input.GetKeyDown(KeyCode.D)) { int num = (int)(Mathf.Ceil(Random.value * (float)lstPrefab.Count) - 1f); if (num >= 0 && num < lstPrefab.Count) { GameObject obj = lstPrefab[num]; lstPrefab.RemoveAt(num); Object.Destroy(obj); } } } }