// Use this for initialization
    /// <summary>
    /// function start form unity3d
    /// Create all the led on the specific part of the cube
    /// </summary>
    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 < 17)
            {
                continue;
            }

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

            // Coordinates
            int x, y, z, reversed;


            // 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) &&
                int.TryParse(name.Substring(14, 2), out z) && int.TryParse(name.Substring(17, 1), out reversed))
            {
                // Store the segment
                segments_dic.Add(x + "-" + y + "-" + z, segment_object);

                // Display the value in the editor
                DebugSegment debug = segment_object.GetComponent <DebugSegment>();
                debug.setCoordinates(x, y, z, System.Convert.ToBoolean(reversed));
            }
        }

        // Read csv file to get the address of each led (univers + channel).
        // The adress were stored for each segment.
        //TextAsset cube_asset = Resources.Load<TextAsset>("xyz/xyz");
        //JsonSegment[] json_segments = JsonHelper.FromJson<JsonSegment>(cube_asset.text);

        TextAsset         cube_asset   = Resources.Load <TextAsset>("xyz/xyz");
        List <CsvSegment> csv_segments = CsvHelper.FromCsv(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 (CsvSegment csv in csv_segments)
        {
            // Find the segment in the scene
            Transform segment;
            bool      segment_found = segments_dic.TryGetValue(csv.AxisX + "-" + csv.AxisY + "-" + csv.AxisZ, out segment);
            // If the object exist in Unity scene
            if (segment_found && csv.Universe1 != -1)
            {
                bool reverse = isReversed(csv.Reverse, segment.GetComponent <DebugSegment>().revertAdress);
                if (csv.Universe2 == -1)
                {
                    addLedNormalSegement(segment, csv, reverse);
                }
                else
                {
                    addSeparatedSegment(segment, csv, reverse);
                }
            }
        }
    }
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;
                    }
                }
            }
        }
    }