/// <summary>
    /// Add leds on the segement
    /// </summary>
    /// <param name="segment">segement to add leds</param>
    /// <param name="universe">universe of the segement</param>
    /// <param name="address">start adress of led</param>
    /// <param name="position">position of the led</param>
    void addToScene(Transform segment, int universe, int address, int position)
    {
        // Instantiate a led at the right position in the segment parent
        Transform led_object = Instantiate(Led, segment.position + segment.right * position * scale, segment.rotation);

        led_object.SetParent(segment);

        //Set the name (key in dictionnary)
        led_object.name = "Led-" + universe + "-" + address;
        // Set opacity of the sprite to null
        SpriteRenderer sprite_renderer = led_object.GetComponent <SpriteRenderer>();

        sprite_renderer.color = new Color(1f, 1f, 1f, LED_OFF_VALUE);
        List <SpriteRenderer> list_led;

        if (leds_by_universe.TryGetValue(led_object.name, out list_led))
        {
            list_led.Add(sprite_renderer);
        }
        else
        {
            // Store the led in the table
            leds_by_universe.Add(led_object.name, new List <SpriteRenderer> {
                sprite_renderer
            });
        }

        // Display the value in the editor
        DebugLed debug = led_object.GetComponent <DebugLed>();

        debug.setConfigLed(universe, address);
    }
Esempio n. 2
0
    // Use this for initialization
    void Start()
    {
        // Get all gameobjects with the tag "Segment".
        // GameObject[] segments_objects = GameObject.FindGameObjectsWithTag("Segment");
        Transform[] segments_objects = GetComponentsInChildren <Transform>();

        // Order gameobjects segments in 2 dimensionnal array (see documentation).
        foreach (Transform segment_object in segments_objects)
        {
            // The name of gameObject is used to set the coordinate.
            // => Segment-XX-YY
            string name = segment_object.gameObject.name;

            // Check the format of the name
            if (name.Length < 13)
            {
                continue;
            }

            // Check the format of the name
            if (name.Substring(0, 8) != "Segment-")
            {
                continue;
            }

            // Coordinates
            int x, y;

            // Try to decode the name of the gameObject
            if (int.TryParse(name.Substring(8, 2), out x) && int.TryParse(name.Substring(11, 2), out y))
            {
                // Store the segment
                segments_dic.Add(x + "-" + y, segment_object);

                // Display the value in the editor
                DebugSegment debug = segment_object.GetComponent <DebugSegment>();
                debug.setCoordinates(x, y, false);
            }
        }

        // Read json file to get the adress of each led (univers + channel).
        // The adress were stored for each segment.
        TextAsset cube_asset = Resources.Load <TextAsset>("json/Cube");

        JsonSegment[] json_segments = JsonHelper.FromJson <JsonSegment>(cube_asset.text);

        // For all segments stored in the json
        // Find the corresponding unity object in the scene
        // Instantiate the leds of the segment
        foreach (JsonSegment json in json_segments)
        {
            // Find the segment in the scene
            Transform segment;
            bool      segment_found = segments_dic.TryGetValue(json.axis_x + "-" + json.axis_y, out segment);

            // If the object exist in Unity scene
            if (segment_found)
            {
                int address  = json.channel;  // Start address in the universe
                int universe = json.universe; // Universe

                // For each leds set the channel and the universe
                for (int i = 0; i < segmentLength; i++)
                {
                    // Instantiate a led at the right position in the segment parent
                    Transform led_object = Instantiate(Led, segment.position + segment.right * i * scale, segment.rotation);
                    led_object.SetParent(segment);

                    //Set the name (key in dictionnary)
                    led_object.name = "Led-" + universe + "-" + address;

                    // Set opacity of the sprite to null
                    SpriteRenderer sprite_renderer = led_object.GetComponent <SpriteRenderer>();
                    sprite_renderer.color = new Color(1f, 1f, 1f, LED_OFF_VALUE);

                    // Store the led in the table
                    leds_by_universe.Add(led_object.name, sprite_renderer);

                    // Display the value in the editor
                    DebugLed debug = led_object.GetComponent <DebugLed>();
                    debug.setConfigLed(universe, address);

                    // Set the next address : each DMX led take 3 bytes
                    address += 3;

                    // Take the next universe if the first universe is full
                    if (address > DMX_UNIVERSE_SIZE - DMX_LED_SIZE)
                    {
                        universe++;
                        address = 0;
                    }
                }
            }
        }
    }