Ejemplo n.º 1
0
 protected void UpdateColliderBounds()
 {
     foreach (var handle in handles)
     {
         var handleBounds = VisualUtils.GetMaxBounds(GetVisual(handle).gameObject);
         UpdateColliderBounds(handle, handleBounds.size);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates the corner visual and returns the bounds of the created visual
        /// </summary>
        /// <param name="parent">parent of visual</param>
        /// <param name="isFlattened">instantiate in flattened mode - slate</param>
        /// <returns>bounds of the created visual</returns>
        private Bounds CreateVisual(GameObject parent, bool isFlattened)
        {
            // figure out which prefab to instantiate
            GameObject cornerVisual = null;

            areHandlesFlattened = isFlattened;
            GameObject prefabType = isFlattened ? config.HandleSlatePrefab : config.HandlePrefab;

            if (prefabType == null)
            {
                // instantiate default prefab, a cube with box collider
                cornerVisual = GameObject.CreatePrimitive(PrimitiveType.Cube);
                cornerVisual.transform.parent        = parent.transform;
                cornerVisual.transform.localPosition = Vector3.zero;
                // deactivate collider on visuals and register for deletion - actual collider
                // of handle is attached to the handle gameobject, not the visual
                var collider = cornerVisual.GetComponent <BoxCollider>();
                collider.enabled = false;
                Object.Destroy(collider);
            }
            else
            {
                cornerVisual = GameObject.Instantiate(prefabType, parent.transform);
            }

            if (isFlattened)
            {
                // Rotate 2D slate handle asset for proper orientation
                cornerVisual.transform.Rotate(0, 0, -90);
            }

            cornerVisual.name = visualsName;

            // this is the size of the corner visuals
            var   cornerbounds = VisualUtils.GetMaxBounds(cornerVisual);
            float maxDim       = Mathf.Max(Mathf.Max(cornerbounds.size.x, cornerbounds.size.y), cornerbounds.size.z);

            cornerbounds.size = maxDim * Vector3.one;

            // we need to multiply by this amount to get to desired scale handle size
            var invScale = cornerbounds.size.x == 0.0f ? 0.0f : config.HandleSize / cornerbounds.size.x;

            cornerVisual.transform.localScale = new Vector3(invScale, invScale, invScale);

            VisualUtils.ApplyMaterialToAllRenderers(cornerVisual, config.HandleMaterial);

            return(cornerbounds);
        }
        private void UpdateColliderType()
        {
            foreach (var handle in handles)
            {
                // remove old colliders
                bool shouldCreateNewCollider = false;
                var  oldBoxCollider          = handle.GetComponent <BoxCollider>();

                // Caution, Destroy() will destroy one frame later.
                // Do not check later for presence this frame!
                if (oldBoxCollider != null && config.HandlePrefabColliderType == HandlePrefabCollider.Sphere)
                {
                    shouldCreateNewCollider = true;
                    Object.Destroy(oldBoxCollider);
                }

                var oldSphereCollider = handle.GetComponent <SphereCollider>();
                if (oldSphereCollider != null && config.HandlePrefabColliderType == HandlePrefabCollider.Box)
                {
                    shouldCreateNewCollider = true;
                    Object.Destroy(oldSphereCollider);
                }

                if (shouldCreateNewCollider)
                {
                    // attach new collider
                    var     handleBounds         = VisualUtils.GetMaxBounds(GetVisual(handle).gameObject);
                    float   maxDim               = VisualUtils.GetMaxComponent(handleBounds.size);
                    float   invScale             = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;
                    Vector3 colliderSizeScaled   = handleBounds.size * invScale;
                    Vector3 colliderCenterScaled = handleBounds.center * invScale;
                    if (config.HandlePrefabColliderType == HandlePrefabCollider.Box)
                    {
                        BoxCollider collider = handle.gameObject.AddComponent <BoxCollider>();
                        collider.size   = colliderSizeScaled;
                        collider.center = colliderCenterScaled;
                        collider.size  += config.ColliderPadding;
                    }
                    else
                    {
                        SphereCollider sphere = handle.gameObject.AddComponent <SphereCollider>();
                        sphere.center  = colliderCenterScaled;
                        sphere.radius  = VisualUtils.GetMaxComponent(colliderSizeScaled) * 0.5f;
                        sphere.radius += VisualUtils.GetMaxComponent(config.ColliderPadding);
                    }
                }
            }
        }
        /// <summary>
        /// Creates the corner visual and returns the bounds of the created visual
        /// </summary>
        /// <param name="handleIndex">cornerIndex</param>
        /// <param name="parent">parent of visual</param>
        /// <param name="isFlattened">instantiate in flattened mode - slate</param>
        /// <returns>bounds of the created visual</returns>
        private Bounds CreateVisual(int handleIndex, GameObject parent, bool isFlattened)
        {
            // figure out which prefab to instantiate
            GameObject handleVisual = null;

            areHandlesFlattened = isFlattened;
            GameObject prefabType = isFlattened ? config.HandleSlatePrefab : config.HandlePrefab;

            if (prefabType == null)
            {
                // instantiate default prefab, a cube with box collider
                handleVisual = GameObject.CreatePrimitive(PrimitiveType.Cube);

                // deactivate collider on visuals and register for deletion - actual collider
                // of handle is attached to the handle gameobject, not the visual
                var collider = handleVisual.GetComponent <BoxCollider>();
                collider.enabled = false;
                UnityEngine.Object.Destroy(collider);
            }
            else
            {
                handleVisual = GameObject.Instantiate(prefabType, parent.transform);
            }

            // this is the size of the corner visuals
            var   handleVisualBounds = VisualUtils.GetMaxBounds(handleVisual);
            float maxDim             = VisualUtils.GetMaxComponent(handleVisualBounds.size);
            float invScale           = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;

            handleVisual.name                    = visualsName;
            handleVisual.transform.parent        = parent.transform;
            handleVisual.transform.localScale    = new Vector3(invScale, invScale, invScale);
            handleVisual.transform.localPosition = Vector3.zero;
            handleVisual.transform.localRotation = Quaternion.identity;


            Quaternion realignment = GetRotationRealignment(handleIndex, isFlattened);

            parent.transform.localRotation = realignment;

            if (config.HandleMaterial != null)
            {
                VisualUtils.ApplyMaterialToAllRenderers(handleVisual, config.HandleMaterial);
            }

            return(handleVisualBounds);
        }
        private Bounds CreateVisual(int handleIndex, GameObject parent)
        {
            GameObject handleVisual;
            GameObject prefabType = config.HandlePrefab;

            if (prefabType != null)
            {
                handleVisual = Object.Instantiate(prefabType);
            }
            else
            {
                handleVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                // We only want the Primitive sphere mesh, but CreatePrimitive will
                // give us a sphere collider too. Remove the sphere collider here
                // so we can manually add our own properly configured collider later.
                var collider = handleVisual.GetComponent <SphereCollider>();
                collider.enabled = false;

                // Caution, Destroy() will destroy one frame later.
                // Do not check later for presence this frame!
                Object.Destroy(collider);
            }

            // handleVisualBounds are returned in handleVisual-local space.
            Bounds handleVisualBounds = VisualUtils.GetMaxBounds(handleVisual);
            float  maxDim             = VisualUtils.GetMaxComponent(handleVisualBounds.size);
            float  invScale           = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;

            handleVisual.name                    = visualsName;
            handleVisual.transform.parent        = parent.transform;
            handleVisual.transform.localScale    = new Vector3(invScale, invScale, invScale);
            handleVisual.transform.localPosition = Vector3.zero;
            handleVisual.transform.localRotation = Quaternion.identity;

            Quaternion realignment = GetRotationRealignment(handleIndex);

            parent.transform.localRotation = realignment;

            if (config.HandleMaterial != null)
            {
                VisualUtils.ApplyMaterialToAllRenderers(handleVisual, config.HandleMaterial);
            }

            return(handleVisualBounds);
        }
        private void UpdateColliderType()
        {
            foreach (var handle in handles)
            {
                // remove old colliders
                bool shouldCreateNewCollider = false;
                var  oldBoxCollider          = handle.GetComponent <BoxCollider>();
                if (oldBoxCollider != null && config.HandlePrefabColliderType == HandlePrefabCollider.Sphere)
                {
                    shouldCreateNewCollider = true;
                    Object.Destroy(oldBoxCollider);
                }

                var oldSphereCollider = handle.GetComponent <SphereCollider>();
                if (oldSphereCollider != null && config.HandlePrefabColliderType == HandlePrefabCollider.Box)
                {
                    shouldCreateNewCollider = true;
                    Object.Destroy(oldSphereCollider);
                }

                if (shouldCreateNewCollider)
                {
                    // attach new collider
                    var     handleBounds         = VisualUtils.GetMaxBounds(GetVisual(handle).gameObject);
                    var     invScale             = handleBounds.size.x == 0.0f ? 0.0f : config.HandleSize / handleBounds.size.x;
                    Vector3 colliderSizeScaled   = handleBounds.size * invScale;
                    Vector3 colliderCenterScaled = handleBounds.center * invScale;
                    if (config.HandlePrefabColliderType == HandlePrefabCollider.Box)
                    {
                        BoxCollider collider = handle.gameObject.AddComponent <BoxCollider>();
                        collider.size   = colliderSizeScaled;
                        collider.center = colliderCenterScaled;
                        collider.size  += config.ColliderPadding;
                    }
                    else
                    {
                        SphereCollider sphere = handle.gameObject.AddComponent <SphereCollider>();
                        sphere.center  = colliderCenterScaled;
                        sphere.radius  = colliderSizeScaled.x * 0.5f;
                        sphere.radius += VisualUtils.GetMaxComponent(config.ColliderPadding);
                    }
                }
            }
        }
        private Bounds CreateVisual(int handleIndex, GameObject parent)
        {
            GameObject midpointVisual;
            GameObject prefabType = config.HandlePrefab;

            if (prefabType != null)
            {
                midpointVisual = Object.Instantiate(prefabType);
            }
            else
            {
                midpointVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                // deactivate collider on visuals and register for deletion - actual collider
                // of handle is attached to the handle gameobject, not the visual
                var collider = midpointVisual.GetComponent <SphereCollider>();
                collider.enabled = false;
                Object.Destroy(collider);
            }

            Quaternion realignment = GetRotationRealignment(handleIndex);

            midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation;

            Bounds midpointBounds = VisualUtils.GetMaxBounds(midpointVisual);
            float  maxDim         = VisualUtils.GetMaxComponent(midpointBounds.size);
            float  invScale       = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;

            midpointVisual.name                    = visualsName;
            midpointVisual.transform.parent        = parent.transform;
            midpointVisual.transform.localScale    = new Vector3(invScale, invScale, invScale);
            midpointVisual.transform.localPosition = Vector3.zero;

            if (config.HandleMaterial != null)
            {
                VisualUtils.ApplyMaterialToAllRenderers(midpointVisual, config.HandleMaterial);
            }

            return(midpointBounds);
        }
        private Bounds CreateVisual(int handleIndex, GameObject parent)
        {
            GameObject midpointVisual;
            GameObject prefabType = config.HandlePrefab;

            if (prefabType != null)
            {
                midpointVisual = Object.Instantiate(prefabType);
            }
            else
            {
                midpointVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                if (config.HandlePrefabColliderType != HandlePrefabCollider.Sphere)
                {
                    Object.Destroy(midpointVisual.GetComponent <SphereCollider>());
                }
            }

            Quaternion realignment = GetRotationRealignment(handleIndex);

            midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation;

            Bounds midpointBounds = VisualUtils.GetMaxBounds(midpointVisual);
            float  maxDim         = VisualUtils.GetMaxComponent(midpointBounds.size);
            float  invScale       = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;

            midpointVisual.name                    = visualsName;
            midpointVisual.transform.parent        = parent.transform;
            midpointVisual.transform.localScale    = new Vector3(invScale, invScale, invScale);
            midpointVisual.transform.localPosition = Vector3.zero;

            if (config.HandleMaterial != null)
            {
                VisualUtils.ApplyMaterialToAllRenderers(midpointVisual, config.HandleMaterial);
            }

            return(midpointBounds);
        }