コード例 #1
0
        public static UIKeyView Create(CreateParams input)
        {
            GameObject go = new GameObject(input.widgetName);

            go.tag = "UICollider";

            // Find the anchor of the parent if it is a UIElement
            Vector3 parentAnchor = Vector3.zero;

            if (input.parent)
            {
                UIElement elem = input.parent.gameObject.GetComponent <UIElement>();
                if (elem)
                {
                    parentAnchor = elem.Anchor;
                }
            }

            UIKeyView uiKeyView = go.AddComponent <UIKeyView>(); // NOTE: also creates the MeshFilter, MeshRenderer and Collider components

            uiKeyView.relativeLocation        = input.relativeLocation;
            uiKeyView.transform.parent        = input.parent;
            uiKeyView.transform.localPosition = parentAnchor + input.relativeLocation;
            uiKeyView.transform.localRotation = Quaternion.identity;
            uiKeyView.transform.localScale    = Vector3.one;
            uiKeyView.width                     = input.width;
            uiKeyView.height                    = input.height;
            uiKeyView.margin                    = input.margin;
            uiKeyView.thickness                 = input.thickness;
            uiKeyView.source_material           = input.material;
            uiKeyView.baseColor.useConstant     = false;
            uiKeyView.baseColor.reference       = input.bgcolor;
            uiKeyView.textColor.useConstant     = false;
            uiKeyView.textColor.reference       = input.fgcolor;
            uiKeyView.pushedColor.useConstant   = false;
            uiKeyView.pushedColor.reference     = input.pushedColor;
            uiKeyView.selectedColor.useConstant = false;
            uiKeyView.selectedColor.reference   = input.selectedColor;

            // Setup the Meshfilter
            MeshFilter meshFilter = go.GetComponent <MeshFilter>();

            if (meshFilter != null)
            {
                meshFilter.sharedMesh = UIUtils.BuildRoundedBox(input.width, input.height, input.margin, input.thickness);
                uiKeyView.Anchor      = Vector3.zero;
                BoxCollider coll = go.GetComponent <BoxCollider>();
                if (coll != null)
                {
                    Vector3 initColliderCenter = meshFilter.sharedMesh.bounds.center;
                    Vector3 initColliderSize   = meshFilter.sharedMesh.bounds.size;
                    if (initColliderSize.z < UIElement.collider_min_depth_shallow)
                    {
                        coll.center = new Vector3(initColliderCenter.x, initColliderCenter.y, UIElement.collider_min_depth_shallow / 2.0f);
                        coll.size   = new Vector3(initColliderSize.x, initColliderSize.y, UIElement.collider_min_depth_shallow);
                    }
                    else
                    {
                        coll.center = initColliderCenter;
                        coll.size   = initColliderSize;
                    }
                    coll.isTrigger = true;
                }
            }

            // Setup the MeshRenderer
            MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>();

            if (meshRenderer != null && input.material != null)
            {
                // Clone the material.
                meshRenderer.sharedMaterial = Instantiate(input.material);
                Material sharedMaterial = meshRenderer.sharedMaterial;
                meshRenderer.shadowCastingMode  = UnityEngine.Rendering.ShadowCastingMode.Off;
                meshRenderer.rendererPriority   = 1;
                meshRenderer.renderingLayerMask = 2; // "LightLayer 1"

                uiKeyView.SetColor(input.bgcolor.value);
            }

            UIUtils.SetRecursiveLayer(go, "CameraHidden");

            return(uiKeyView);
        }