void Test_using() { using (TemporaryObject tmp = new TemporaryObject(obj = new GameObject())) { Assert_True( obj ); } Assert_False( obj ); }
void Test_ManualDisposal() { TemporaryObject tmp = new TemporaryObject(obj = new GameObject()); Assert_True( obj ); tmp.Dispose(); Assert_False( obj ); }
void Test_Leak() { using (TemporaryObject tmp = new TemporaryObject(obj = new GameObject())) { Assert_True( obj ); tmp.Leak(); } Assert_True( obj ); // force cleanup Object.DestroyImmediate( obj ); Assert_False( obj ); }
void Test_CleanupOnException() { try { using (TemporaryObject tmp = new TemporaryObject(obj = new GameObject())) { Assert_True( obj ); throw new System.Exception(); } } catch (System.Exception) { // by now the object is destroyed already Assert_False( obj ); } finally { // and here too Assert_False( obj ); } }
protected virtual void Update() { for (int i = m_TemporaryObjects.Count - 1; i >= 0; i--) { TemporaryObject to = m_TemporaryObjects[i]; to.remainingTime -= Time.deltaTime; if (to.remainingTime <= 0f) { ReturnObject(m_TemporaryObjects[i].rentedObject); } else { m_TemporaryObjects[i] = to; } } }
/// <summary> /// Rent an object of the given prefab. If duration is set to a positive value, the object will be returned after that amount of seconds. /// </summary> /// <param name="prefab">Reference to the prefab you want to get an instance of.</param> /// <param name="position">The position that the object should be activated at. Position will be changed before activating the object, so OnEnable calls will know about the updated position.</param> /// <param name="rotation">The rotation that the object should be activated at. Rotation will be changed before activating the object, so OnEnable calls will know about the updated rotation.</param> /// <param name="parent">The parent transform that the object will be a child of. Parent will be changed before activating the object, so OnEnable calls will know about the updated parent.</param> /// <param name="duration">How long the instance should be active. If value is positive, the object will be automatically returned after that amount of seconds.</param> /// <returns>Rented object instance.</returns> public GameObject RentObject(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent, float duration = -1) { if (m_ObjectPoolDictionary.ContainsKey(prefab)) { if (m_ObjectPoolDictionary[prefab] == null) { RecreatePoolFromPrefab(prefab, 3); } // Get an instance of the requested prefab GameObject rentedObject = m_ObjectPoolDictionary[prefab].RentObject(position, rotation, parent); if (rentedObject == null) { return(null); } if (!m_RentedObjects.ContainsKey(prefab)) { // If an entry for this prefab doesn't already exists in the dictionary of rented out objects, we create it. List <GameObject> newList = new List <GameObject>(); newList.Add(rentedObject); m_RentedObjects.Add(prefab, newList); } else { m_RentedObjects[prefab].Add(rentedObject); } // If rented object is set to have a specific duration, add it to list of temporary rented objects, so it'll be returned automatically if (duration >= 0f) { TemporaryObject to = new TemporaryObject(); to.rentedObject = rentedObject; to.remainingTime = duration; m_TemporaryObjects.Add(to); } PoolObject pgo = rentedObject.GetComponent <PoolObject>(); if (pgo != null) { pgo.SetOwningPoolManager(this); } return(rentedObject); } return(null); }
private void SpawnPhantom() { GameObject phantom = new GameObject("Phantom"); phantom.transform.position = this.transform.position; SpriteRenderer renderer = phantom.AddComponent <SpriteRenderer>(); renderer.color = phantomColor; renderer.sprite = this.GetComponent <SpriteRenderer>().sprite; renderer.flipX = this.GetComponent <SpriteRenderer>().flipX; renderer.sortingLayerName = GetComponent <SpriteRenderer>().sortingLayerName; renderer.sortingOrder = GetComponent <SpriteRenderer>().sortingOrder - 1; if (fadeoutPhantoms) { FadeoutObject fadeout = phantom.AddComponent <FadeoutObject>(); fadeout.lifetime = phantomLifetime; } else { TemporaryObject temporary = phantom.AddComponent <TemporaryObject>(); temporary.lifetime = phantomLifetime; } }
IEnumerator MegaBeam(float length, bool useRapidCannon) { if (useRapidCannon) { StartCoroutine(RapidCannon(0.5f, Mathf.Clamp(Netman.difficulty - 2, 0, 5))); } yield return(null); if (BackGunActive) { int diffmod2 = diffmod; GameObject gun3 = GameObject.Find("GunMid/Gun"); //Vector3 gun3point = BMan.GetEnemyBulletSpawnPoint(gun3.transform.position + (Quaternion.Euler(gun3.transform.rotation.eulerAngles) * new Vector3(-25.2f,0f,0f))); GameObject.Instantiate(Resources.Load("Sounds/ChargeUpLaser"), Vector3.zero, Quaternion.identity); GameObject laserflare = GameObject.Instantiate(Resources.Load("LaserFlare"), Vector3.zero, Quaternion.identity) as GameObject; laserflare.transform.parent = gun3.transform; laserflare.transform.localPosition = new Vector3(-7.4f, 0f, 0f); LensFlare f = laserflare.GetComponent <LensFlare>(); float brightness = 0f; while (brightness < 1.5f) { if (CurrentHealth <= 0) { yield break; } brightness += Time.deltaTime; f.brightness = brightness; yield return(new WaitForEndOfFrame()); } yield return(new WaitForSeconds(0.5f)); GameObject beam = GameObject.Instantiate(Resources.Load("Beam"), Vector3.zero, Quaternion.identity) as GameObject; GameObject beam2 = null; GameObject beam3 = null; GameObject sound = GameObject.Instantiate(Resources.Load("Sounds/LaserShot"), Vector3.zero, Quaternion.identity) as GameObject; GameObject.Instantiate(Resources.Load("Sounds/LaserFiring"), Vector3.zero, Quaternion.identity); beam.transform.parent = gun3.transform; beam.transform.localPosition = new Vector3(-7.4f, 0f, 0f); beam.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, 180f)); TemporaryObject t = sound.GetComponent <TemporaryObject>(); t.life = length + 2f; if (Netman.difficulty + diffmod2 >= 3) { beam2 = GameObject.Instantiate(Resources.Load("Beam"), Vector3.zero, Quaternion.identity) as GameObject; beam2.transform.parent = gun3.transform; beam2.transform.localPosition = new Vector3(-7.4f, 0f, 0f); beam2.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, 135f)); beam3 = GameObject.Instantiate(Resources.Load("Beam"), Vector3.zero, Quaternion.identity) as GameObject; beam3.transform.parent = gun3.transform; beam3.transform.localPosition = new Vector3(-7.4f, 0f, 0f); beam3.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, 225f)); } brightness = 4f; while (brightness > 1f) { if (CurrentHealth <= 0) //If he dies mid beam { if (beam) { GameObject.Destroy(beam); } if (beam2) { GameObject.Destroy(beam2); } if (beam3) { GameObject.Destroy(beam3); } if (sound) { GameObject.Destroy(sound); } yield break; } brightness -= Time.deltaTime * 3; f.brightness = brightness; yield return(new WaitForEndOfFrame()); } brightness = 1f; f.brightness = brightness; GameObject beamcollider = GameObject.Instantiate(Resources.Load("LaserCollider"), Vector3.zero, Quaternion.identity) as GameObject; GameObject beamcollider2 = null; GameObject beamcollider3 = null; if (Netman.difficulty + diffmod2 >= 3) { beamcollider2 = GameObject.Instantiate(Resources.Load("LaserCollider"), Vector3.zero, Quaternion.identity) as GameObject; beamcollider3 = GameObject.Instantiate(Resources.Load("LaserCollider"), Vector3.zero, Quaternion.identity) as GameObject; } float time = 0f; breaklaser = false; while (time < length) { time += Time.deltaTime; if (CurrentHealth <= 0) //If he dies mid beam { if (beam) { GameObject.Destroy(beam); } if (beam2) { GameObject.Destroy(beam2); } if (beam3) { GameObject.Destroy(beam3); } if (sound) { GameObject.Destroy(sound); } yield break; } Vector3 beamspawn = BMan.GetEnemyBulletSpawnPoint(beam.transform.position); beamcollider.transform.position = beamspawn; beamcollider.transform.rotation = beam.transform.rotation; if (Netman.difficulty + diffmod2 >= 3) { beamspawn = BMan.GetEnemyBulletSpawnPoint(beam2.transform.position); beamcollider2.transform.position = beamspawn; beamcollider2.transform.rotation = beam2.transform.rotation; beamspawn = BMan.GetEnemyBulletSpawnPoint(beam3.transform.position); beamcollider3.transform.position = beamspawn; beamcollider3.transform.rotation = beam3.transform.rotation; } if (breaklaser) { time = length; if (sound != null) { GameObject.Destroy(sound); } } yield return(new WaitForEndOfFrame()); } GameObject.Destroy(beamcollider); if (Netman.difficulty + diffmod2 >= 3) { GameObject.Destroy(beamcollider2); GameObject.Destroy(beamcollider3); } LineRenderer l = beam.GetComponent <LineRenderer>(); LineRenderer l2 = null; LineRenderer l3 = null; if (Netman.difficulty + diffmod2 >= 3) { l2 = beam2.GetComponent <LineRenderer>(); l3 = beam3.GetComponent <LineRenderer>(); } //AudioSource a = beamsound.GetComponent<AudioSource>(); float width = 20f; while (width > 0f && brightness > 0f) { if (CurrentHealth <= 0) { yield break; } width = Mathf.Clamp(width - Time.deltaTime * 20, 0, 20); brightness = Mathf.Clamp(brightness - Time.deltaTime / 2f, 0, 1); f.brightness = brightness; l.SetWidth(width, width); if (Netman.difficulty + diffmod2 >= 3) { l2.SetWidth(width, width); l3.SetWidth(width, width); } //a.volume = brightness; yield return(new WaitForEndOfFrame()); } f.brightness = 0; yield return(new WaitForSeconds(0.2f)); GameObject.Destroy(beam); GameObject.Destroy(laserflare); if (Netman.difficulty + diffmod2 >= 3) { GameObject.Destroy(beam2); GameObject.Destroy(beam3); } } }
void CreateCompactPrefab(List<Transform> cutBoxes, List<Transform> shadowBoxes, GameObject orig) { using(TemporaryObject tmp = new TemporaryObject(new GameObject(base_name + "--loc", typeof(SlideShow)))) { GameObject root_go = tmp.Instance as GameObject; root_go.GetComponent<SlideShow>().slices = orig.GetComponent<ImportedCloud>().slices; root_go.transform.localRotation = orig.transform.localRotation; GameObject node = new GameObject("Objects"); ProceduralUtils.InsertAtOrigin(node, root_go); foreach(Transform box in cutBoxes) { SetupBoxCloud(node.transform, box); } GameObject shadows_node = new GameObject("Shadow"); ProceduralUtils.InsertAtOrigin(shadows_node, node); foreach(Transform box in shadowBoxes) { SetupBoxCloud(shadows_node.transform, box); } GameObject preview = Object.Instantiate( orig.transform.FindChild("Preview").gameObject ) as GameObject; preview.name = "Full Cloud Preview"; ProceduralUtils.InsertAtOrigin(preview, root_go); SetupSound(root_go); SetupAnimation(root_go); IOExt.Directory.EnsureExists(Prefs.CompactPrefabsPath); Object prefab = EditorUtility.CreateEmptyPrefab(Prefs.CompactPrefab( root_go.name )); EditorUtility.ReplacePrefab(root_go, prefab); // save minimeshes... foreach(MeshFilter mf in node.GetComponentsInChildren<MeshFilter>()) { AssetDatabase.AddObjectToAsset(mf.sharedMesh, prefab); } } }
void ImportCloud(string cloud_path, string bin_path, string prefab_path) { string baseName = Path.GetFileNameWithoutExtension(cloud_path); using (TemporaryObject tmp = new TemporaryObject(new GameObject(baseName, typeof(ImportedCloud))) ) { GameObject root = tmp.Instance as GameObject; GameObject previewGo = new GameObject("Preview", typeof(MeshFilter), typeof(MeshRenderer)); previewGo.transform.parent = root.transform; new GameObject("CutBoxes").transform.parent = root.transform; iCloud = root.GetComponent<ImportedCloud>(); // parse the list of slices from .cloud file List<Slice> sliceList = ParseCloud(cloud_path); // sort slices on size sliceList.Sort((slice1, slice2) => (slice2.size - slice1.size)); iCloud.slices = sliceList.ToArray (); sliceSampleSize = Prefs.OrigPreviewSize / System.Math.Min( Prefs.PreviewSlicesCount, sliceList.Count ); // shuffle individual slices and sample prefs.origPreviewSize from first prefs.previewSlicesCount slices // sampled points end up in meshConv ShuffleSlicesAndSample(bin_path); Material material = AssetDatabase.LoadAssetAtPath("Assets/Materials/FastPoint.mat", typeof(Material)) as Material; previewGo.GetComponent<MeshRenderer>().material = material; // generate preview mesh by sampling some number of points over the whole original Mesh mesh = meshConv.MakeMesh(); mesh.name = baseName + "-preview"; meshConv.Convert(mesh); // it's ok, it's just a preview mesh, this will stop Unity from complaining... mesh.RecalculateNormals(); previewGo.GetComponent<MeshFilter>().mesh = mesh; iCloud.skin = AssetDatabase.LoadAssetAtPath("Assets/GUI/ExplodedGUI.GUISkin",typeof(GUISkin)) as GUISkin; // turn it -90 degrees... root.transform.Rotate(-90,0,0); // save the branch into the prefab IOExt.Directory.EnsureExists(Prefs.PrefabsPath); Object prefab = EditorUtility.CreateEmptyPrefab(prefab_path); EditorUtility.ReplacePrefab(root, prefab); // save mesh into prefab and attach it to the Preview game object AssetDatabase.AddObjectToAsset(mesh, prefab); // do this last, after the rest succeeded IOExt.Directory.EnsureExists(Prefs.ImportedPath); FileUtil.MoveFileOrDirectory(bin_path, Prefs.ImportedBin(bin_path)); FileUtil.MoveFileOrDirectory(cloud_path, Prefs.ImportedCloud(cloud_path)); } }