Пример #1
0
 public RoomReferences()
 {
     I = this;
 }
        private void AddButtonsToDisplayal(Exhibit e, GameObject displayal)
        {
            var genButtonPrefab = ObjectFactory.GetGenerationButtonPrefab();

            // Get IDs and potential reference.
            List <string> ids = new List <string>();

            if (e.Metadata.ContainsKey(GenerationMetadata.MemberIds.GetKey()))
            {
                var json          = e.Metadata[GenerationMetadata.MemberIds.GetKey()];
                var idDoublePairs = JsonConvert.DeserializeObject <List <IdDoublePair> >(json);
                idDoublePairs.ForEach(it => ids.Add(it.id));
            }

            var idConfig = displayal.AddComponent <IdListPair>();

            idConfig.associatedIds = ids;
            idConfig.originId      = e.Metadata[GenerationMetadata.ObjectId.GetKey()];

            List <GenerationMethod> types = GenerationMethodUtil.GetButtonTypes(ids.Count);

            var localScale      = displayal.transform.localScale;
            var offset          = -2.0f;
            var shift           = (types.Count - 1) / 2.0f;
            var xFactor         = 0.2f / localScale.x;
            var zFactor         = 0.2f / localScale.z;
            var displayalHeight = displayal.GetComponent <BoxCollider>().size.z;

            var parentGameObj = new GameObject("Button Wrapper");

            parentGameObj.transform.SetParent(displayal.transform.parent, false);
            parentGameObj.transform.localPosition = displayal.transform.localPosition;
            parentGameObj.transform.localRotation = displayal.transform.localRotation;
            parentGameObj.transform.localScale    = displayal.transform.localScale;

            var wrapperObj = parentGameObj.AddComponent <ButtonWrapper>();

            wrapperObj.displayal = displayal.GetComponent <Displayal>();

            for (var i = 0; i < types.Count; i++)
            {
                var method = types[i];

                var genButton = Instantiate(genButtonPrefab, parentGameObj.transform, false);
                genButton.name = "Generation Button (" + method.GetName() + ")";

                genButton.transform.localRotation = Quaternion.Euler(90.0f, 0.0f, 180.0f);
                genButton.transform.localPosition =
                    new Vector3(offset * (i - shift) * xFactor, 0.5f, 0.5f * displayalHeight + 0.75f * zFactor);
                genButton.transform.localScale = new Vector3(
                    0.75f * xFactor,
                    0.75f * zFactor, // Y/Z inverted due to different prefab orientation...
                    2.0f
                    );

                // Button.
                RoomReferences references = null;

                if (e.Metadata.ContainsKey(GenerationMetadata.References.GetKey()))
                {
                    var refJson = e.Metadata[GenerationMetadata.References.GetKey()];
                    references = JsonConvert.DeserializeObject <RoomReferences>(refJson);
                }

                GenerationButton genButtonComponent = genButton.GetComponent <GenerationButton>();
                genButtonComponent.type = method;
                if (references != null && references.References.ContainsKey(method.ToString()))
                {
                    // If a room was already generated from this button, link it.
                    genButtonComponent.targetRoomId = references.References[method.ToString()];
                }

                TextMesh genButtonText = genButton.GetComponentInChildren <TextMesh>();
                genButtonText.text = method.GetName();
            }
        }