Ejemplo n.º 1
0
        void Start()
        {
            Configurator      = FindObjectOfType <ConfigurationManager>();
            ElementController = GetComponentInChildren <ElementController>();
            ElementMetadata   = GetComponentInChildren <ElementMetadata>();

            // Set up buttons.
            MainCamera = Camera.main;

            if (!ElementMetadata.LeftSlot.Contains(-1))
            {
                LeftPlusButton = Instantiate(PlusButtonPrefab);
                LeftPlusButton.transform.parent = transform;
                LeftPlusButton.name             = "Left Plus Button";
                LeftPlusButton.GetComponentInChildren <Canvas>().worldCamera = MainCamera;
                LeftPlusButton.GetComponentInChildren <Button>().onClick.AddListener(() => ButtonClicked(Placement.Left));
                LeftPlusButton.SetActive(false);
            }

            if (!ElementMetadata.RightSlot.Contains(-1))
            {
                RightPlusButton = Instantiate(PlusButtonPrefab);
                RightPlusButton.transform.parent = transform;
                RightPlusButton.name             = "Right Plus Button";
                RightPlusButton.GetComponentInChildren <Canvas>().worldCamera = MainCamera;
                RightPlusButton.GetComponentInChildren <Button>().onClick.AddListener(() => ButtonClicked(Placement.Right));
                RightPlusButton.SetActive(false);
            }

            if (!ElementMetadata.UpperSlot.Contains(-1))
            {
                UpperPlusButton = Instantiate(PlusButtonPrefab);
                UpperPlusButton.transform.parent = transform;
                UpperPlusButton.name             = "Upper Plus Button";
                UpperPlusButton.GetComponentInChildren <Canvas>().worldCamera = MainCamera;
                UpperPlusButton.GetComponentInChildren <Button>().onClick.AddListener(() => ButtonClicked(Placement.Upper));
                UpperPlusButton.SetActive(false);
            }
        }
        // Note: initialization logic HAS to happen inside Awake instead of Start
        // Otherwise, the collider won't have enough time to instantiate when element is placed.
        void Awake()
        {
            ElementMetadata = GetComponent <ElementMetadata>();

            ElementRenderers = GetComponentsInChildren <Renderer>();
            ColorHistory     = new Stack <Color>();
            OriginalColors   = new Dictionary <int, Color>();
            OriginalTextures = new Dictionary <int, Texture>();
            foreach (Renderer renderer in ElementRenderers)
            {
                foreach (Material material in renderer.materials)
                {
                    OriginalColors.Add(material.GetInstanceID(), material.color);
                    OriginalTextures.Add(material.GetInstanceID(), material.mainTexture);
                }
            }

            // Rebuild the collider mesh.
            var colliderMesh = new Mesh();

            colliderMesh.vertices  = ColliderMeshVertices;
            colliderMesh.triangles = ColliderMeshTriangles;
            colliderMesh.uv        = ColliderMeshUV;
            colliderMesh.RecalculateNormals();
            colliderMesh.RecalculateBounds();

            // Add a mesh collider to the prefab.
            var collider = gameObject.AddComponent <MeshCollider>();

            collider.sharedMesh = colliderMesh;
            collider.convex     = true;
            collider.isTrigger  = true;

            // Add physics to the element
            var rb = gameObject.AddComponent <Rigidbody>();

            rb.isKinematic = true;
        }