Ejemplo n.º 1
0
        public static UIPanel Create(CreatePanelParams 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;
                }
            }

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

            uiPanel.relativeLocation        = input.relativeLocation;
            uiPanel.transform.parent        = input.parent;
            uiPanel.transform.localPosition = parentAnchor + input.relativeLocation;
            uiPanel.transform.localRotation = Quaternion.identity;
            uiPanel.transform.localScale    = Vector3.one;
            uiPanel.width     = input.width;
            uiPanel.height    = input.height;
            uiPanel.margin    = input.margin;
            uiPanel.radius    = input.radius;
            uiPanel.thickness = input.thickness;
            uiPanel.backgroundGeometryStyle = input.backgroundGeometryStyle;
            uiPanel.source_material         = input.material;
            uiPanel.baseColor.useConstant   = false;
            uiPanel.baseColor.reference     = input.color;

            MeshFilter meshFilter = go.GetComponent <MeshFilter>();

            if (meshFilter != null)
            {
                meshFilter.sharedMesh =
                    (uiPanel.backgroundGeometryStyle == BackgroundGeometryStyle.Tube)
                        ? UIUtils.BuildRoundedRectTube(input.width, input.height, input.margin, input.radius)
                        : UIUtils.BuildRoundedBox(input.width, input.height, input.margin, input.thickness);

                uiPanel.Anchor = Vector3.zero; // TODO: thickness goes +Z and Anchor stays zero? or thickness goes -Z and Anchor follows the surface?

                BoxCollider coll = go.GetComponent <BoxCollider>();
                if (coll != null)
                {
                    Vector3 initColliderCenter = meshFilter.sharedMesh.bounds.center;
                    Vector3 initColliderSize   = meshFilter.sharedMesh.bounds.size;
                    coll.center    = initColliderCenter;
                    coll.size      = initColliderSize;
                    coll.isTrigger = true;
                }
            }

            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.renderingLayerMask = 2; // "LightLayer 1"

                uiPanel.SetColor(input.color.value);
            }

            UIUtils.SetRecursiveLayer(go, "CameraHidden");

            return(uiPanel);
        }