예제 #1
0
    //Refresh the radius of colliders and the line-renderer
    void RefreshRadii()
    {
        //set the line renderer width
        var rend = ComponentUtilities.GetOrAddComponent <LineRenderer>(this);

        rend.startWidth = radius * 2f;
        rend.endWidth   = radius * 2f;

        //set collision radii
        if (start != null)
        {
            ComponentUtilities.GetOrAddComponent <SphereCollider>(start).radius = radius;
        }
        if (end != null)
        {
            ComponentUtilities.GetOrAddComponent <SphereCollider>(end).radius = radius;
        }
        foreach (RopeSegment seg in (root ?? transform).GetComponentsInChildren <RopeSegment>(true))
        {
            if (seg != null)
            {
                var coll = seg.GetOrAddComponent <CapsuleCollider>();
                coll.radius = radius;
                coll.height = SegmentLength;
            }
        }
    }
        public void GetOrAddComponent_ComponentExists_FindsComponent()
        {
            MeshFilter addedComponent = testObject.AddComponent <MeshFilter>();

            MeshFilter res = ComponentUtilities.GetOrAddComponent <MeshFilter>(testObject);

            Assert.NotNull(res);
            Assert.AreEqual(addedComponent, res);
        }
        public void GetOrAddComponent_ComponentDoesNotExist_ComponentAdded()
        {
            MeshFilter res = ComponentUtilities.GetOrAddComponent <MeshFilter>(testObject);

            Assert.NotNull(res);
            MeshFilter checkRes = testObject.GetComponent <MeshFilter>();

            Assert.NotNull(checkRes);
            Assert.AreEqual(res, checkRes);
        }
예제 #4
0
        /// <summary>
        /// Constructs a GameObject and populates it with the mesh of the geometry constructor and the material of the material constructor
        /// </summary>
        /// <param name="parent">Optional; Parents the GameObject to the specified transform</param>
        /// <returns>Returns the created GameObject</returns>
        public GameObject ConstructObject(Transform parent = null)
        {
            // get a GameObject from the pool
            GameObject gameObject = ObjectPool <GameObject> .RequestResource(() => { return(new GameObject("Object Constructor Result")); });

            if (parent != null)
            {
                gameObject.transform.parent = parent;
            }

            // we can create objects without geometry but this is unnecessarily complex so we inform the dev about this
            if (GeometryConstructor == null || GeometryConstructor.Vertices.Count == 0)
            {
                Debug.LogWarning("Created object with empty geometry."
                                 + "This might not be intended since you can just use Instantiate oder the ObjectPool.");
                gameObject.name = "New GameObject";
            }
            else
            {
                // set up the GameObject
                gameObject.name = GeometryConstructor.Name;

                MeshFilter   meshFilter   = ComponentUtilities.GetOrAddComponent <MeshFilter>(gameObject);
                MeshRenderer meshRenderer = ComponentUtilities.GetOrAddComponent <MeshRenderer>(gameObject);

                if (MaterialConstructor != null)
                {
                    meshRenderer.material = MaterialConstructor.ConstructMaterial();
                }
                else
                {
                    meshRenderer.material = new Material(Shader.Find("Standard"));
                }
                Mesh mesh = GeometryConstructor.ConstructMesh();
                meshFilter.sharedMesh = mesh;
            }

            return(gameObject);
        }