Search Unity

Looking for a better solution for this Statement chain

Discussion in 'Scripting' started by SD2020_, Sep 25, 2017.

  1. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    Hi there,

    I have this Function written which assigns a Image to a List if it contains and ID.

    However I want to do this without doing a large sum of if statements as I will have over 50 different lists to check.

    Is there a way of assigning an Image to a List so it just automatically knows which image to use?

    Here is my code for drawing checking if the list contains the ID::
    Code (CSharp):
    1.         static void HierarchyItemCB(int instanceID, Rect selectionRect)
    2.         {
    3.             Rect r = new Rect(selectionRect);
    4.             r.x = r.width - 20;
    5.             r.width = 18;
    6.  
    7.             if (_LightComponentList.Contains(instanceID))
    8.                 DrawIcons(r, t_IconLight);
    9.  
    10.             if (_ListCanvas.Contains(instanceID))
    11.                 DrawIcons(r, t_IconCanvas);
    12.  
    13.             if (_ListEventSystem.Contains(instanceID))
    14.                 DrawIcons(r, t_IconEvent);
    15.  
    16.             if (_ListCamera.Contains(instanceID))
    17.                 DrawIcons(r, t_IconCamera);
    18.         }
    I just want to remove this ball of If statements and have something like a separate function to handle this without a massive clutter.


    Thanks in advance.

    Kind regards,
    Scott D
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    50 Lists?
    May I ask what you're trying to do? Perhaps there's a much different yet more elegant solution to this.
     
  3. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
  4. SD2020_

    SD2020_

    Joined:
    Nov 3, 2015
    Posts:
    107
    Might I ask if you can paste what you found in the comment section for some reason I cant seem to load the webpage open..

    the 50 lists was a bit of a large number to ball park with but Just looking for a solution to handle something on the scale!
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I might suggest a Dictionary<int, GameObject> (or whatever type the t_IconLight etc. are). Then you can set this dictionary up just once, probably wherever you're currently setting up _ListCamera etc., and then the method above reduces to:
    Code (CSharp):
    1. static void HierarchyItemCB(int instanceID, Rect selectionRect)
    2.         {
    3.             Rect r = new Rect(selectionRect);
    4.             r.x = r.width - 20;
    5.             r.width = 18;
    6.             DrawIcons(r, iconMap.Value(instanceID));
    7.         }
     
    makeshiftwings and SD2020_ like this.
  6. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
     
  7. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I was going to post what JoeStrout posted. This is pretty much exactly what dictionaries were made for.