Skip to main content
Typo
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401
public class ClockDigit : MonoBehaviour {

    //  Assuming you number your segments as follows:
    //   -- 0 --
    //  |       |
    //  5       1
    //  |       |
    //   -- 6 --
    //  |       |
    //  4       2
    //  |       |
    //   -- 3 --

    // Store a lookup table for which segments 
    // should be active when displaying each digit.
    static readonly bool[,] SEGMENT_IS_ACTIVE = new bool[,] {
    {
        {true,  true,  true,  true,  true,  true,  false}, // 0
        {false, true,  true,  false, false, false, false}, // 1
        {true,  true,  false, true,  true,  false, true }, // 2
        {true,  true,  true,  true,  false, false, true }, // 3
        {false, true,  true,  false, false, true,  true }, // 4
        {true,  false, true,  true,  false, true,  true }, // 5
        {true,  false, true,  true,  true,  true,  true }, // 6
        {true,  true,  true,  false, false, false, false}, // 7
        {true,  true,  true,  true,  true,  true,  true }, // 8
        {true,  true,  true,  true,  false, true,  true }  // 9
    };    

    public Color32 activeColour = Color.red;
    public Color32 inactiveColour = Color.black;

    public SpriteRenderer[] segments = new SpriteRenderer[7];

    public void Display(int number) {
        var digit = number % 10;
        if (digit < 0) digit *= -1;

        for (int i = 0; i < 7; i++) {
            if (SEGMENT_IS_ACTIVE[digit, i]) {
                segments[i].color = activeColour;
            } else {
                segments[i].color = inactiveColour;
            }
        }
    }   
}
public class ClockDigit : MonoBehaviour {

    //  Assuming you number your segments as follows:
    //   -- 0 --
    //  |       |
    //  5       1
    //  |       |
    //   -- 6 --
    //  |       |
    //  4       2
    //  |       |
    //   -- 3 --

    // Store a lookup table for which segments 
    // should be active when displaying each digit.
    static readonly bool[,] SEGMENT_IS_ACTIVE = new bool[,] {
    {
        {true,  true,  true,  true,  true,  true,  false}, // 0
        {false, true,  true,  false, false, false, false}, // 1
        {true,  true,  false, true,  true,  false, true }, // 2
        {true,  true,  true,  true,  false, false, true }, // 3
        {false, true,  true,  false, false, true,  true }, // 4
        {true,  false, true,  true,  false, true,  true }, // 5
        {true,  false, true,  true,  true,  true,  true }, // 6
        {true,  true,  true,  false, false, false, false}, // 7
        {true,  true,  true,  true,  true,  true,  true }, // 8
        {true,  true,  true,  true,  false, true,  true }  // 9
    };    

    public Color32 activeColour = Color.red;
    public Color32 inactiveColour = Color.black;

    public SpriteRenderer[] segments = new SpriteRenderer[7];

    public void Display(int number) {
        var digit = number % 10;
        if (digit < 0) digit *= -1;

        for (int i = 0; i < 7; i++) {
            if (SEGMENT_IS_ACTIVE[digit, i]) {
                segments[i].color = activeColour;
            } else {
                segments[i].color = inactiveColour;
            }
        }
    }   
}
public class ClockDigit : MonoBehaviour {

    //  Assuming you number your segments as follows:
    //   -- 0 --
    //  |       |
    //  5       1
    //  |       |
    //   -- 6 --
    //  |       |
    //  4       2
    //  |       |
    //   -- 3 --

    // Store a lookup table for which segments 
    // should be active when displaying each digit.
    static readonly bool[,] SEGMENT_IS_ACTIVE = new bool[,] {

        {true,  true,  true,  true,  true,  true,  false}, // 0
        {false, true,  true,  false, false, false, false}, // 1
        {true,  true,  false, true,  true,  false, true }, // 2
        {true,  true,  true,  true,  false, false, true }, // 3
        {false, true,  true,  false, false, true,  true }, // 4
        {true,  false, true,  true,  false, true,  true }, // 5
        {true,  false, true,  true,  true,  true,  true }, // 6
        {true,  true,  true,  false, false, false, false}, // 7
        {true,  true,  true,  true,  true,  true,  true }, // 8
        {true,  true,  true,  true,  false, true,  true }  // 9
    };    

    public Color32 activeColour = Color.red;
    public Color32 inactiveColour = Color.black;

    public SpriteRenderer[] segments = new SpriteRenderer[7];

    public void Display(int number) {
        var digit = number % 10;
        if (digit < 0) digit *= -1;

        for (int i = 0; i < 7; i++) {
            if (SEGMENT_IS_ACTIVE[digit, i]) {
                segments[i].color = activeColour;
            } else {
                segments[i].color = inactiveColour;
            }
        }
    }   
}
Correcting negative case
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401
public class ClockDigit : MonoBehaviour {

    //  Assuming you number your segments as follows:
    //   -- 0 --
    //  |       |
    //  5       1
    //  |       |
    //   -- 6 --
    //  |       |
    //  4       2
    //  |       |
    //   -- 3 --

    // Store a lookup table for which segments 
    // should be active when displaying each digit.
    static readonly bool[,] SEGMENT_IS_ACTIVE = new bool[,] {
    {
        {true,  true,  true,  true,  true,  true,  false}, // 0
        {false, true,  true,  false, false, false, false}, // 1
        {true,  true,  false, true,  true,  false, true }, // 2
        {true,  true,  true,  true,  false, false, true }, // 3
        {false, true,  true,  false, false, true,  true }, // 4
        {true,  false, true,  true,  false, true,  true }, // 5
        {true,  false, true,  true,  true,  true,  true }, // 6
        {true,  true,  true,  false, false, false, false}, // 7
        {true,  true,  true,  true,  true,  true,  true }, // 8
        {true,  true,  true,  true,  false, true,  true }  // 9
    };    

    public Color32 activeColour = Color.red;
    public Color32 inactiveColour = Color.black;

    public SpriteRenderer[] segments = new SpriteRenderer[7];

    public void Display(int number) {
        var digit = number % 10;
        if (digit < 0) digit +=*= 10;-1;

        for (int i = 0; i < 7; i++) {
            if (SEGMENT_IS_ACTIVE[digit, i]) {
                segments[i].color = activeColour;
            } else {
                segments[i].color = inactiveColour;
            }
        }
    }   
}
public class ClockDigit : MonoBehaviour {

    //  Assuming you number your segments as follows:
    //   -- 0 --
    //  |       |
    //  5       1
    //  |       |
    //   -- 6 --
    //  |       |
    //  4       2
    //  |       |
    //   -- 3 --

    // Store a lookup table for which segments 
    // should be active when displaying each digit.
    static readonly bool[,] SEGMENT_IS_ACTIVE = new bool[,] {
    {
        {true,  true,  true,  true,  true,  true,  false}, // 0
        {false, true,  true,  false, false, false, false}, // 1
        {true,  true,  false, true,  true,  false, true }, // 2
        {true,  true,  true,  true,  false, false, true }, // 3
        {false, true,  true,  false, false, true,  true }, // 4
        {true,  false, true,  true,  false, true,  true }, // 5
        {true,  false, true,  true,  true,  true,  true }, // 6
        {true,  true,  true,  false, false, false, false}, // 7
        {true,  true,  true,  true,  true,  true,  true }, // 8
        {true,  true,  true,  true,  false, true,  true }  // 9
    };    

    public Color32 activeColour = Color.red;
    public Color32 inactiveColour = Color.black;

    public SpriteRenderer[] segments = new SpriteRenderer[7];

    public void Display(int number) {
        var digit = number % 10;
        if (digit < 0) digit += 10;

        for (int i = 0; i < 7; i++) {
            if (SEGMENT_IS_ACTIVE[digit, i]) {
                segments[i].color = activeColour;
            } else {
                segments[i].color = inactiveColour;
            }
        }
    }   
}
public class ClockDigit : MonoBehaviour {

    //  Assuming you number your segments as follows:
    //   -- 0 --
    //  |       |
    //  5       1
    //  |       |
    //   -- 6 --
    //  |       |
    //  4       2
    //  |       |
    //   -- 3 --

    // Store a lookup table for which segments 
    // should be active when displaying each digit.
    static readonly bool[,] SEGMENT_IS_ACTIVE = new bool[,] {
    {
        {true,  true,  true,  true,  true,  true,  false}, // 0
        {false, true,  true,  false, false, false, false}, // 1
        {true,  true,  false, true,  true,  false, true }, // 2
        {true,  true,  true,  true,  false, false, true }, // 3
        {false, true,  true,  false, false, true,  true }, // 4
        {true,  false, true,  true,  false, true,  true }, // 5
        {true,  false, true,  true,  true,  true,  true }, // 6
        {true,  true,  true,  false, false, false, false}, // 7
        {true,  true,  true,  true,  true,  true,  true }, // 8
        {true,  true,  true,  true,  false, true,  true }  // 9
    };    

    public Color32 activeColour = Color.red;
    public Color32 inactiveColour = Color.black;

    public SpriteRenderer[] segments = new SpriteRenderer[7];

    public void Display(int number) {
        var digit = number % 10;
        if (digit < 0) digit *= -1;

        for (int i = 0; i < 7; i++) {
            if (SEGMENT_IS_ACTIVE[digit, i]) {
                segments[i].color = activeColour;
            } else {
                segments[i].color = inactiveColour;
            }
        }
    }   
}
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

I'd attack it something like this...

Have one script that represents the digit as a whole, and assign to that script in the Inspector an array of SpriteRenderers representing the seven segments. They don't need their own custom scripts, since the only functionality we need from them is setting the colour, which the built-in SpriteRenderer component has already.

Then store a lookup table that encodes which segments should be on for each digit value.

When we set the clock digit to a new number, we can loop over all our segments, and set them to the corresponding state from the lookup table for that value.

The one tricky part here is that you need to assign your segment references to the correct array slots so they get the right value from the lookup table, but that should be easy enough to do with a little care.

public class ClockDigit : MonoBehaviour {

    //  Assuming you number your segments as follows:
    //   -- 0 --
    //  |       |
    //  5       1
    //  |       |
    //   -- 6 --
    //  |       |
    //  4       2
    //  |       |
    //   -- 3 --

    // Store a lookup table for which segments 
    // should be active when displaying each digit.
    static readonly bool[,] SEGMENT_IS_ACTIVE = new bool[,] {
    {
        {true,  true,  true,  true,  true,  true,  false}, // 0
        {false, true,  true,  false, false, false, false}, // 1
        {true,  true,  false, true,  true,  false, true }, // 2
        {true,  true,  true,  true,  false, false, true }, // 3
        {false, true,  true,  false, false, true,  true }, // 4
        {true,  false, true,  true,  false, true,  true }, // 5
        {true,  false, true,  true,  true,  true,  true }, // 6
        {true,  true,  true,  false, false, false, false}, // 7
        {true,  true,  true,  true,  true,  true,  true }, // 8
        {true,  true,  true,  true,  false, true,  true }  // 9
    };    

    public Color32 activeColour = Color.red;
    public Color32 inactiveColour = Color.black;

    public SpriteRenderer[] segments = new SpriteRenderer[7];

    public void Display(int number) {
        var digit = number % 10;
        if (digit < 0) digit += 10;

        for (int i = 0; i < 7; i++) {
            if (SEGMENT_IS_ACTIVE[digit, i]) {
                segments[i].color = activeColour;
            } else {
                segments[i].color = inactiveColour;
            }
        }
    }   
}